Friday 3 July 2015

C programs for Job Interviews

Welcome back guys !!
Here are some common C programs that are usually asked in the job interviews. I hope practicing these programs will be enough to get prepared for the same.

Find out the perfect number using C program

Perfect number is a positive number which sum of all positive divisors excluding that number is equal to that number. For example 6 is perfect number since divisor of 6 are 1, 2 and 3.  Sum of its divisor is
1 + 2+ 3 =6
6 is the smallest perfect number. Next perfect number is 28 since 1+ 2 + 4 + 7 + 14 = 28 Some more perfect numbers: 496, 8128

#include<stdio.h>
int main(){
  int n,i=1,sum=0;

  printf("Enter a number: ");
  scanf("%d",&n);

  while(i<n){
      if(n%i==0)
           sum=sum+i;
          i++;
  }
  if(sum==n)
      printf("%d is a perfect number",i);
  else
      printf("%d is not a perfect number",i);

  return 0;
}


OUTPUT:
Enter a number: 6
6 is a perfect number

C program to print perfect numbers from 1 to 100
#include<stdio.h>
int main(){
  int n,i,sum;
  printf("Perfect numbers are: ");
  for(n=1;n<=100;n++){
    i=1;
    sum = 0;
    while(i<n){
      if(n%i==0)
           sum=sum+i;
          i++;
    }
    if(sum==n)
      printf("%d ",n);
  }
  return 0;
}

OUTPUT:
Perfect numbers are: 6 28

Check the given number is armstrong number or not using C program
Definition of Armstrong number or what is an Armstrong number:
Definition according to 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.
#include<stdio.h>
int main(){
    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;
}

OUTPUT:
Enter a number: 153
153 is an Armstrong number

Check given number is prime number or not using C program
Definition of prime number:
A natural number greater than one has not any other divisors except 1 and itself. In other word we can say which has only two divisors 1 and number itself. For example: 5
Their divisors are 1 and 5.
Note: 2 is only even prime number.
Logic for prime number in c
We will take a loop and divide number from 2 to number/2. If the number is not divisible by any of the numbers then we will print it as prime number.

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

OUTPUT:
Enter a number: 5
5 is a prime number

SUM OF PRIME NUMBERS
#include<stdio.h>
int main(){
    int num,i,count,min,max,sum=0;
     printf("Enter min range: ");
     scanf("%d",&min);
    printf("Enter max range: ");
    scanf("%d",&max);
    for(num = min;num<=max;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;
}

OUTPUT:
Enter min range: 50
Enter max range: 100

C program to check a number is odd or even.


#include<stdio.h>
int main(){
    int number;
    printf("Enter any integer: ");
    scanf("%d",&number);
    if(number % 2 ==0)
         printf("%d is even number.",number);
    else
         printf("%d is odd number.",number);
    return 0;
}
Write a C program to check given number is palindrome number or not
Definition of Palindrome number or What is 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
Palindrome numbers examples: 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.

#include<stdio.h>
int main(){
    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;
}
C program to check if a number is palindrome using recursion
#include<stdio.h>
int checkPalindrome(int);
int main(){
    int num,sum;
    printf("Enter a number: ");
    scanf("%d",&num);
    sum = checkPalindrome(num);
    if(num==sum)
         printf("%d is a palindrome",num);
    else
    printf("%d is not a palindrome",num);
    return 0;
}
int checkPalindrome(int num){
    static int sum=0,r;
    if(num!=0){
         r=num%10;
         sum=sum*10+r;
         checkPalindrome(num/10);
    }
    return sum;
}
Write a C program to check given string is palindrome number or not.
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.

#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;
}
Write a C program to print Fibonacci series of given range.
Algorithm:
What is Fibonacci series?
Logic of Fibonacci series
Definition of Fibonacci numbers:
We assume first two Fibonacci are 0 and 1
A series of numbers in which each sequent number is sum of its two previous numbers is known as Fibonacci series and each numbers are called Fibonacci numbers. So Fibonacci numbers is
Algorithm for Fibonacci series 
Fn = Fn-2 + Fn-1
Example of Fibonacci series:
0 , 1 ,1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55  …

