1) Write a program to check whether a string is palindrome/not, without using string header file?
2) Program for prime number or not?
3) Generate fibonacci series up to n?
4) Factorial of a number using recursion
5) Write a program for Leap year?
6) Write a program to print all the perfect numbers from 5 to 100?
7) Program for LCM(Lowest Common Multiple) and GCM(Greatest Common Divisor) ?
8) Swap without using third variable?
9) Repeated Digits in a given number?
10) Number of even and odd digits in a number?
11) Program for converting floatto Binary Display?
12) float (*test(char a))(int b,int c) Explain this declaration?
13) Write a Program to display of C code or C file...
14) find the number is power of two or not?
15) How to reverse the bits in an interger ?
16) Count the number of 1's in a integer
17) Program for what type of machine u r using?Big endian or Little Endian?
18) Reverse the string of an array without using another array.
19) Swap LSB & MSB of an integer
20) Convert one Endian to another
21) whether the particular bit is One or Zero?
22) find the size of structure without using sizeof operator
23) Convert Decimal to Binary
24) Convert decimal to Hexadecimal
25) Write your own String Copy function
26) OutPut of the program ?
printf("%s\n", argv[argc]);
28) What is the O/P of the below code ?
main()
{
floata = 2.1;
if(a == 2.1)
printf("EQUAL\n");
else
printf("NOT EQUAL\n");
}
29) How the structure typecasting is done?
30) C's Precedence Table
31) Write C Program Compilation Steps?
ANSWERS :
1) Write a program to check whether a string is palindrome/not, without using string header file?
/*1st method*/
#include<stdio.h>
#include<string.h>
#define size 26
void main()
{
char strsrc[size];
char strtmp[size];
printf("\n Enter String:= ");
gets(strsrc);
strcpy(strtmp,strsrc);
strrev(strtmp);
if(strcmp(strsrc,strtmp)==0)
printf("\n Entered string \"%s\" ispalindrome",strsrc);
else
printf("\n Entered string \"%s\" is not palindrome",strsrc);
getch();
}
/*2nd method*/
void main ()
{
inti,j,f=0;
chara[10];
gets(a);
for(i=0;a[i]!='\0';i++)
{
}
i--;
for(j=0;a[j]!='\0';j++,i--)
{
if(a[i]!=a[j])
f=1;
}
if(f==0)
printf("string is palindrome");
else
printf("string is not palindrome");
getch ();
}
2) Program for prime number or not?
main()
{
intnumber,i=0,prime_flag = 0;
printf("Enter any number\n");
scanf("%d",&number);
for(i = 2 ;i < number/2;i++)
{
if(number%i==0)
{
prime_flag=1;
break;
}
}
if(prime_flag)
printf("NOT A PRIME NUMBER\n");
else if(prime_flag == 0)
printf("IS PRIME NUMBER\n");
}
3) Generate fibonacci series up to n?
void main()
{
//variables
intcntr,range,number1=0,number2=1,number3;
printf(”Enter range to generate fibonacci series : “);
scanf(”%d”,&range);
printf(”%d–>%d–>”,number1,number2);
//generate series.
for(cntr=2;cntr<range;cntr++)
{
number3=number1+number2;
printf(”%d–>”,number3);
number1=number2;
number2=number3;
}
getch();
}
[ OR ]
intfibonacci(int n)
{
if(n == 1)
return0;
if(n==2)
rreturn 1;
returnfibonacci(n-1)+fibonacci(n-2);
}
4) Factorial of a number using recursion
intfactorial(int n)
{
if(n == 0)
return 1;
returnn*factorial(n-1);
}
void main()
{
int m,n;
printf("Enter a number");
scanf("%d",&n);
m = factorial(n);
printf("factorial = %d\n",n);
}
5) Write a program for Leap year?
int main()
{
int year;
printf("Enter any year to check\n");
scanf("%d",&year);
if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
printf("LEAP YEAR") /* leap */
else
printf("NOT A LEAP YEAR") /* no leap */
return 0;
}
6) Write a program to print all the perfect numbers from 5 to 100?
Perfect numbers are those numbers that equal the sum of all their divisors including 1 and excluding the number itself.
Eg: take 6 3+2+1 = 6 is a perfect number.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,d;
printf("Enter a Number");
scanf("%d", &a);
d=a;
for (int i=1;i<=a-1;i++)
{
b=a%i;
if(b==0)
c=c+i;
}
if(d==a)
printf("Entered Number is perfect number");
else
printf("Entered Number is not perfect");
getch();
}
This is a program to print all the perfect numbers from 5 to 100
void main()
{
int sum,i,j;
printf("The following numbers are perfect numbers:");
for(i=5; i<=100; i++)
{
sum=0;
for(j=1; j<i; j++)
{
if(i%j==0)
sum=sum+j;
}
if(sum==i)
printf(“%d ”,i);
}
getch();
}
7) Program for LCM(Lowest Common Multiple) and GCM(Greatest Common Divisor) ?
a & b are the numbers whose LCM and GCD is to be found
int LCM(int a,int b)
{
int n;
for(n=1;;n++)
{
if(n%a == 0 && n%b == 0)
return n;
}
printf(“LCM of a, b is %d ”,n);
}
int GCD(int a,int b)
{
int c;
if(a<b)
{
c = a;
a = b;
b = c;
}
while(1)
{
c = a%b;
if(c==0)
return b;
a = b;
b = c;
}
printf(“GCD of a, b is %d ”,b);
}
8) Swap without using third variable?
/* 1ST METHOD */
void main()
{
inta,b;
scanf("%d",a);
scanf("%d",b);
#if 1
a=a+b-(b=a);
#else/* or */
a=a+b;
b=a-b;
a=a-b;
#endif
printf("%d %d",a,b);
getch();
}
/* 2ND METHOD */
void main()
{
intx= 12, y= 66; /* the values to swap */
x^=y;
y^=x;
x^=y;
printf("%d %d",x,y);
}
/* 3rd Method */
int main()
{
int a = 20,b = 30;
a^=b^=a^=b;
printf("a = %d , b = %d \n",a,b);
return 0;
}
9) Repeated Digits in a given number?
#include <stdio.h>
#define TRUE 1
#define FALSE 0
typedef int Bool;
int main()
{
Bool digit_seen[10] = (0);
int digit;
long int n;
printf("Enter a number: ");
scanf("%ld", &n);
while (n > 0)
{
digit = n % 10;
if(digit_seen[digit])
break;
digit_seen[digit] = TRUE;
n/=10;
}
if (n > 0)
printf("Repeated digit\n\n");
else
printf("No repeated digit\n\n");
return 0;
}
10) Number of even and odd digits in a number?
void main()
{
int even_count = 0 , odd_count =0,i=0;
long number;
printf("Enter a number \n");
scanf("%ld",&number);
while(number)
{
if((number%10)%2 == 0)
even_count++;
else
odd_count++;
number = number / 10;
}
printf("even_count = %d , odd_count = %d\n",even_count,odd_count);
}
11) Program forconverting float to Binary Display
void main()
{
union{
//Assuming float and int are same size(4 Bytes)
float b;
unsigned int a;
}co;
unsigned int size=0,k=0;
printf("Please enter a float value : ");
scanf("%f",&co.b);
size=sizeof(co.a)*8;
for(k= 1<<(size-1) ;k ;k>>=1)
printf("%d",(co.a & k)&&1 );
}
12) float (*test(char a))(int b,int c); Explain this declaration?
test is a function,it takes a charand returns a pointer to a function which takes two int variable and returns a float
Two methods fora Function to return a function pointer
Direct Method :
float (*test(char a))(int b,int c);
Using typedef :
typedef float (*ptr)(int b,int c);
ptr test(char a);
13) Write a Program to display of c file...
void main (void)
{
char *str="type ""\""__FILE__"\"";
system(str);
}
14) Find the number is power of two or not?
void main (void)
{
if(n<2||n&(n-1))==0)
printf("n is Power of 2");
else
printf("n is not a Power of 2");
}
15) How to reverse the bits in an interger ?
void main()
{
int i = 0,k=0,n = 0x7ABBCCDD;
printf("BEFORE Bit Reverese : %0X\n",n);
for(i = 0; i < (sizeof(int)*8) ;i++)
{
k <<= 1;
k |= (n&(1<<i))?1:0;
}
printf("AFTER Bit Reverese : %0X\n",k);
}
16) Count the number of 1's in a integer
void main()
{
unsigned int num;
int count;
for(count=1;num = num&(num-1);count++);
printf("%d \n",count);
}
17) What type of machine u r using?Big endian or Little Endian?
void main()
{
int num = 1;
if(*(char *) &num == 1)
printf("Little Endian ");
else
printf("Big endian ");
}
[ OR ]
void main()
{
typedef UNION
{
char p;
int k;
}xyz;
xyz d;
d.k = 0xabcd;
printf("%x",d.p);
}
O/P :
AB=> little Endian
CD=>Big Endian
18) Reverse the string of an array without using another array.
void main()
{
char str[20],s;
int m,n=0;
printf("Enter a string");
scanf("%s",&str);
m = strlen(str)-1;
while(m>n)
{
s=str[n];
str[n]=str[m];
str[m]=s;
m--;
n++;
}
printf("%s",str);
}
[ OR ]
char revp(char *str, char*dstr)
{
char *p = dstr;
int i;
for(i = strlen(str);i>=0;i--)
{
*dstr = str[i];
dstr++;
}
*dstr='\0';
}
19) Swap LSB & MSB of an integer
void main()
{
#if 1
//1st method
unsigned int t1,t2;
unsigned num = 0xABCDEF12;
t1 = num >> 24;
t2 = num << 24;
num = ((num & 0x00ffff00) | t2 ) | ((num & 0x00ffff00) | t1 ) ;
#else
// [ OR ]
//2nd method
union {
unsignednum;
unsigned char buf[sizeof(int)];
}map;
unsigned char temp =0;
map.num = 0xABCDEF12;//value need to be swap LSB ,MSB
temp = map.buf[0];
map.buf[0] = map.buf[sizeof(int)-1];
map.buf[sizeof(int)-1] = temp;
num = map.num;
#endif
printf("%0x\n",num);
}
20) Convert one Endian to another
void main()
{
int n,b0,b1,b2,b3;
printf("Enter the Value");
scanf("%d",&n);
b0 = (n&0x000000ff)>>0;
b1 = (n&0x0000ff00)>>8;
b2 = (n&0x00ff0000)>>16;
b3 = (n&0xff000000)>>24;
n = ((b0<<24) | (b1<<16) | (b2<<8) | (b3<<0));
printf("The result is %d \n",n);
}
21) whether the particular bit is One or Zero in a integer
void main()
{
int n;
int bitpos,flag,result;
printf("read Numbers");
scanf("%d %d",&n,&bitpos);
flag = 1<<(bitpos-1);
result = flag&n;
if(result)
printf("One");
else
printf("Zero");
}
22) find the size of structure without using sizeof operator
struct abc
{
int a;
char b;
float c;
};
int main()
{
struct abc *p=NULL;
int size;
size = ((char *)(p+1)-(char *)p);
printf("%d",size);
return 0;
}
23) Convert Decimal to Binary
void ToBin(int n)
{
if(n>1)
ToBin(n/2);
printf("%d",n%2);
}
void main()
{
int number = 72;
ToBin(number);
}
24) Convert decimal to Hexadecimal
void ToHex(int n)
{
char *htab[]={"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
if(n>15)
ToHex(n/16);
printf("%s",htab[n%16]);
}
void main()
{
int number = 72;
ToHex(number);
}
25) Write your own String Copy function
void main()
{
char str1[]="Keep India beautiful";
char str2[40];
strcpy(str2,str1);
printf("\n %s",str2);
}
strcpy(char *t,char *s)
{
while(*s)
{
*t = *s;
t++;
s++;
}
*t = '\0';
}
//[ OR ]
strcpy(char *t,char *s)
{
while(*t++ = *s++);
}
26) OutPut of the program ?
printf("%s\n", argv[argc]);
Output: NULL
27) Write a program, which should through a segmentation fault ?
void main
{
char *s= "C WORLD";
*s = 'b';
}
28) What is the O/P of the below code ?
void main()
{
float a = 2.1;
if(a == 2.1)
printf("EQUAL\n");
else
printf("NOT EQUAL\n");
/* Output: NOT EQUAL :: 2.1 is double */
}
29) How the structure typecasting is done?
structure type casting is not possible :: only by address type cast can do
Example:
typedef struct{
int a;
int b;
float c;
char d;
}node1;
typedef struct{
int a;
int b;
float c;
char k;
}node2;
node1 xx;
node2 yy;
memset(&xx , 0 , sizeof(xx));
memset(&yy , 0 , sizeof(yy));
No comments:
Post a Comment