TOP 15 IMPORTANT PROGRAMS FOR INTERVIEWS WITH SOLUTION

0
7411

Top 15 programs list for MNC interviews:

1.Prime Number Program:

Prime Number: A number that is greater than 1 & divided by 1 or itself

  1. #include<stdio.h>
  2. int main(){
  3. int n,i,m=0,flag=0;
  4. printf(“Enter the number to check prime:”);
  5. scanf(“%d”,&n);
  6. m=n/2;
  7. for(i=2;i<=m;i++)
  8. {
  9. if(n%i==0)
  10. {
  11. printf(“Number is not prime”);
  12. flag=1;
  13. break;
  14. }
  15. }
  16. if(flag==0)
  17. printf(“Number is prime”);
  18. return 0;
  19.  }

Output:

Enter the number to check prime:56
Number is not prime

Enter the number to check prime:23
Number is prime

 

2. Fibonacci Series:

Fibonacci: When next number is the sum of given 2 number then the series is called Fibonacci Series.

Example: 0, 1, 1, 2, 3, 5, 8….

  1. #include<stdio.h>  
  2. int main()
  3. {
  4.  int n1=0,n2=1,n3,i,number;
  5.  printf(“Enter the number of elements:”);
  6.  scanf(“%d”,&number);
  7.  printf(“\n%d %d”,n1,n2);//printing 0 and 1  
  8.  for(i=2;i<number;++i)//loop starts from 2 because 0 and 1 are already printed  
  9.  {
  10.   n3=n1+n2;
  11.   printf(” %d”,n3);
  12.   n1=n2;
  13.   n2=n3;
  14.  }
  15.   return 0;
  16.  }

The above program is Fibonacci Series without Recursion

  1. #include<stdio.h>  
  2. void printFibonacci(int n){
  3.     static int n1=0,n2=1,n3;
  4.     if(n>0){
  5.          n3 = n1 + n2;
  6.          n1 = n2;
  7.          n2 = n3;
  8.          printf(“%d “,n3);
  9.          printFibonacci(n-1);
  10.     }
  11. }
  12. int main(){
  13.     int n;
  14.     printf(“Enter the number of elements: “);
  15.     scanf(“%d”,&n);
  16.     printf(“Fibonacci Series: “);
  17.     printf(“%d %d “,0,1);
  18.     printFibonacci(n-2);//n-2 because 2 numbers are already printed  
  19.   return 0;
  20.  }    ‘

The above Program is Fibonacci Series with Recursion

3.Factorial Number Program

Factorial: Factorial is represented as “5!” that means, 5*4*3*2*1

  1. #include<stdio.h>
  2. int main()
  3. {
  4.  int i,fact=1,number;
  5.  printf(“Enter a number: “);
  6.   scanf(“%d”,&number);
  7.     for(i=1;i<=number;i++){
  8.       fact=fact*i;
  9.   }
  10.   printf(“Factorial of %d is: %d”,number,fact);
  11. return 0;
  12. }

4. Pattern Program:

  1. #include <stdio.h>
  2. #include <conio.h>
  3. void main()
  4. {
  5.     int i, j, rows, k = 0;
  6.     printf (” Enter a number to define the rows: \n”);
  7.     scanf (“%d”, &rows);
  8.     for ( i =1; i <= rows; i++)
  9.     {
  10.         for ( j = 1; j <= rows – i; j++)
  11.         {
  12.             printf (”  “);
  13.         }
  14.         // use for loop where k is less than equal to (2 * i -1)
  15.         for ( k = 1; k <= ( 2 * i – 1); k++)
  16.         {
  17.             printf (“* “); // print the Star
  18.         }
  19.         printf (“\n”);
  20.     }
  21.     getch();
  22. }

Output:

*

*         *

*      *        *

5. Sum of Digits in a Program:

  1. #include<stdio.h>
  2.  int main()
  3. {
  4. int n,sum=0,m;
  5. printf(“Enter a number:”);
  6. scanf(“%d”,&n);
  7. while(n>0)
  8. {
  9. m=n%10;
  10. sum=sum+m;
  11. n=n/10;
  12. }
  13. printf(“Sum is=%d”,sum);
  14. return 0;
  15. }

Output:

Enter a number:654

Sum is=15

Enter a number:123

Sum is=6

6. Given Number is Palindrome Number or not:

Palindrome: Palindrome is a number that is same even after reverse.

Example: 121, 34543

  1. #include<stdio.h>
  2. int main()
  3. {
  4. int n,r,sum=0,temp;
  5. printf(“enter the number=”);
  6. scanf(“%d”,&n);
  7. temp=n;
  8. while(n>0)
  9. {
  10. r=n%10;
  11. sum=(sum*10)+r;
  12. n=n/10;
  13. }
  14. if(temp==sum)
  15. printf(“palindrome number “);
  16. else
  17. printf(“not palindrome”);
  18. return 0;
  19. }

Output: 

Given Number: 151

The Number is Palindrome

7. Given Number is Leap Year is not?

#include <stdio.h>

int main()
{    
    int year;      
    printf("Enter a year to check if it is a leap year\n");
    scanf("%d", &year);
          
    if ( year%400 == 0)      
    printf("%d is a leap year.\n", year);  
        
    else if ( year%100 == 0)      
    printf("%d is not a leap year.\n", year); 
         
    else if ( year%4 == 0 )      
    printf("%d is a leap year.\n", year);
    
    else      
    printf("%d is not a leap year.\n", year);      
    
    return 0;    
}

Output of program:

