Friday 26 August 2011

Important Basic C Programs in Interview Asked

(A) Write a c program to check given string is palindrome number or not

#include<string.h>
#include<stdio.h>
int main(){
  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;
}

Definition of Palindrome string:

A string is called palindrome if it symmetric. In other word a string is called palindrome if string remains same if its characters are reversed. For example: asdsa
If we will reverse it will remain same i.e. asdsa

Example of string palindrome:  a,b, aa,aba,qwertrewq etc.

(B) check the given number is palindrome number or not using c program

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

Definition of Palindrome number:

A number is called palindrome number if it is remain same when its digits are reversed. For example 121 is palindrome number. When we will reverse its digit it will remain same number i.e. 121

Examples of palindrome number: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191 etc.

(C) CHECKING LEAP YEAR USING C PROGRAM

#include<stdio.h>
#include<conio.h>
void main(){
    int year;
    clrscr();
    printf("Enter any year: ");
    scanf("%d",&year);
    if(((year%4==0)&&(year%100!=0))||(year%400==0))
         printf("%d is a leap year",year);
    else
         printf("%d is not a leap year",year);
    getch();
}

Definition of leap year :

Rule 1 : A year is called leap year if it is divisible by 400.
For example : 1600,2000  etc leap year while 1500,1700 are not leap year.
Rule 2 : If year is not divisible by 400 as well as 100 but it is divisible by 4 then
that year are also leap year.
For example:  2004,2008,1012 are leap year.

Algorithm of leap year :

IF year MODULER 400 IS 0
THEN leap_year
ELSE IF year MODULER 100 IS 0
THEN not_leap_year
ELSE IF year MODULER 4 IS 0
THEN leap_year
ELSE
not_leap_year



(D) Write a c program to check given number is strong number or not.

#include<stdio.h>
int main(){
  int num,i,f,r,sum=0,temp;
  printf("\nEnter 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;
}

Definition of strong number:

A number is called strong number if sum of the factorial of its digit is equal to number itself. For example: 145 since
1! + 4! + 5! = 1 + 24 + 120 = 145

(E) Check the given number is armstrong number or not using c program

#include<stdio.h>
int main(){
int num,r,sum=0,temp;
printf("\nEnter 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("\nThe number %d is an armstrong number",temp);
else
printf("\nThe number %d is not an armstrong number",temp);
return 0;
}

Definition of Armstrong number:

Definition for c programming point of view:
Those numbers which sum of the cube of its digits is equal to that number are known as Armstrong numbers. For example 153 since 1^3 + 5^3 + 3^3 = 1+ 125 + 9 =153
Other Armstrong numbers: 370,371,407 etc.
In general definition:
Those numbers which sum of its digits to power of number of its digits is equal to that number are known as Armstrong numbers.
Example 1: 153
Total digits in 153 is 3
And 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
Example 2: 1634
Total digits in 1634 is 4
And 1^4 + 6^4 + 3^4 +4^4 = 1 + 1296 + 81 + 64 =1634
Examples of Armstrong numbers: 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834, 1741725

(F) write the program for prime numbers?
Answer
# 1    

main()
{
int i,j=2,ch=0;
clrscr();
       printf("\nENTER ANY NUMBER");
       scanf("%d",&i);
  while(j<=i/2)
       {
        if(i%j==0)
          {
            printf("%d IS NOT PRIME",i);
            ch=1;
            break;
          }
        else
          {
           j++;
          }
      }
 if(ch==0)
   {
   printf("%d IS PRIME",i);
   }
}

(G) TO FIND FACTORIAL OF A NUMBER USING C PROGRAM

#include<stdio.h>
int main(){
  int i=1,f=1,num;
  printf("\nEnter a number:");
  scanf("%d",&num);
  while(i<=num){
      f=f*i;
      i++;
  }
  printf("\nFactorial of %d is:%d",num,f);
  return 0;
}

No comments:

Post a Comment

Complete Details about eLitmus pH Test at Rs:699/- Share your java material and fresher interview Information for us to Help Others... mail to : vhkrishnan.v@gmail.com