Table Of Contents

- 1 C Programs that are Asked in Interviews & Exams in 2018
- 1.1 Print Pyramid using C language
- 1.2 Multiplication of Two Matrices
- 1.3 Factorial of two numbers
- 1.4 Check for Prime numbers
- 1.5 Program to find the area of a rectangle
- 1.6 C program for EVEN or ODD
- 1.7 Use C language to swap two numbers with four different methods
- 1.8 Solution of this series
- 1.9 Write a program to reverse a number
- 1.10 Calculator using C (Without Output)

C language is the language designed by Dennis Ritchie which is treated as the base of every computer language. In order to learn programming, learning c programs are the best to start with. Also, they are usually asked in interviews to scrutinize the candidate. As far as interviews are concerned, they are better termed as the procedure of rejection than a procedure of selection. Thus, to crack interviews look at our list of C programs that are frequently asked in Interviews and exams in 2018.

**Share This Post **

## C Programs that are Asked in Interviews & Exams in 2018

There is no hard and fast rule to crack an interview, even the most intelligent fail in interviews. Success in the interviews basically depends on hard-work and luck. However, you only have hard-work in your hands and nothing else. It is highly recommended to prepare yourself starting from the base because usually, the interviewer tries to ask basic questions and many times candidates fail to answer these questions. This is because when they prepare for an interview, they prepare only the difficult part and leave the easy part. C programs and C language are the easy part but don’t leave this part because they are very frequently asked in the interviews and exams 2018.

Check out our list of C programs that are frequently asked in Interviews or competitive exams. See how many you know and can write yourself. It is recommended, you should prepare these c programs as they are frequently asked in several interviews, written tests and competitive exams. Go ahead with your preparation>

### Print Pyramid using C language

Pattern programs are usually asked in interviews and making a pyramid of numbers is generally asked. The pyramid of numbers as we illustrated below is symmetrical and follows a logic. The trick for solving these type of programs is to first find the logic used and then accordingly use for and while loops. Just look at how we have solved this problem.

Print Pyramid using C language. 1 2 3 2 3 4 5 4 3 4 5 6 7 6 5 4 5 6 7 8 9 8 7 6 5

**Source Code**

#include <stdio.h> #include <conio.h> int main() { int i, j, r, k=0, count1= 0, count2= 0; printf("Enter number of rows: "); scanf("%d",&r); for(i=1; i<=r; ++i) { for(j=1; j<=r-i; ++j) { printf(" "); ++count1; } while(k!=2*i-1) { if (count1<=r-1) { printf("%d ",i+k); ++count1; } else { ++count2; printf("%d ", (i+k-2*count2)); } ++k; } count2 = count1 = k = 0; printf("\n"); } return 0;

### Multiplication of Two Matrices

We have illustrated a C program in which the input matrix which is to be multiplied is entered by the user himself. For Matrix multiplication, we have to follow these steps.

- Ask the user to input the rows and column of the first and second matrix.
- Check the number of columns in the first matrix is equal to the number of rows in the second matrix. If this rule is satisfied then the matrix multiplication is possible otherwise matrix multiplication is not possible (display error message).
- Ask the user to enter the elements of the first and second matrix.
- Use for loop to multiply the row elements of the first matrix by the column of the second matrix.
- Now, Add the results within the for loop to generate an output matrix which is the product of first and second matrix.
- Display the output matrix.

#include <stdio.h> #include <conio.h> int main() { int a[10][10], b[10][10], result[10][10], r1, c1, r2, c2, i, j, k; printf("Enter rows and column of first matrix: "); scanf("%d %d", &r1, &c1); printf("Enter rows and column of second matrix: "); scanf("%d %d",&r2, &c2); // Rule - Column of first matrix should be equal to rows of second matrix while (c1 != r2) { printf("Error! column of first matrix not equal to row of second.\n\n"); printf("Enter rows and column of first matrix: "); scanf("%d %d", &r1, &c1); printf("Enter rows and column of second matrix: "); scanf("%d %d",&r2, &c2); } // Elements of first matrix a. printf("\nEnter elements of first matrix:\n"); for(i=0; i<r1; ++i) for(j=0; j<c1; ++j) { printf("Enter elements a%d%d: ",i+1, j+1); scanf("%d",&a[i][j]); } // Elements of second matrix b. printf("\nEnter elements of second matrix:\n"); for(i=0; i<r2; ++i) for(j=0; j<c2; ++j) { printf("Enter elements b%d%d: ",i+1, j+1); scanf("%d",&b[i][j]); } // Initializing all elements of result matrix to 0 for(i=0; i<r1; ++i) for(j=0; j<c2; ++j) { result[i][j] = 0; } // Multiply matrices a and b for(i=0; i<r1; ++i) for(j=0; j<c2; ++j) for(k=0; k<c1; ++k) { result[i][j]+=a[i][k]*b[k][j]; } // Displaying the result printf("\nResulting Matrix After Multiplication:\n"); for(i=0; i<r1; ++i) for(j=0; j<c2; ++j) { printf("%d", result[i][j]); if(j==c2-1) printf("\n\n"); } return 0; }

