C Programming Interview Questions: C is a general-purpose, structured & imperative computer programming language. It is developed by - Dennis Ritchie.
C Programming Interview Questions
1.) Write A Program in C to check whether Number is even or odd?
#include
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;
}
Output:
Enter any integer: 5
5 is an odd number.
2.) Write a Program to swap two number without using 3rd variable?
There are two common ways to swap two numbers without using third variable:
- By using + and -
- By using * and /
Method 1: Using + and -
Let's see a simple c example to swap two numbers without using the third variable.
#include
#include
main()
{
int a=10, b=20;
clrscr();
printf("Before swap a=%d b=%d",a,b);
a=a+b;//a=30 (10+20)
b=a-b;//b=10 (30-20)
a=a-b;//a=20 (30-10)
printf("\nAfter swap a=%d b=%d",a,b);
getch();
}
Output:
Before swap a=10 b=20
After swap a=20 b=10
Method 2: Using * and /
Let's see another example to swap two numbers using * and /.
#include
#include
main()
{
int a=10, b=20;
clrscr();
printf("Before swap a=%d b=%d",a,b);
a=a*b;//a=200 (10*20)
b=a/b;//b=10 (200/20)
a=a/b;//a=20 (200/10)
printf("\nAfter swap a=%d b=%d",a,b);
getch();
}
Output:
Before swap a=10 b=20
After swap a=20 b=10
3) Write a program to print "Full Stack Tutorials" without using semicolon?
Method: 1
void main(){
if(printf("Full Stack Tutorials")){
}
}
Method: 2
void main(){
while(!printf("Full Stack Tutorials")){
}
}
Method: 3
void main(){
switch(printf("Full Stack Tutorials")){
}
}
4.) Write a C program to display fibonacci series without using recursion?
Fibonacci Series: In case of fibonacci series, next number is the sum of previous two numbers.
for example 0, 1, 1, 2, 3, 5, 8, 13, 21 etc. The first two numbers of fibonacci series are 0 and 1.
There are two ways to write the fibonacci series program:- Fibonacci Series without recursion
- Fibonacci Series using recursion
Write a C program to display fibonacci series using recursion?
Fibonacci Series using recursion in C
Let's see the fibonacci series program in c using recursion.
#include
#include
void printFibonacci(int n){
static int n1=0,n2=1,n3;
if(n>0){
n3 = n1 + n2;
n1 = n2;
n2 = n3;
printf("%d ",n3);
printFibonacci(n-1);
}
}
void main(){
int n;
clrscr();
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("Fibonacci Series: ");
printf("%d %d ",0,1);
printFibonacci(n-2);//n-2 because 2 numbers are already printed
getch();
}
Output:
Enter the number of elements:15
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
5.) Write a C program to check whether a number is prime or not ?
#include
#include
void main()
{
int n,i,m=0,flag=0;
clrscr();
printf("Enter the number to check prime:");
scanf("%d",&n);
m=n/2;
for(i=2;i<=m;i++)
{
if(n%i==0)
{
printf("Number is not prime");
flag=1;
break;
}
}
if(flag==0)
printf("Number is prime");
getch();
}
Output:
Enter the number to check prime:56
Number is not prime
Enter the number to check prime:23
Number is prime
You may also like C Language Interview Questions and Answers
6.) Write a C program to check whether a number is a palindrome or not?
A palindrome number is a number such that if we reverse it, it will not change.
The first 15 palindromic numbers (in decimal) are: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55,...
#include
#include
main()
{
int n,r,sum=0,temp;
clrscr();
printf("enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
printf("palindrome number ");
else
printf("not palindrome");
getch();
}
Output:
enter the number=151
palindrome number
enter the number=5621
not palindrome number
7.) Write a C program to print factorial of given number without using recursion?
Methods:
Factorial Program using loop
Factorial Program using recursion
Factorial Program using the loop
Let's see the factorial Program using the loop.
#include
#include
void main(){
int i,fact=1,number;
clrscr();
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=number;i++){
fact=fact*i;
}
printf("Factorial of %d is: %d",number,fact);
getch();
}
Output:
Enter a number: 5
Factorial of 5 is: 120
Factorial Program using recursion in C
Let's see the factorial program in c using recursion.
#include
#include
long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
void main()
{
int number;
long fact;
clrscr();
printf("Enter a number: ");
scanf("%d", &number);
fact = factorial(number);
printf("Factorial of %d is %ld\n", number, fact);
getch();
}
Output:
Enter a number: 6
Factorial of 5 is: 720
8.)Write a C Program to reverse the given number?
#include
#include
main()
{
int n, reverse=0, rem;
clrscr();
printf("Enter a number: ");
scanf("%d", &n);
while(n!=0)
{
rem=n%10;
reverse=reverse*10+rem;
n/=10;
}
printf("Reversed Number: %d",reverse);
getch();
}
Output:
Enter a number: 123
Reversed Number: 321
9.)Write a C program which produces its own source code as its output?
#include
int main(){
FILE *fp;
char c;
fp = fopen(__FILE__,"r");
do{
c= getc(fp);
putchar(c);
}
while(c!=EOF);
fclose(fp);
return 0;
}
Output:
#include
int main(){
FILE *fp;
char c;
fp = fopen(__FILE__,"r");
do{
c= getc(fp);
putchar(c);
}
while(c!=EOF);
fclose(fp);
return 0;
}