Search This Blog

Saturday, May 2, 2020

Program which are frequently asked in interviews [C-Programming]

          C-Programming:- 

In this tutorial you will learn:- 

 Program which are frequently asked in interviews


1. Fibonacci series:

#include<stdio.h>
int main ()
{
int n1=0, n2=1, n3,number;
int= i;
printf("enter the number of element");
scanf('%d%d",&number);
 printf("\n%d%d",n1,n2);  //printing 0 and 1
for(i=2 ; i<number ; ++i) // loop starts from 2 bacause 0 and 1 is already printed
{
 n3=n1 + n2 ;
printf("%d"n3);
n1=n2;
n2=n3;
}
return 0 ;
}

OUTPUT:

enter the number of element :7
0 1 1 2 3 5 8

3. Prime number:-

#include<stdio.h>
int main ()
{
int n, i,  m=0, flag=0 ;
printf("Enter the number to check prime :");
scanf("%d",&n);
m=n/2 ;
for(i=2 ; i<=m ; i++)
{
if(n%i==0)
{
printf("number is not prime");
}
flag=0 ;
break;
}
}
if(flag==0)
{
printf("number is prime");
return 0 ;
}

OUTPUT:

Enter the number to check prime: 11
number is prime
3.

 4. Palindrome number:

#include<stdio.h>
int main()
{
int n sum=0, rev temp;
printf("enter number to check palindrome number");
scanf("%d",&n);
temp=n;
while(n>0) 
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp=sum)
{
printf("number is palindrome");
}
else
{
printf("number is not palindrome");
}
return 0 ;
}

OUTPUT:

enter a number to check palindrome number:  121
number is palindrome

5. Factorial number:-

#includestdio.h>
int main()
{
int n, f=1, i;
printf("enter any number");
scanf("%d"&n);
for(i=1 ; i<n ; i++)
{
f=f*i;
}
printf("fact is: %d",f);
return 0 ;
}

OUTPUT:

enter any number: 3
fact is 6

 6.Amstrong number:

#include<stdio.h>
int mian()
{
int n, r, c, temp, sum=0;
printf("enter any number to check amstrong number");
scanf("%d",n);
temp=n;
while(n>0)
{
r=n%10;
c=r*r*r;
sum=sum+c ;
n=n/10;
}
if(temp=sum)
{
printf("nummber is amstrong");
}
else
{
printf("number is not amstrong");
}
return 0;
}

OUTPUT:

enter any number to check Armstrong number: 153
number is amstrong number


7. Swap number using without using third variable:-

#include<stdio.h>
int main ()
{
int n, a=10 , b=20;
printf("before swapping: a=10 ,  b =20);
a=a+b;  //a=30(10+20)
b=a-b; //b=10(30-20)
a=a-b; //a=20(30-10)
printf("after swapping :\n a= %d  b= %d",a,b);
return 0;
}

OUTPUT:

before swapping  a=10 , b=20
after swapping
a=20  b=10

8. Swap number using third variable:

#include<stdio.h>

int main ()

{

int a=10, b=20 , c;

printf("before swap a=10 , b =20");

c=a;

a=b;

b=c;

 printf("after swapping: a=%d , b=%d", a ,b);

return0;

}

OUTPUT:

before swapping: a=10 , b=20
after swapping: a=20 , b=10

9. Matrix multiplication:

In this program, we multply two matrices of any order but the order of row and column must be  same other wise the compiler ma confuse and show some errors in your program.
Include<stdio.h>
int mian()
{
clrscr();
int a[3][3] , b[3][3] , c[3][3];
int i, j, k;
printf("enter the elements of matrix a\n ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",a[i][j]);
}
}
printf("enter the element of matrix b\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",b[i][j]);
}
}
 printf("multiply of matrix a and b is :\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++) 
{
c[i][j=0;
for(k=0;k<3;k++)
{
c[i][j]=a[i][j] * b[i][j] ;
}
}
}
printf("result is = \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf('%d",c[i][j]);
}
printf("\n");
}
return0;
}

OUTPUT:-


enter the first matrix element=
1 1 1
2 2 2
3 3 3
enter the second matrix element=
1 1 1
2 2 2
3 3 3
multiply of the matrix=
6 6 6
12 12 12
18 18 18

10. Convert Decimal to Binary:-

Decimal Number

A decimal number is a base 10 number because it ranges from 0 to 9, there are total 10 digits between 0 to 9. Any combination of digits is a decimal number such as 23, 445, 132, 0, 2, etc.

Binary Number

The binary number is a base 2 number because it is either 0 or 1. Any combination of 0 and 1 is a binary number such as 1001, 101, 11111, 101010 etc
 #include<stdio.h>
int main()
{
int a[10] i, n;
printf("enter a number to convert");
scanf("%d"&n);
for(i=0;n>0;i++)
{
a[i]=n%2;
n=n/2;
}
printf("binary is :\n");
for(i=i;i>=0;i--)
{
  printf("%d",a[i]);
}
return 0 ;
}

OUTPUT:-

Enter the number to convert: 5 
Binary of Given Number is=101 

Thanks for reading.........


 

Mr. abhijeet dwivedi

Developer

Hey, I'm Abhijeet

0 comments:

Post a Comment

If you have any doubt, please let me know