**Output**

Enter rows and column of first matrix: 3 2 Enter rows and column of second matrix: 3 2 Error! column of first matrix not equal to row of second. Enter rows and column of first matrix: 2 3 Enter rows and column of second matrix: 3 2 Enter elements of first matrix: Enter elements a11: 2 Enter elements a12: 5 Enter elements a13: 1 Enter elements a21: 3 Enter elements a22: 0 Enter elements a23: 3 Enter elements of second matrix: Enter elements b11: 5 Enter elements b12: 2 Enter elements b21: 1 Enter elements b22: 0 Enter elements b31: 2 Enter elements b32: 2 Resulting Matrix After Multiplication: 21 12 18 6

### Factorial of two numbers

**Factorial** in mathematics is the product of all non-negative integers less than or equal to n. It is denoted by n! for a positive integer n. The rule is the factorial of a negative number doesn’t exist and the factorial of 0 is 1.

n! = n*(n-1)*(n-2)*(n-3)…3.2.1

0! = 1.

For example – 5! = 5*4*3*2*1 = 120

#include <stdio.h> int main() { int n, i; unsigned long factorial = 1; printf("Enter an integer: "); scanf("%d",&n); // Rule - Factorial of negative number doesn't exist if (n < 0) printf("Error! Factorial rule violation."); else { for(i=1; i<=n; ++i) { factorial *= i; // factorial = factorial*i; } printf("Factorial of %d = %lu", n, factorial); } return 0; }

**Output**

Enter an integer: 5 Factorial of 5 = 120

### Check for Prime numbers

A prime number is a natural number (greater than 1) which has only two factors 1 and the number itself. Prime numbers till 200 are: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199.

#include <stdio.h> #include <math.h> int checkPrimeNumber(int n); int main() { int n, flag; printf("Enter a positive number: "); scanf("%d", &n); // Check for prime number flag = checkPrimeNumber(n); if (flag == 1) printf("%d is a prime number.\n", n); else printf("%d is not a prime number.\n", n); return 0; } int checkPrimeNumber(int n) { int i, flag = 1; for(i=2; i<=n/2; ++i) { // condition for non-prime number if(n%i == 0) { flag = 0; break; } } return flag; }

**Output**

Enter a positive number: 11 11 is a prime number.

### Program to find the area of a rectangle

Area of a rectangle is the multiplication of length and its breadth. In C programs, the thing to remember is length, width and area are in the float as recommended.

Area = Length * Width, where Area is measured in sq. units.

#include <stdio.h> int main() { float length, width, area; /* Input length and width of rectangle */ printf("Enter length of rectangle: "); scanf("%f", &length); printf("Enter width of rectangle: "); scanf("%f", &width); /* Calculate area of rectangle */ area = length * width; /* Print area of rectangle */ printf("Area of rectangle = %f sq. units ", area); return 0; }

**Output**

Enter length of rectangle: 5 Enter width of rectangle : 10 Area of rectangle = 50.00000 sq. units

**C program for EVEN or ODD**

Numbers which are divisible by 2 are termed as **EVEN numbers** and which are not divisible by 2 are termed as **ODD numbers. **For example, even numbers are: 0,2,4,6,8,10,12,14,16,18,20, etc. Odd numbers are: 1,3,5,7,9,11,13,15,17,19, etc.

** **

#include <stdio.h> int main() { int num; printf("Enter an integer number: "); scanf("%d",&num); /*If number is divisible by 2 then number is EVEN otherwise number is ODD*/ if(num%2==0) printf("%d is an EVEN number.",num); else printf("%d is an ODD number.",num); return 0;

**Output**

Enter an integer number: 121 121 is an ODD number. Enter an integer number: 100 100 an EVEN number.

### Use C language to swap two numbers with four different methods

Swapping two numbers means interchanging of two numbers with each other. For example, for two variables a and b, suppose a=3, b=4. After swapping we get, a=4, b=3. We can perform the swapping of numbers with each other in different ways or methods.

#include <stdio.h> int main() { int a,b,t; printf(" Enter value of A ? "); scanf("%d",&a); printf(" Enter value of B ? "); scanf("%d",&b); printf("\n Before swapping : A= %d, B= %d",a,b); /*1 using third variable*/ t=a; a=b; b=t; printf("\n After swapping (First method) : A= %d, B= %d\n",a,b); /*2 without using third variable*/ a=a+b; b=a-b; a=a-b; printf("\n After swapping (second method): A= %d, B= %d\n",a,b); /*3 using X-Or */ a^=b; b^=a; a^=b; printf("\n After swapping (third method) : A= %d, B= %d\n",a,b); /*4 method (single statement)*/ a=a+b-(b=a); printf("\n After swapping (fourth method): A= %d, B= %d\n",a,b); return 0; }