#include<stdio.h>
int main(){
    int k,r;
    long int i=0l,j=1,f;
 
  //Taking maximum numbers form user
    printf("Enter the number range:");
    scanf("%d",&r);
    printf("FIBONACCI SERIES: ");
    printf("%ld %ld",i,j); //printing firts two values.
    for(k=2;k<r;k++){
         f=i+j;
         i=j;
         j=f;
         printf(" %ld",j);
    }
    return 0;
}
Write a C program to get factorial of given number.

Algorithm:
Factorial value
Factorial of number is defined as:
Factorial (n) = 1*2*3 … * n
For example: Factorial of 5 = 1*2*3*4*5 = 120
Note: Factorial of zero = 1

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

C program to print hello world without using semicolon


#include<stdio.h>
void main(){
    if(printf("Hello world")){
    }
}

Solution: 2

#include<stdio.h>
void main(){
    while(!printf("Hello world")){
    }
}

Solution: 3

#include<stdio.h>
void main(){
    switch(printf("Hello world")){
    }
}

Program in C to print 1 to 100 without using loop


#include<stdio.h>
int main(){
    int num = 1;
    print(num);
    return 0;
}
int print(num){
    if(num<=100){
         printf("%d ",num);
         print(num+1);
    }
}

How to convert string to int without using library functions in C


#include<stdio.h>
int stringToInt(char[] );
int main(){
    char str[10];
    int intValue;
    printf("Enter any integer as a string: ");
    scanf("%s",str);
   intValue = stringToInt(str);
    printf("Equivalent integer value: %d",intValue);
    return 0;
}
int stringToInt(char str[]){
    int i=0,sum=0;
    while(str[i]!='\0'){
         if(str[i]< 48 || str[i] > 57){
             printf("Unable to convert it into integer.\n");
             return 0;
         }
         else{
             sum = sum*10 + (str[i] - 48);
             i++;
         }
    }
    return sum;
}

Write a C program to find out prime factor of given number


#include<stdio.h>
int main(){
  int num,i=1,j,k;
  printf("\nEnter a number:");
  scanf("%d",&num);
  while(i<=num){
      k=0;
      if(num%i==0){
         j=1;
          while(j<=i){
            if(i%j==0)
                 k++;
             j++;
          }
          if(k==2)
             printf("\n%d is a prime factor",i);
      }
      i++;
   }
   return 0;
}
Write a C program to add two numbers without using addition operator
Algorithm:
In c ~ is 1’s complement operator. This is equivalent to:
~a = -b + 1
So, a – ~b -1
= a-(-b + 1) + 1
= a + b – 1 + 1
= a + b

#include<stdio.h>
int main(){
    int a,b;
    int sum;
    printf("Enter any two integers: ");
    scanf("%d%d",&a,&b);
    //sum = a - (-b);
    sum = a - ~b -1;
    printf("Sum of two integers: %d",sum);
    return 0;
}

Write a C program to swap two numbers without using third variable


#include<stdio.h>
int main(){
    int a=5,b=10;

    //process one
    a=b+a;
    b=a-b;
    a=a-b;
    printf("a= %d  b=  %d",a,b);

    //process two
    a=5;b=10;
    a=a+b-(b=a);
    printf("\na= %d  b=  %d",a,b);

    //process three
    a=5;b=10;
    a=a^b;
    b=a^b;
    a=b^a;
    printf("\na= %d  b=  %d",a,b);

    //process four
    a=5;b=10;
    a=b-~a-1;
    b=a+~b+1;
    a=a+~b+1;
    printf("\na= %d  b=  %d",a,b);

    //process five
    a=5,b=10;
    a=b+a,b=a-b,a=a-b;
    printf("\na= %d  b=  %d",a,b);

    return 0;
}

C program for swapping of two numbers using pointers


#include<stdio.h>
int main(){
    int a,b;
    int *ptra,*ptrb;
    int *temp;
    printf("Enter any two integers: ");
    scanf("%d%d",&a,&b);
    printf("Before swapping: a = %d, b=%d",a,b);
    ptra = &a;
    ptrb = &b;
     temp = ptra;
    *ptra = *ptrb;
    *ptrb = *temp;
    printf("\nAfter swapping: a = %d, b=%d",a,b);
    return 0;
}

