Search This Blog

Wednesday 23 July 2014

Simple C logics for interviews

1.C program to find given number is prime or not

 int num,i,count=0;

 printf("Enter a number: ");

 scanf("%d",&num);

 for(i=2;i<=num/2;i++){

 if(num%i==0){

 count++;

 break;

 }

 }

 if(count==0 && num!= 1)

 printf("%d is a prime number",num);

 else

 printf("%d is not a prime number",num);

 return 0; 

 _______________



2.C program for prime numbers between 1 to 100

int num,i,count; 

 for(num = 1;num<=100;num++){

 count = 0;

 for(i=2;i<=num/2;i++){

 if(num%i==0){

 count++;

 break;

 }

 } 



 if(count==0 && num!= 1)

 printf("%d ",num);

 } 

 return 0; 

 _________________ 

3.Sum of prime numbers from 1 to 100

int num,i,count,sum=0;

 for(num = 1;num<=100;num++){

 count = 0;

 for(i=2;i<=num/2;i++){

 if(num%i==0){

 count++;

 break;

 }

 }



 if(count==0 && num!= 1)

 sum = sum + num;

 }

 printf("Sum of prime numbers is: %d ",sum); 

 return 0;

4.C program to find whether a number is palindrome or not

int num,r,sum=0,temp;

 printf("Enter a number: ");

 scanf("%d",&num);

 temp=num;

 while(num){

 r=num%10;

 num=num/10;

 sum=sum*10+r;

 }

 if(temp==sum)

 printf("%d is a palindrome",temp);

 else

 printf("%d is not a palindrome",temp);

 return 0;

_________________________

5.String Reverse 

char *str,*rev;

 int i,j;

 printf("\nEnter a string:");

 scanf("%s",str);

 for(i=strlen(str)-1,j=0;i>=0;i--,j++)

 rev[j]=str[i];

 rev[j]='\0';

 if(strcmp(rev,str))

 printf("\nThe string is not a palindrome");

 else

 printf("\nThe string is a palindrome");

 return 0;

 _____________________________

6.C program to calculate sum of digits



 int num,sum=0,r;

 printf("Enter a number: ");

 scanf("%d",&num);

 while(num){

 r=num%10;

 num=num/10;

 sum=sum+r;

 }

 printf("Sum of digits of number: %d",sum);

 return 0;

 ________________________

7.Reverse of a number in c using while loop

int num,r,reverse=0;

 printf("Enter any number: ");

 scanf("%d",&num);

 while(num){

 r=num%10;

 reverse=reverse*10+r;

 num=num/10;

 }

 printf("Reversed of number: %d",reverse);

 return 0;

_______________________

8.Code for swapping in c

int a,b,temp; 

 printf("Enter any two integers: ");

 scanf("%d%d",&a,&b);

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

 temp = a;

 a = b;

 b = temp;

{(a=a+b;

b=a-b;

a=a-b;)}

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

 return 0;

_____________________

9.Simple program of c find the largest number

int n,num,i;

 int big; 

 printf("Enter the values of n: ");

 scanf("%d",&n); 

 printf("Number %d",1);

 scanf("%d",&big);

 for(i=2;i<=n;i++){

 printf("Number %d: ",i);

 scanf("%d",&num);

 if(big<num)

 big=num;

 } 

 printf("Largest number is: %d",big);

 return 0; 

 ___________________

 10.Extract digits from integer in c language 

 int num,temp,factor=1;

 printf("Enter a number: ");

 scanf("%d",&num);

 temp=num;

 while(temp){

 temp=temp/10;

 factor = factor*10;

 }

 printf("Each digits of given number are: ");

 while(factor>1){

 factor = factor/10;

 printf("%d ",num/factor);

 num = num % factor;

 }

 return 0; 

__________________________

11. Write a c program to check whether a number is strong or not

int num,i,f,r,sum=0,temp;

 printf("Enter a number: ");

 scanf("%d",&num); 

 temp=num;

 while(num){

 i=1,f=1;

 r=num%10;

 while(i<=r){

 f=f*i;

 i++;

 }

 sum=sum+f;

 num=num/10;

 }

 if(sum==temp)

 printf("%d is a strong number",temp);

 else

 printf("%d is not a strong number",temp);

 return 0;

 __________________________________

 12.Warp to check a number is Armstrong 

 int num,r,sum=0,temp;

 printf("Enter a number: ");

 scanf("%d",&num);

 temp=num;

 while(num!=0){

 r=num%10;

 num=num/10;

 sum=sum+(r*r*r);

 }

 if(sum==temp)

 printf("%d is an Armstrong number",temp);

 else

 printf("%d is not an Armstrong number",temp);

 return 0;

No comments:

Post a Comment