Enter a year to check it is a leap year or not

2012

2012 is not a leap year

8. Given Number is Armstrong Number or Not?

Armstrong Number: It is a number that is sum of cubes of its number.

For example take a number: 371

Logic:

  1. 371 = (3*3*3)+(7*7*7)+(1*1*1)
  2. where:
  3. (3*3*3)=27
  4. (7*7*7)=343
  5. (1*1*1)=1
  6. So:
  7. 27+343+1=371

Program:

  1. #include<stdio.h>
  2.  int main()
  3. {
  4. int n,r,sum=0,temp;
  5. printf(“enter the number=”);
  6. scanf(“%d”,&n);
  7. temp=n;
  8. while(n>0)
  9. {
  10. r=n%10;
  11. sum=sum+(r*r*r);
  12. n=n/10;
  13. }
  14. if(temp==sum)
  15. printf(“armstrong  number “);
  16. else
  17. printf(“not armstrong number”);
  18. return 0;
  19. }

9. Given Number is Strong or Not:

Strong Number: If sum of the factorial of the individual digits is equal to the number.

Program:

  1. #include <stdio.h>
  2. int main()
  3. {
  4.     int n;
  5.     int sum=0;
  6.     printf(“Enter a number”);
  7.     scanf(“%d”,&n);
  8.     int k=n;
  9.     int r;
  10.     while(k!=0)
  11.     {
  12.         r=k%10;
  13.         int f=fact(r);
  14.         k=k/10;
  15.         sum=sum+f;
  16.     }
  17.     if(sum==n)
  18.     {
  19.         printf(“\nNumber is a strong”);
  20.     }
  21.     else
  22.     {
  23.         printf(“\nNumber is not a strong”);
  24.     }
  25.     return 0;
  26. }
  27. int fact(int r)
  28. {
  29.     int mul=1;
  30.     for(int i=1;i<=r;i++)
  31.     {
  32.         mul=mul*i;
  33.     }
  34.     return mul;
  35. }

10. Given Number is Perfect Number or not?

Perfect Number: 

  1. #include<stdio.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. int i = 1, num, Sum = 0;
  6. printf(” Enter any number to check Perfect Number \n”);
  7. scanf(“%d”, &num);
  8. while(i < num )
  9.                      {
  10.                                if(num % i == 0)
  11.                                Sum = Sum + i;
  12.                                i++;
  13.                      }
  14.            if(Sum == num)
  15.                   printf(“\n %d is Perfect Number”, num);
  16.            else
  17.            printf(“\n %d is not a Perfect Number”, num);
  18. getch();
  19. }

11. Swapping of 2 numbers without using the 3rd Number:

  1. #include<stdio.h>
  2.  int main()
  3. {
  4. int a=10, b=20;
  5. printf(“Before swap a=%d b=%d”,a,b);
  6. a=a+b;//a=30 (10+20)  
  7. b=a-b;//b=10 (30-20)  
  8. a=a-b;//a=20 (30-10)  
  9. printf(“\nAfter swap a=%d b=%d”,a,b);
  10. return 0;
  11. }

12. Harshad Number:

If a number is divisible by the sum of its digits then it will be known as a Harshad Number.

For example:

156 is divisible by the sum (12) of its digits (1, 5, 6 ).

  1. #include <stdio.h>  
  2. int main()
  3. {
  4.     int num = 156;
  5.     int rem = 0, sum = 0, n;
  6.     //Make a copy of num and store it in variable n  
  7.     n = num;
  8.     //Calculates sum of digits  
  9.     while(num > 0){
  10.         rem = num%10;
  11.         sum = sum + rem;
  12.         num = num/10;
  13.     }
  14.     //Checks whether number is divisible by sum of digits  
  15.     if(n%sum == 0)
  16.         printf(“%d is a harshad number”, n);
  17.     else
  18.         printf(“%d is not a harshad number”, n);
  19.     return 0;
  20. }

13. Program to check whether a given number is EVEN or ODD

#include <stdio.h>
int main() {
    int num;
    printf("Enter an integer: ");
    scanf("%d", &num);

    // true if num is perfectly divisible by 2
    if(num % 2 == 0)
        printf("%d is even.", num);
    else
        printf("%d is odd.", num);
    
    return 0;
}

 14. Reversing of Numbers:

  1. #include<stdio.h>
  2.  int main()
  3. {
  4. int n, reverse=0, rem;
  5. printf(“Enter a number: “);
  6.   scanf(“%d”, &n);
  7.   while(n!=0)
  8.   {
  9.      rem=n%10;
  10.      reverse=reverse*10+rem;
  11.      n/=10;
  12.   }
  13.   printf(“Reversed Number: %d”,reverse);
  14. return 0;
  15. }

15. Program to convert Decimal to Binary:

  1. #include<stdio.h>  
  2. #include<stdlib.h>
  3. int main(){
  4. int a[10],n,i;
  5. system (“cls”);
  6. printf(“Enter the number to convert: “);
  7. scanf(“%d”,&n);
  8. for(i=0;n>0;i++)
  9. {
  10. a[i]=n%2;
  11. n=n/2;
  12. }
  13. printf(“\nBinary of Given Number is=”);
  14. for(i=i-1;i>=0;i–)
  15. {
  16. printf(“%d”,a[i]);
  17. }
  18. return 0;
  19. }

Output:

Enter the number to convert: 5

Binary of Given Number is=101

This program can be written in C/JAVA/PYTHON. We have provided

in C Language

LEAVE A REPLY

Please enter your comment!
Please enter your name here