Write a C program to find out second largest element of an unsorted array


#include<stdio.h>
int main(){
  int a[50],size,i,j=0,big,secondbig;
  printf("Enter the size of the array: ");
  scanf("%d",&size);
  printf("Enter %d elements in to the array: ", size);
  for(i=0;i<size;i++)
      scanf("%d",&a[i]);
  big=a[0];
  for(i=1;i<size;i++){
      if(big<a[i]){
           big=a[i];
           j = i;
      }
  }
  secondbig=a[size-j-1];
  for(i=1;i<size;i++){
      if(secondbig <a[i] && j != i)
          secondbig =a[i];
  }
  printf("Second biggest: %d", secondbig);
  return 0;
}

Write a C program to find out largest element of an array

 

#include<stdio.h>
int main(){
  int a[50],size,i,big;
  printf("\nEnter the size of the array: ");
  scanf("%d",&size);
  printf("\nEnter %d elements in to the array: ”, size);
  for(i=0;i<size;i++)
      scanf("%d",&a[i]);
  big=a[0];
  for(i=1;i<size;i++){
      if(big<a[i])
           big=a[i];
  }
  printf("\nBiggest: %d",big);
  return 0;
}

Write a C program to find out L.C.M. of two numbers


#include<stdio.h>
int main(){
  int n1,n2,x,y;
  printf("\nEnter two numbers:");
  scanf("%d %d",&n1,&n2);
  x=n1,y=n2;
  while(n1!=n2){
      if(n1>n2)
           n1=n1-n2;
      else
      n2=n2-n1;
  }
  printf("L.C.M=%d",x*y/n1);
  return 0;
}
Write a C program to find out H.C.F. of two numbers

Logic for writing program:
It is clear that any number is not divisible by greater than number itself. In case of more than one numbers, a possible maximum number which can divide all of the numbers must be minimum of all of that numbers.
For example: 10, 20, and 30
Min (10, 20, 30) =10 can divide all there numbers. So we will take one for loop which will start form min of the numbers and will stop the loop when it became one, since all numbers are divisible by one. Inside for loop we will write one if conditions which will check divisibility of both the numbers.

#include<stdio.h>
int main(){
    int x,y,m,i;
    printf("Insert any two number: ");
    scanf("%d%d",&x,&y);
    if(x>y)
         m=y;
    else
         m=x;
    for(i=m;i>=1;i--){
         if(x%i==0&&y%i==0){
             printf("\nHCF of two number is : %d",i) ;
             break;
         }
    }
    return 0;
}

Write a C program to convert the string from upper case to lower case


#include<stdio.h>
#include<string.h>
int main(){
  char str[20];
  int i;
  printf("Enter any string->");
  scanf("%s",str);
  printf("The string is->%s",str);
  for(i=0;i<=strlen(str);i++){
      if(str[i]>=65&&str[i]<=90)
       str[i]=str[i]+32;
  }
  printf("\nThe string in lower case is->%s",str);
  return 0;
}

Write a C program to convert the string from lower case to upper case


#include<stdio.h>
int main(){
  char str[20];
  int i;
  printf("Enter any string->");
  scanf("%s",str);
  printf("The string is->%s",str);
  for(i=0;i<=strlen(str);i++){
            if(str[i]>=97&&str[i]<=122)
            str[i]=str[i]-32;
  }
  printf("\nThe string in lowercase is->%s",str);
  return 0;
}

Write a C program to find factorial of a number using recursion


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

int fact(int n){
   if(n==1)
       return 1;
   else
       return(n*fact(n-1));
 }

Write a C program to find out sum digits of a number using recursion


#include<stdio.h>
int main(){
  int num,x;
  clrscr();
  printf("\nEnter a number: ");
  scanf("%d",&num);
  x=findsum(num);
  printf("Sum of the digits of %d is: %d",num,x);
  return 0;
}

int r,s;
int findsum(int n){
if(n){
         r=n%10;
         s=s+r;
         findsum(n/10);
     }
     else
       return s;
}


1 comment:

  1. Sir can you please provide help regarding josh process

    ReplyDelete