**Output**

Enter value of A ? 50 Enter value of B ? 60 Before swapping : A= 50 B= 60 After swapping (1 method) : A= 60, B= 50 After swapping (2 method): A= 50, B= 60 After swapping (3 method) : A= 60, B= 50 After swapping (4 method): A= 50, B= 60

**Solution of this series **

**Solution of series n^2 + (n-1)^2 + ……… + 2 + 1 using c language**

When n=10; Series will be

10^2 + 9^2 + 8^2 + 7^2 + 6^2 + 5^2 + 4^2 + 3^2 + 2 +1

= 100+81+64+49+36+25+16+9+2+1

= 383

#include <stdio.h> #include <math.h> int main() { int n=10; long int sum =0; int loop; //loop will run till 3 for( loop=n; loop>2; loop--) sum += pow(loop,2); //adding 2 and 1 sum += (2+1); //print the sum printf("sum of the series = %ld\n",sum); return 0; }

**OUTPUT**

sum of the series = 383

**Write a program to reverse a number**

Here, we have illustrated an example to reverse a number where n is the original number, r is the remainder and t is the reversed number.

Obtain remainder, remainder = number mod 10

reversed number = remainder + reverse multiplied by 10

Repeat this process till quotient becomes zero.

#include<stdio.h> #include<conio.h> void main() { long n, r, t = 0 ; printf("Enter a positive number : ") ; scanf("%ld", &n) ; // reverse the number while(n > 0) { r = n % 10 ; t = r + t * 10 ; n = n / 10 ; } // Print the reversed number printf("\nThe reversed number is : %ld", t) ; getch() ; }

**OUTPUT**

Enter a positive number : 4562 The Reversed Number is : 2654

**Calculator using C (Without Output)**

#include <stdio.h> #include <math.h> int main() { int a,b,choice; while(1) { printf("-----------------\n MENU\n-----------------\n"); printf("1: Addition\n"); printf("2: Subtraction\n"); printf("3: Multiplication\n"); printf("4: Division\n"); printf("5: Power\n"); printf("6: Exit\n"); printf("Please select your option: "); scanf("%d",&choice); switch(choice) { case 1: printf("-----------------\n Addition\n-----------------\n"); printf("Please give input<e.g.15+12>: "); scanf("%d+%d",&a,&b); printf("The Summation of %d and %d is: %d\n",a,b,(a+b)); break; case 2: printf("-----------------\n Subtraction\n-----------------\n"); printf("Please give input<e.g.15-12>: "); scanf("%d-%d",&a,&b); printf("The subtraction of %d and %d is: %d\n",a,b,(a-b)); break; case 3: printf("-----------------\n Multiplication\n-----------------\n"); printf("Please give input<e.g.5*2>: "); scanf("%d*%d",&a,&b); printf("The multiplication of %d and %d is: %d\n",a,b,(a*b)); break; case 4: printf("-----------------\n Dvision\n----------------\n"); printf("Please give input<e.g.6 2="">: "); scanf("%d/%d",&a,&b); printf("The Division of %d and %d is: %d\n",a,b,(a/b)); break; case 5: printf("-----------------\n Power\n-----------------\n"); printf("please Give input<e.g.5^2>: "); scanf("%d^%d",&a,&b); printf("The Power of %d and %d is: %d\n",a,b,pow(a,b)); break; case 6: printf("Press any key to continue..."); getchar(); exit(1); default: printf("Invalid option\n"); break; } } return 0; }

We hope that you this post will definitely help you in your interviews and exams. For getting updates of more post like this, follow us on facebook and twitter. Also, Don’t forget to like and comment below.

You may also like>

Online Grammar Check {FREE} – Top 10 Software For Proof-Reading Contents

How to Share Screen Online for Business Meetings, Conferences & Online Teaching

This is the right webpage for everyone who would like to find out about this topic.

You definitely put a fresh spin on a topic that has been discussed for ages.

Wonderful stuff, just wonderful!

What’s up to all, it’s really a pleasant for me to go to

see this site, it consists of useful Information.

Great goods from you, man. I’ve understand your stuff previous to and you

are just too wonderful. I actually like what you’ve acquired right here, certainly like what you are stating and the best way by which you are saying it.

You make it entertaining and you continue to take care of to

stay it sensible. I can’t wait to learn much more from you.

This is actually a tremendous website.

Hi! I just want to offer you a big thumbs up for the excellent info you have

right here on this post. I will be coming back to your website

for more soon.