Nothing Special   »   [go: up one dir, main page]

C Excersice Programs PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 118

WWW.FOURSTEPSOLUTIONS.

COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Hello World Program


Below is a simple program printing Hello World in C language.
printf() is a system defined function under the header file stdio.h, used to print
data onto the screen
\n is used to move the control onto the next line
\t is used to give a horizontal tab i.e. continuous five spaces
#include <stdio.h>

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int num;
printf("\nHello world!\nWelcome to Studytonight: Best place to learn\n");
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}

Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Program to take input of various


datatypes in C
Below is a program to explain how to take input from user for different datatypes
available in C language. The different datatypes are int(integer
values), float(decimal values) and char(character values).
Here is the C language tutorial explaining various datatypes → Datatypes in C
printf() is used to display text onto the screen
& is used to assign the input value to the variable and store it at that particular
location.
scanf() is uded to take input from the user using format specifier discussed in
upcoming tutorials
%d and %i, both are used to take numbers as input from the user.

%f is the format specifier to take float as input from the user


%s is the format specifier to take character as input from the user
#include<stdio.h>

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

int num1, num2;


float fraction;
char character;

printf("Enter two numbers number\n");

// Taking integer as input from user


scanf("%d%i", &num1, &num2);
printf("\n\nThe two numbers You have entered are %d and %i\n\n", num1, num2);

// Taking float or fraction as input from the user


printf("\n\nEnter a Decimal number\n");
scanf("%f", &fraction);
printf("\n\nThe float or fraction that you have entered is %f", fraction);

// Taking Character as input from the user


WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

printf("\n\nEnter a Character\n");
scanf("%c",&character);
printf("\n\nThe character that you have entered is %c", character);

printf("\n\n\t\t\tCoding is Fun !\n\n\n");

return 0;
}

Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

ASCII value of Character


Below is a program to find ASCII value of any input character.
%c is the format specifier to take character as input
#include<stdio.h>

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

char c;
printf("Enter a character : ");
scanf("%c" , &c);
printf("\n\nASCII value of %c = %d",c,c);

printf("\n\n\t\t\tCoding is Fun !\n\n\n");


return 0;
}

Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

How to use gets() function


Some of the important points about scanf() and gets() are:

 scanf() and gets() both are used to take input from the user.

 scanf() can only take input until it encounters a space. The words after space are ignored by

it.

 gets() is used to take a single input at a time but can be used to input a complete sentence

with spaces unlike scanf().

Below is a program on use of gets().


gets() takes only a single line at a time i.e all the words before hitting \n(enter key).
#include<stdio.h>

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

char str[50]; // char array of size 50


printf("Enter your complete name:\n\n\n");

gets(str);
printf("\n\nWelcome to Studytonight %s\n\n\n", str);
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}

Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Basic ifelse condition program


Below is a program on if-else.
Here is the C language tutorial explaining If Else → If Else in C
#include<stdio.h>

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

int number;
printf("Please enter a number:\n");
scanf("%d",&number);
/*
For single statements we can skip the curly brackets
*/
if(number < 100)
printf("Number is less than 100!\n");
else if(number == 100)
printf("Number is 100!\n");
else
printf("Number is greater than 100!\n");

printf("\n\n\t\t\tCoding is Fun !\n\n\n");

return 0;
}

Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Swich Case with break


Below is a program on switch case with break.
switch() can only contain char and int.
break is used to exit from switch statement.
switch case can be without default case.
Another piece of information here is that a char variable is always initialized
within ''(single quotes).
Here is the C language tutorial explaining Switch Case → Switch Case in C
#include<stdio.h>

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

// Local Variable Definition


char grade;
printf("Enter your grade:\n");
scanf("%c", &grade);

switch(grade)
{
case 'A':
printf("Excellent\n");
break;
case 'B':
printf("Keep it up!\n\n");
break;
case 'C':
printf("Well done\nbreak keyword takes execution to exit the switch
case\n\n");
break;
case 'D':
printf("You passed\n");
break;
case 'F':
printf("Better luck next time\n");
break;
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

default:
printf("Invalid grade\n");
}
printf("Your grade is %c\n",grade);
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}

Output:

Swich Case without break


Below is a program on switch case without break.
If there is no break statement then the cases following the matched case other
than default will get executed.
#include<stdio.h>

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

/* Local Variable Definition */


char grade;
printf("Enter your grade:\n");
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

scanf("%c", &grade);

switch(grade)
{
case 'A':
printf("Excellent\n");
case 'B':
printf("\n\n\nKeep it up!\n\nNo break statement\n\nHence all the case
following this(but not the ones above this) except the default case will get
executed !\n\n");
case 'C':
printf("\n\n\t\tCase C : Well done !\n\n");
case 'D':
printf("\t\tCase D : You passed!\n\n");
case 'F':
printf("\t\tCase E : Better luck next time\n\n\n");
default:
printf("\t\tDefault Case : Invalid grade\n\n\n");
}
printf("Your grade is %c\n",grade);
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}

Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Program to check if input character


is a vowel using Switch Case
Below is a program to check vowel using switch case.
#include<stdio.h>

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

char ch;
printf("Input a Character : ");
scanf("%c", &ch);

switch(ch)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
printf("\n\n%c is a vowel.\n\n", ch);
break;
default:
printf("%c is not a vowel.\n\n", ch);
}
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Output:

Explanation:
 If break statement is not used for a case then all the cases following the valid

case are executed and evaluated. This way you can make your code easier

to understand by writing only breakstatement only once to check multiple

conditions in one go.

 default is executed only if none of the above cases are true. It is similar to

the else statement of the if-else code.


WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Program to reverse the case of


input character
Below is a program to reverse the case of input character.
getchar() is similar to scanf().
islower() is system defined function under ctype.h header file to check if the
character is in lowercase or not.
toupper() converts the input parameter into equivalent uppercase char.
putchar() is similar to printf().
#include<stdio.h>
#include<ctype.h> // to use system defined function islower & toupper

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

char alphabet;
printf("Enter an alphabet : ");
putchar('\n'); // to move to next Line

alphabet=getchar();

printf("\n\nReverse case of %c is : ",alphabet);

if(islower(alphabet))
putchar(toupper(alphabet));

else
// must be an uppercase character
printf("%c",tolower(alphabet)) ;

printf("\n\n\t\t\tCoding is Fun !\n\n\n");


return 0;
}
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Swapping two Numbers using a


Temporary Variable
Below is a program to swap two numbers using temporary variable.
#include<stdio.h>
#include<conio.h>

void main()
{
int x = 10, y = 15, temp;
temp = x;
x = y;
y = temp;
printf("x = %d and y = %d", x, y);
getch();
}

x = 15 and y = 10

Swapping tow Numbers without


using a Temporary Variable
Below is a program to swap two numbers without using any temporary variable.
#include<stdio.h>
#include<conio.h>

void main()
{
int x = 10, y = 15;
x = x + y - (y = x);
printf("x = %d and y = %d",x,y);
getch();
}

x = 15 and y = 10
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Swapping two Numbers using


Bitwise Operator
Below is a program to swap two numbers using bitwise operator.
#include<stdio.h>
#include<conio.h>

void main()
{
int x = 6, y = 4;
x = x^y;
y = x^y;
x = x^y;
printf("x = %d and y = %d", x, y);
getch();
}

x = 4 and y = 6

Swapping two Numbers using


Multiplication and Division
Below is a program to swap two numbers using multiplication and division.
#include<stdio.h>
#include<conio.h>

void main()
{
int x = 6, y = 4;
x = x*y;
y = x/y;
x = x/y;
printf("x = %d and y = %d", x, y);
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

getch();
}

x = 4 and y = 6

Program to print the Largest and


Smallest using Global Declaration
Some important points about Global variable declaration are:

 It can be done anywhere within the program.

 Unlike local variables that can be used within the scope of a particular function.

 & is used to assign the input value to the variable and store it at that particular location.

%0nd is used to represent numbers in n digit format with leading 0's.


Below is a program to find largest and smallest value using global declaration.
#include<stdio.h>

int a,b;
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
printf("\n\nEnter the two values to find the greatest and smallest number:
\n");
scanf("%d%d", &a, &b);

if(a == b)
printf("Both are equal\n");

else if(a < b)


{
printf("\n\nThe largest number is %03d\n", b);
printf("\nThe smallest number is %03d\n", a);
printf("\nThe largest number is %03d\n", b);
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

}
else //Only possibility remaining
{
printf("The largest number is %03d\n", a);
printf("The smallest number is %03d\n", b);
}
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}

Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Basic for Loop Program


Every loop consists of three parts in a sequence

1. Initialization: Use to initialize the loop variable.

2. Condition: It is checked after each iteration as an entry point to the loop.

3. Updation: Incrementing the loop variable to eventually terminate the loop not

satisfying the loop condition.

Remember that the loop condition checks the conditional statement before it
loops again.
Syntax:
for(initialization, condition, incrementation)
{
code statements;
}

Below is a simple program on for loop.


Here is the C language tutorial explaining for Loop → For Loop in C
#include<stdio.h>

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

/*
Always declare the variables before using them
*/
int i = 0; // declaration and initialization at the same time

for(i = 0; i < 10; i++)


{
printf("i = %d\n", i);

/*
consequently, when i equals 10, the loop breaks.
i is updated before the condition is checked-
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

hence the value of i after exiting the loop is 10


*/
}

printf("\n\The value of i after exiting the loop is %d\n\n", i);

printf("\nRemember that the loop condition checks the conditional statement


before it loops again.\n\n");

printf("Consequently, when i equals 10, the loop breaks.\n\n");

printf("i is updated before the condition is checked- hence the value of i


after exiting the loop is 10 .\n\n");

printf("\n\n\t\t\tCoding is Fun !\n\n\n");


return 0;
}

Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Simple while Loop Program


Every loop consists of three parts in sequence

1. Initialization: Use to initialize the loop variable.

2. Condition: It is checked after each iteration as an entry point to the loop.

3. Updation: Incrementing the loop variable to eventually terminate the loop not

satisfying the loop condition.

Below is a simple program on while loop.


#include<stdio.h>
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

/*
always declare the variables before using them
*/
int i = 0; // declaration and initialization at the same time

printf("\nPrinting numbers using while loop from 0 to 9\n\n");

/*
while i is less than 10
*/
while(i<10)
{
printf("%d\n",i);

/*
Update i so the condition can be met eventually
to terminate the loop
*/
i++; // same as i=i+1;
}
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}

Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Basic do while Loop Program


Every loop consists of three parts in sequence:

1. Initialization: Use to initialize the loop variable.

2. Condition: It is checked after each iteration as an entry point to the loop.

3. Updation: Incrementing the loop variable to eventually terminate the loop not

satisfying the loop condition.

Do while loop is used when the actual code must be executed atleast once. For
example: Incase of menu driven functions.
Below is a simple program on do while loop.
#include<stdio.h>

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

/*
always declare the variables before using them
*/
int i = 10; // declaration and initialization at the same time

do // do contains the actual code and the updation


{
printf("i = %d\n",i);
i = i-1; // updation
}
// while loop doesn't contain any code but just the condition
while(i > 0);

printf("\n\The value of i after exiting the loop is %d\n\n", i);


printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}

Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Basic program to show use of


nested for Loops
Nested loops are usually used to print a pattern in C. They are also used to print
out the matrix using a 2 dimensional array and a lot of other patterns like pyramid
of numbers etc.
Using a loop inside another loop is called nested loop.
Below is a simple program on nested loops.
#include<stdio.h>

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
printf("\n\nNested loops are usually used to print a pattern in c. \n\n");
printf("\n\nThey are also used to print out the matrix using a 2 dimensional
array. \n\n");

int i,j,k;
printf("\n\nOutput of the nested loop is :\n\n");
for(i = 0; i < 5; i++)
{
printf("\t\t\t\t");
for(j = 0; j < 5; j++)
printf("* ");

printf("\n");
}
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}

Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Program to print Factorial of a


Number
Below is a program to find factorial of a number using for loop.
#include<stdio.h>
#include<conio.h>
void main()
{
int fact, i, n;
fact = 1;
printf("Enter the number\t");
scanf("%d" , &n);
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

for(i = 1; i <= n; i++)


{
fact = fact*i;
}
printf("Factorial of %d is %d", n , fact);
getch();
}

Enter the number 5

Factorial of 5 is 120

Program to print the Fibonacci


Series
Below is a program to print the Fibonacci series using while loop.
#include<stdio.h>
#include<conio.h>

void fibonacci(int num);


void main()
{
int num = 0;
clrscr();
printf("Enter number of terms\t");
scanf("%d", &num);
fibonacci(num);
getch();
}

void fibonacci(int num)


{
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

int a, b, c, i = 3;
a = 0;
b = 1;
if(num == 1)
printf("%d",a);

if(num >= 2)
printf("%d\t%d",a,b);

while(i <= num)


{
c = a+b;
printf("\t%d", c);
a = b;
b = c;
i++;
}
}

Enter number of terms 6

0 1 1 2 3 5

Program to check whether a


Number is a Palindrome
Below is a program to check whether a number is a palindrome or not.
A palindrome is a number or a string which is similar when read from the front and from the
rear. For example: 121 or Oppo etc.
#include<stdio.h>
#include<conio.h>

void main()
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

{
int a, b, c, s = 0;
clrscr();
printf("Enter a number:\t");
scanf("%d", &a);
c = a;

// the number is reversed inside the while loop.


while(a > 0)
{
b = a%10;
s = (s*10)+b;
a = a/10;
}

// here the reversed number is compared with the given number.


if(s == c)
{
printf("The number %d is a palindrome", c);
}
else
{
printf("The number %d is not a palindrome", c);
}
getch();
}

Enter the number: 121

The number 121 is a palindrome


WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Program to find Sum of Digits of a


Number
Below is a program for sum of digits of a number.
#include<stdio.h>

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

int n, sum = 0, c, remainder;

printf("Enter the number you want to add digits of: ");


scanf("%d", &n);

while(n != 0)
{
remainder = n%10;
sum += remainder;
n = n/10;
}

printf("\n\nSum of the digits of the entered number is = %d\n\n", sum);


printf("\n\n\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}

Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Program to reverse a String


Below is a program to reverse a string in C language.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, k;
char str[100];
char rev[100];
printf("Enter a string:\t");
scanf("%s", str);
printf("The original string is %s\n", str);
for(i = 0; str[i] != '\0'; i++);
{
k = i-1;
}
for(j = 0; j <= i-1; j++)
{
rev[j] = str[k];
k--;
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

}
printf("The reverse string is %s\n", rev);
getch();
}

Enter a string: studytonight

The original string is studytonight

The reverse string is thginotyduts

Program to find average of N


Numbers
Below is a program to calculate average of n numbers.
#include<stdio.h>

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

int n, i;
float sum = 0, x;

printf("Enter number of elements: ");


scanf("%d", &n);
printf("\n\n\nEnter %d elements\n\n", n);
for(i = 0; i < n; i++)
{
scanf("%f", &x);
sum += x;
}
printf("\n\n\nAverage of the entered numbers is = %f", (sum/n));
printf("\n\n\n\n\t\t\tCoding is Fun !\n\n\n");
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

return 0;
}

Output:

Program to find Armstrong Number


between 1 to 500
An Armstrong number or Narcissistic number is a n digit number such that the
sum of its digits raised to the nth power is equal to the number itself.
For example, Let's take an armstrong number: 153, which is 3 digit number,
here 13 + 53 + 33 is 1 + 125 + 27 which is equal to 153.
Below is a program to find armstrong numbers between 1 to 500.
#include<stdio.h>
#include<math.h>
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int n,sum,i,t,a;
printf("\n\n\nThe Armstrong numbers in between 1 to 500 are : \n\n\n");

for(i = 1; i <= 500; i++)


{
t = i; // as we need to retain the original number
sum = 0;
while(t != 0)
{
a = t%10;
sum += a*a*a;
t = t/10;
}

if(sum == i)
printf("\n\t\t\t%d", i);
}

printf("\n\n\n\n\t\t\tCoding is Fun !\n\n\n");


return 0;
}

Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Program to check whether a


number is Armstrong Number
Below is a program to check whether a number is armstrong or not.
#include<stdio.h>
#include<math.h>

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

int n, sum = 0, c, t, a;

printf("Enter a number: ");


scanf("%d", &n);

t = n; // as need to retain the original number


while(n != 0)
{
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

a = n%10;
sum += a*a*a;
n = n/10;
}

printf("\n\n\n\t\t\tsum = %d", sum);

if(sum == t)
printf("\n\n\t\t%d is an armstrong number\n", t);
else
printf("\n\n\t\t%d is not an armstrong number\n", t);

printf("\n\n\n\n\t\t\tCoding is Fun !\n\n\n");


return 0;
}

Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Checking for Odd and Even


Numbers using Bitwise Operator
Below is a program to find whether a number is even or odd using bitwise
operator.
x&1 returns true if the LSB(Least significant Bit) of binary representation of an
integer x is 1. It returns false if the LSB or the Right most bit in a binary sequence
is 0.
In binary representation of an integer, if LSB is 1 then it is odd and if LSB is 0
then it is even.
#include<stdio.h>

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int x;
for(x = 0; x <= 10; x++)
{
if(x&1) // if number is odd
printf("\t\t\t%d is odd\n",x);
else if(!(x&1)) // ! is used inside if to reverse the boolean value
printf("\t\t\t%d is even\n",x);
}

printf("\n\n\n\n\t\t\tCoding is Fun !\n\n\n");


return 0;
}

Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

We have used a simple for loop to input numbers and show how to use the
Bitwise operator. You can take input from user using scanf() and use th same
logic to find if the input number is odd or even.

Checking if inout number is Odd or


Even without using %(Mod)
Operator
Below is a program to find whether a number is even or odd without using %(Mod)
operator.
#include<stdio.h>

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int n;
printf("Enter a number: ");
scanf("%d",&n);

if((n/2)*2 == n)
printf("\n\n\t\t %d is Even\n", n);
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

else
printf("\n\n\t\t %d is Odd\n", n);

printf("\n\n\n\n\t\t\tCoding is Fun !\n\n\n");


return 0;
}

Output:

Program to find Factors of a


Number
Below is a program to find factors of a number.
#include<stdio.h>

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int num, i;
printf("Enter the number to find the factors of : ");
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

scanf("%d",&num);
printf("\n\n\nFactors of %d are \n\n", num);

for(i = 1; i <= num/2; i++)


{
if(num%i == 0)
printf("\t\t\t%d\n", i);
}

printf("\n\n\n\n\t\t\tCoding is Fun !\n\n\n");


return 0;
}

Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Program to find Sum of N input


Numbers in C
Below is a program on sum of n numbers.
#include<stdio.h>

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int n,sum=0,c,value;

printf("\n\nEnter the number of integers you want to add: ");


scanf("%d", &n);

printf("Enter %d integers\n\n",n);
for(c = 1; c <= n; c++)
{
scanf("%d", &value);
/*
need to initialise sum before using otherwise
garbage value will get printed
*/
sum += value;
}

printf("\n\n\nsum of entered numbers = %d", sum);


printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}

Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Program to find first N Prime


Numbers
Below is a program to find first n prime numbers using nested for loops, where
the value of n is input by the user.
#include<stdio.h>

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int n,i = 3, count, c;

printf("\nEnter the number of prime numbers required : ");


scanf("%d", &n);

if(n >= 1)
{
printf("\n\nFirst %d prime numbers are : ", n);
printf("2 ");
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

// iteration for n prime numbers


// i is the number to be checked in each iteration starting from 3
for(count = 2; count <= n; i++)
{
// iteration to check c is prime or not
for(c = 2; c < i; c++)
{
if(i%c == 0)
break;
}

if(c == i) // c is prime
{
printf("%d ", i);
count++; // increment the count of prime numbers
}

}
printf("\n\n\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}

Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Program to find the Largest


number among ninput Numbers
Below is a program to find largest number among n user input numbers.
#include<stdio.h>

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int n,i;
float c,big;

printf("\n\nEnter the number of elements you wish to find the greatest element
of: ");
scanf("%d", &n);
printf("\n\nEnter %d numbers :\n", n);

printf("\n\n\t\t\tElement 1: ");

//Important step- always initialize big to the first element


scanf("%f", &big);
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

for(i = 2; i <= n; i++)


{
printf("\n\t\t\tElement %d : ", i);
scanf("%f", &c);
/*
if input number is larger than the
current largest number
*/
if(big < c)
big = c; // update big to the larger value
}

printf("\n\n\nThe largest of the %d numbers is %f ", n, big);


printf("\n\n\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}

Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Program to find exponential


without using pow()method
Below is a program to find exponential without using pow() method.
long long int is of double the size of long int.
%lld is the format specifier for long long int.
#include<stdio.h>
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

int n, exp, exp1;


long long int value = 1;
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

printf("Enter the number and its exponential:\n\n");


scanf("%d%d",&n, &exp);

exp1 = exp; // storing original value for future use

// same as while((--exp)!=-1)
while(exp-- > 0)
{
value *= n; // multiply n to itself exp times
}

printf("\n\n %d^%d = %lld\n\n", n, exp1, value);


printf("\n\n\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}

Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Program to check if input Number


is int or float
Below is a program to check whether the user input number is of integer or float
datatype.
strlen() does not count the null character '\0'.
#include<stdio.h>

#include<conio.h>
#include<string.h>

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
char number[10];
int flag = 0;
int length, i = 0;

printf("\n\nEnter a number: ");


scanf("%s", number);

length = strlen(number);

// till string does not end


while(number[i++] != '\0') // same as while(length-->0)
{
if(number[i] == '.') // decimal point is present
{
flag = 1;
break;
}
}

// if(0) is same as if(false)


if(flag)
printf("\n\n\n\tEntered Number is a Floating point Number\n\n");
else
printf("\n\n\n\tEntered Number is a integer Number\n\n");
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

printf("\n\n\n\n\t\t\tCoding is Fun !\n\n\n");


return 0;
}

Output:

Program to print the Multiplication


Table of any Number
Below is a program to print the multiplication table of any user input number.
#include<stdio.h>

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int n,i;
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

printf("Enter an integer you need to print the table of: ");


scanf("%d", &n);
printf("\n\n\n");

for(i = 1; i <= 10; i++)


{
printf("\n\t\t\t%d * %d = %d \n", n, i, n*i);
}

printf("\n\n\n\n\t\t\tCoding is Fun !\n\n\n");


return 0;
}

Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Program to print the reverse of an


Array
Below is a simple program to reverse an array.
#include<stdio.h>

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

int c, d, n, a[100], b[100];


printf("\n\nEnter number of elements in array :");
scanf("%d", &n);
printf("\n\nEnter %d elements\n", n);

for(c = 0; c < n; c++)


scanf("%d", &a[c]);

/*
temporarily storing elements into array b
starting from end of array a
*/
for(c = n-1, d = 0; c >= 0; c--, d++)
b[d] = a[c];

/*
copying reversed array into original.
Here we are modifying original array to reverse it.
*/

for(c = 0; c < n; c++)


a[c] = b[c];

printf("\n\n Resultant array is: ");


for(c = 0; c < n; c++)
printf("%d", a[c]);

printf("\n\n\t\t\tCoding is Fun !\n\n\n");


WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

return 0;
}

Output:

Program to insert an element in an


Array
Below is a simple program to insert an element in an array.
Here is the C language tutorial explaining Arrays → Arrays in C
#include<stdio.h>

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

int array[100], position, c, n, value;

printf("\n\nEnter number of elements in array:");


scanf("%d", &n);

printf("\n\nEnter %d elements\n", n);


for(c = 0; c < n; c++)
scanf("%d", &array[c]);

printf("\n\nEnter the location where you want to insert new element: ");
scanf("%d", &position);

printf("\n\nEnter the value to insert: ");


scanf("%d", &value);

// shifting the elements from (position to n) to right


for(c = n-1; c >= position-1; c--)
array[c+1] = array[c];

array[position - 1] = value; // inserting the given value

printf("\n\nResultant array is: ");


/*
the array size gets increased by 1
after insertion of the element
*/
for(c = 0; c <= n; c++)
printf("%d ", array[c]);

printf("\n\n\t\t\tCoding is Fun !\n\n\n");


return 0;
}

In the above program we take an array as user input and then ask the user for a
new number that they wish to add to the original array, and the position where
they want to add the new number.
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

The we shift the existing numbers from the index position to the end of the array
one position to the right, therby vacating a space for the new element. And then
we add the new number at the user specified position index.

Output:

Program to Delete an Element from


Array in C
Below is a simple program to delete an element from array, where the position of
element to be deleted is given by user.
#include<stdio.h>

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

int array[100], position, c, n;


WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

printf("\n\nEnter number of elements in array:");


scanf("%d", &n);

printf("\n\nEnter %d elements\n", n);


for(c = 0; c < n; c++)
scanf("%d", &array[c]);

printf("\n\nEnter the location where you want to delete element from: ");
scanf("%d", &position);

if(position >= n+1)


printf("\n\nDeletion not possible\n\n");
else
// updating the locations with next elements
for(c = position-1; c < n-1; c++)
array[c] = array[c+1];

printf("\n\nResultant array is: ");


/*
the array size gets reduced by 1
after deletion of the element
*/
for(c = 0; c < n-1; c++)
printf("%d ", array[c]);

printf("\n\n\t\t\tCoding is Fun !\n\n\n");


return 0;
}

Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Program to Delete an element from


array based on value
Below is a simple program to delete an element from array, where the element to
be deleted is given by user:
#include<stdio.h>

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

int array[10], element, c, n, pos;


/*
initialization as garbage value is
stored by default in c variables
*/
int found = 0;
printf("\n\nEnter number of elements in array:");
scanf("%d", &n);

printf("\n\nEnter %d elements\n", n);


WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

for(c = 0; c < n; c++)


scanf("%d", &array[c]);

printf("\n\nThe input array is: ");


for(c = 0; c < n; c++)
printf("%d", array[c]);

printf("\n\nEnter the element to be deleted: ");


scanf("%d", &element);

// check the element to be deleted is in array or not


for(c = 0; c < n; c++)
{
if(array[c] == element)
{
found = 1;
pos = c;
break; // terminate the loop
}
}
if(found == 1) // the element to be deleted exists in the array
{
for(c = pos; c < n-1; c++)
array[c] = array[c+1];
}
else
printf("\n\nElement %d is not found in the array\n\n", element);

printf("\n\nResultant array is: ");


/*
the array size gets reduced by 1
after deletion of the element
*/
for(c = 0; c < n-1; c++)
printf("%d ",array[c]);

printf("\n\n\t\t\tCoding is Fun !\n\n\n");


return 0;
}
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Output:

Program to find Largest and


Smallest Element in an Array
Below is a program to find the largest and smallest elements in array.
#include<stdio.h>

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int a[50], size, i, big, small;

printf("\nEnter the size of the array: ");


WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

scanf("%d", &size);

printf("\n\nEnter the %d elements of the array: \n\n", size);


for(i = 0; i < size; i++)
scanf("%d", &a[i]);

big = a[0]; // initializing


/*
from 2nd element to the last element
find the bigger element than big and
update the value of big
*/
for(i = 1; i < size; i++)
{
if(big < a[i]) // if larger value is encountered
{
big = a[i]; // update the value of big
}
}
printf("\n\nThe largest element is: %d", big);

small = a[0]; // initializing


/*
from 2nd element to the last element
find the smaller element than small and
update the value of small
*/
for(i = 1; i < size; i++)
{
if(small>a[i]) // if smaller value is encountered
{
small = a[i]; // update the value of small
}
}
printf("\n\nThe smallest element is: %d", small);
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Output:

Program to find Sum of N input


Numbers using Array
Below is a program to find and print the sum of n numbers using arrays.
#include<stdio.h>

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int n, sum = 0, c, array[100];

printf("Enter the number of integers you want to add: ");


scanf("%d", &n);
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

printf("\n\nEnter %d integers \n\n", n);

for(c = 0; c < n; c++)


{
scanf("%d", &array[c]);
sum += array[c]; // same as sum = sum + array[c]
}

printf("\n\nSum = %d\n\n", sum);


printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}

Output:

Simple Program to Sort Array


elements
Below is a program to sort array elements in an array.
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

#include<stdio.h>
#include<conio.h>

void sorting(int *x, int y);

void main()
{
int a[20], i, c, n;
clrscr();
printf("Enter number of elements you want to sort: ");
scanf("%d", &n);

for(i = 0; i < n; i++)


scanf("%d", &a[i]);

sorting(a, n);

for(i = 0; i <n; i++)


printf("%d\t", a[i]);

getch();
}

void sorting(int *x, int y)


{
int i, j, temp;
for(i = 1; i <= y-1; i++)
{
for(j = 0; j < y-i; j++)
{
if(*(x+j) > *(x+j+1))
{
temp = *(x+j);
*(x+j) = *(x+j+1);
*(x+j+1) = temp;
}
}
}
}
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Enter number of elements you want to sort: 6

5 3 4 2 1 6

1 2 3 5 6

Simple Program to remove


Duplicate Element in an Array
Below is a program to find and remove any duplicate element present in the
specified array.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20], i, j, k, n;
clrscr();

printf("\nEnter array size: ");


scanf("%d", &n);

printf("\nEnter %d array element: ", n);


for(i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}

printf("\nOriginal array is: ");


for(i = 0; i < n; i++)
{
printf(" %d", a[i]);
}

printf("\nNew array is: ");


for(i = 0; i < n; i++)
{
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

for(j = i+1; j < n; )


{
if(a[j] == a[i])
{
for(k = j; k < n; k++)
{
a[k] = a[k+1];
}
n--;
}
else
{
j++;
}
}
}

for(i = 0; i < n; i++)


{
printf("%d ", a[i]);
}
getch();
}

Enter array size: 5

Enter 5 array element: 11 13 11 12 13

Original array is: 11 13 11 12 13

New array is: 11 13 12

Program to check whether a two


dimensional array is a Sparse
Matrix
A Sparse Matrix is a matrix(two-dimensional array) in which number of 0's is
greater than the number of non-zero elements.
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Below is a program to check whether a matrix is sparse or not.


#include<stdio.h>

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int n, m, c, d, matrix[10][10];
int counter = 0;
printf("\nEnter the number of rows and columns of the matrix \n\n");
scanf("%d%d",&m,&n);

printf("\nEnter the %d elements of the matrix \n\n", m*n);


for(c = 0; c < m; c++) // to iterate the rows
{
for(d = 0; d < n; d++) // to iterate the columns
{
scanf("%d", &matrix[c][d]);
if(matrix[c][d] == 0)
counter++; // same as counter=counter +1
}
}

// printing the matrix


printf("\n\nThe entered matrix is: \n\n");
for(c = 0; c < m; c++) // to iterate the rows
{
for(d = 0; d < n; d++) // to iterate the columns
{
printf("%d\t", matrix[c][d]);
}
printf("\n"); // to take the control to the next row
}

// checking if the matrix is sparse or not


if(counter > (m*n)/2)
printf("\n\nThe entered matrix is a sparse matrix\n\n");
else
printf("\n\nThe entered matrix is not a sparse matrix\n\n");
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

printf("\n\n\t\t\tCoding is Fun !\n\n\n");


return 0;
}

Output:

Program to check whether given


Square Matrix is symmetric or not
Few important points to remember:

 A Square Matrix is said to be symmetric if it is equal to it's transpose.

 Transpose of a matrix is achieved by exchanging indices of rows and

columns.

 Transpose is only defined for a square matrix.

Below is a program to check whether sqaure matrix is symmetric or not.


#include<stdio.h>
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

int c, d, a[10][10], b[10][10], n, temp;


printf("\nEnter the dimension of the matrix: \n\n");
scanf("%d", &n);

printf("\nEnter the %d elements of the matrix: \n\n",n*n);


for(c = 0; c < n; c++) // to iterate the rows
for(d = 0; d < n; d++) // to iterate the columns
scanf("%d", &a[c][d]);

// finding transpose of a matrix and storing it in b[][]


for(c = 0; c < n; c++) // to iterate the rows
for(d = 0; d < n; d++) //to iterate the columns
b[d][c] = a[c][d];

// printing the original matrix


printf("\n\nThe original matrix is: \n\n");
for(c = 0; c < n; c++) // to iterate the rows
{
for(d = 0; d < n; d++) // to iterate the columns
{
printf("%d\t", a[c][d]);
}
printf("\n");
}

// printing the transpose of the entered matrix


printf("\n\nThe Transpose matrix is: \n\n");
for(c = 0; c < n; c++) // to iterate the rows
{
for(d = 0; d < n; d++) // to iterate the columns
{
printf("%d\t", b[c][d]);
}
printf("\n");
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

// checking if the original matrix is same as its transpose


for(c = 0; c < n; c++) // to iterate the rows
{
for(d = 0; d < n; d++) // to iterate the columns
{
/*
even if they differ by a single element,
the matrix is not symmetric
*/
if(a[c][d] != b[c][d])
{
printf("\n\nMatrix is not Symmetric\n\n");
exit(0); // a system defined method to terminate the program
}
}
}

/*
if the program is not terminated yet,
it means the matrix is symmetric
*/
printf("\n\nMatrix is Symmetric\n\n");
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}

Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Program to find Deteminant of 2x2


Matrix
Below is a program to find the determinant of a 2x2 matrix.
Please note that, when we say a 2x2 matrix, we mean an array of 2x2.
#include<stdio.h>

int main()
{
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

int a[2][2], i, j;
long determinant;

printf("\n\nEnter the 4 elements of the array\n");


for(i = 0; i < 2; i++)
for(j = 0; j < 2; j++)
scanf("%d", &a[i][j]);

printf("\n\nThe entered matrix is: \n\n");


for(i = 0; i < 2; i++)
{
for(j = 0; j < 2; j++)
{
printf("%d\t", a[i][j]); // to print the complete row
}
printf("\n"); // to move to the next row
}

// finding the determinant of a 2x2 matrix


determinant = a[0][0]*a[1][1] - a[1][0]*a[0][1];
printf("\n\nDterminant of 2x2 matrix is : %d - %d = %d", a[0][0]*a[1][1],
a[1][0]*a[0][1], determinant);

printf("\n\n\t\t\tCoding is Fun !\n\n\n");


return 0;
}

Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Program to find Normal and Trace


of a Square Matrix
Few important points to remember:

 Normal and Trace are only defined for a square matrix.

 Square Matrix: Matrix in which, the number of rows = number of columns.

 Normal: Square root of the sum of the squares of each element of the matrix.

 Trace: Sum of the diagonal elements of a matrix.

 Diagonal Element: An element having same indices for row and column.

Let's have a simple example to understand these terms better:


WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Below is a program to find Normal and Trace of Square Matrix.


#include<stdio.h>
/*
to use the sqrt method to find
the square root of a number we include
math.h header file
*/
#include<math.h>

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

int i, j, n, aj[10][10], sum = 0, sum1 = 0, a = 0, normal;

printf("\nEnter the number of rows (columns) of the matrix: \n\n");


scanf("%d", &n);

printf("\nEnter the %d elements of the first matrix: \n\n", n*n);

for(i = 0; i < n; i++) // to iterate the rows


WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

{
for(j = 0; j < n; j++) // to iterate the columns
{
scanf("%d", &aj[i][j]);
a = aj[i][j]*aj[i][j]; // finding square of each element
sum1 += a; // same as sum1 = sum1 + a
}
}
normal = sqrt((double)sum1); // typecasting to double value

printf("\n\nThe normal of the given matrix is: %d", normal);


for(i = 0; i < n; i++)
{
sum = sum + aj[i][i]; // sum of the diagonal elements
}
printf("\n\nThe Trace of the given matrix is: %d", sum);
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}

Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Program to perform addition and


subtraction of Matrices
Below is a program to perform Addition and Subtraction on two matrices.
\n is used to take the control to the next row.
\t is used to take control 5 spaces(tab) ahead.
#include<stdio.h>

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

int n, m, c, d, first[10][10], second[10][10], sum[10][10], diff[10][10];


printf("\nEnter the number of rows and columns of the first matrix \n\n");
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

scanf("%d%d", &m, &n);

printf("\nEnter the %d elements of the first matrix \n\n", m*n);


for(c = 0; c < m; c++) // to iterate the rows
for(d = 0; d < n; d++) // to iterate the columns
scanf("%d", &first[c][d]);

printf("\nEnter the %d elements of the second matrix \n\n", m*n);


for(c = 0; c < m; c++) // to iterate the rows
for(d = 0; d < n; d++) // to iterate the columns
scanf("%d", &second[c][d]);

/*
printing the first matrix
*/
printf("\n\nThe first matrix is: \n\n");
for(c = 0; c < m; c++) // to iterate the rows
{
for(d = 0; d < n; d++) // to iterate the columns
{
printf("%d\t", first[c][d]);
}
printf("\n");
}

/*
printing the second matrix
*/
printf("\n\nThe second matrix is: \n\n");
for(c = 0; c < m; c++) // to iterate the rows
{
for(d = 0; d < n; d++) // to iterate the columns
{
printf("%d\t", second[c][d]);
}
printf("\n");
}

/*
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

finding the SUM of the two matrices


and storing in another matrix sum of the same size
*/
for(c = 0; c < m; c++)
for(d = 0; d < n; d++)
sum[c][d] = first[c][d] + second[c][d];

// printing the elements of the sum matrix


printf("\n\nThe sum of the two entered matrices is: \n\n");
for(c = 0; c < m; c++)
{
for(d = 0; d < n; d++)
{
printf("%d\t", sum[c][d]);
}
printf("\n");
}

/*
finding the DIFFERENCE of the two matrices
and storing in another matrix difference of the same size
*/
for(c = 0; c < m; c++)
for(d = 0; d < n; d++)
diff[c][d] = first[c][d] - second[c][d];

// printing the elements of the diff matrix


printf("\n\nThe difference(subtraction) of the two entered matrices is:
\n\n");
for(c = 0; c < m; c++)
{
for(d = 0; d < n; d++)
{
printf("%d\t", diff[c][d]);
}
printf("\n");
}

printf("\n\n\t\t\tCoding is Fun !\n\n\n");


return 0;
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

C Program for Matrix Multiplication


Below is a program on Matrix Multiplication.
Two matrices with a given order can be multiplied only when number of columns
of first matrix is equal to the number of rows of the second matrix.
#include<stdio.h>

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

int n, m, c, d, p, q, k, first[10][10], second[10][10], pro[10][10],sum = 0;

printf("\nEnter the number of rows and columns of the first matrix: \n\n");
scanf("%d%d", &m, &n);

printf("\nEnter the %d elements of the first matrix: \n\n", m*n);


for(c = 0; c < m; c++) // to iterate the rows
for(d = 0; d < n; d++) // to iterate the columns
scanf("%d", &first[c][d]);

printf("\nEnter the number of rows and columns of the first matrix: \n\n");
scanf("%d%d", &p, &q);

if(n != p)
printf("Matrices with the given order cannot be multiplied with each
other.\n\n");

else // matrices can be multiplied


{
printf("\nEnter the %d elements of the second matrix: \n\n",m*n);

for(c = 0; c < p; c++) // to iterate the rows


for(d = 0; d < q; d++) // to iterate the columns
scanf("%d", &second[c][d]);

// printing the first matrix


printf("\n\nThe first matrix is: \n\n");
for(c = 0; c < m; c++) // to iterate the rows
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

{
for(d = 0; d < n; d++) // to iterate the columns
{
printf("%d\t", first[c][d]);
}
printf("\n");
}

// printing the second matrix


printf("\n\nThe second matrix is: \n\n");
for(c = 0; c < p; c++) // to iterate the rows
{
for(d = 0; d < q; d++) // to iterate the columns
{
printf("%d\t", second[c][d]);
}
printf("\n");
}

for(c = 0; c < m; c++) // to iterate the rows


{
for(d = 0; d < q; d++) // to iterate the columns
{
for(k = 0; k < p; k++)
{
sum = sum + first[c][k]*second[k][d];
}
pro[c][d] = sum; // resultant element of pro after multiplication
sum = 0; // to find the next element from scratch
}
}

// printing the elements of the product matrix


printf("\n\nThe multiplication of the two entered matrices is: \n\n");
for(c = 0; c < m; c++) // to iterate the rows
{
for(d = 0; d < q; d++) // to iterate the columns
{
printf("%d\t", pro[c][d]);
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

}
printf("\n"); // to take the control to the next row
}
}
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}

Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Basic Program for Pointers


Few important points to remember:

 * is used to access the value stored in the pointer variable.

 & is used to store the address of a given variable.

Here is the C language tutorial on Pointers in C → Pointers in C


Below is a simple program on pointer.
int *p; is a pointer variable declaration where p is a pointer to an int variable
i.e. it stores the location of an integer.
%x is a format specifier to print hexadecimal value. It is usually used to print the
location.
#include <stdio.h>

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int var = 24; // actual variable declaration
int *p;

p = &var; // storing address of int variable var in pointer p

printf("\n\nAddress of var variable is: %x \n\n", &var);

// address stored in pointer variable


printf("\n\nAddress stored in pointer variable p is: %x", p);

// access the value using the pointer variable


printf("\n\nValue of var variable or the value stored at address p is %d ",
*p);

printf("\n\n\t\t\tCoding is Fun !\n\n\n");


return 0;
}
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Output:

Program for Dynamic Memory


Allocation using malloc()
Below is a program on dynamic memory allocation using malloc() and clearing
out memory space using free().
sizeof() returns the number of bytes occupied by any datatype, in this case by
an integer.
#include <stdio.h>

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int n, i, *ptr, sum = 0;

printf("\n\nEnter number of elements: ");


scanf("%d", &n);

// dynamic memory allocation using malloc()


ptr = (int *) malloc(n*sizeof(int));
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

if(ptr == NULL) // if empty array


{
printf("\n\nError! Memory not allocated\n");
return 0; // end of program
}

printf("\n\nEnter elements of array: \n\n");


for(i = 0; i < n; i++)
{
// storing elements at contiguous memory locations
scanf("%d", ptr+i);
sum = sum + *(ptr + i);
}

// printing the array elements using pointer to the location


printf("\n\nThe elements of the array are: ");
for(i = 0; i < n; i++)
{
printf("%d ",ptr[i]); // ptr[i] is same as *(ptr + i)
}

/*
freeing memory of ptr allocated by malloc
using the free() method
*/
free(ptr);

printf("\n\n\t\t\tCoding is Fun !\n\n\n");


return 0;
}

Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Accessing array
elements(Traversing array) by
incrementing a Pointer
Name of the array refers to the base address of the array.
Here we have a tutorial to understand How Pointer arithmetic works?
Below is a program to access elements of an array using pointer increment.
#include <stdio.h>
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

const int MAX = 3; // Global declaration


int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int var[] = {100, 200, 300};
int i, *ptr;

/*
storing address of the first element
of the array in pointer variable
*/
ptr = var;

for(i = 0; i < MAX; i++)


{
printf("\n\n\nAddress of var[%d] = %x ", i, ptr);
printf("\nValue of var[%d] = %d ", i, *ptr);

// move to the next location


ptr++;
}
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}

Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Traversing array elements by


decrementing a Pointer
Below is a program to access elements of an array using pointer decrement.
#include <stdio.h>

const int MAX = 3; // Global declaration


int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int var[] = {100, 200, 300};
int i, *ptr;

/*
storing address of the last element
of the array in pointer variable
*/
ptr = &var[MAX-1];

for(i = MAX; i > 0; i--)


{
printf("\n\n\nAddress of var[%d] = %x ", i, ptr);
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

printf("\nValue of var[%d] = %d ", i, *ptr);

// move to the previous location


ptr--;
}
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}

Output:

Pointer Comparison in C
In C language pointers can be compared if the two pointers are pointing to the
same array.
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

All relational operators can be used for pointer comparison, but a pointer cannot
Multiplied or Divided.
Below is a program on pointer comparison for same type of pointer:
#include <stdio.h>

int main()
{
int *ptrA,*ptrB;

ptrA = (int *)1;


ptrB = (int *)2;

if(ptr2 > ptr1)


printf("PtrB is greater than ptrA");

return(0);
}

Below is a program on pointer comparison for different type of pointer:


#include <stdio.h>

int main()
{
int *ptrA;
float *ptrB;

ptrA = (int *)1000;


ptrB = (float *)2000;

if(ptrB > ptrA)


printf("PtrB is greater than ptrA");

return(0);
}
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Basic C Program for Pointer to a


Pointer
A pointer variable stores the address of a value. Similarly, a pointer to a pointer
stores the address of the pointer variable. Pointer to a pointer is executed making
use of **.
Below is a program on pointer to a pointer.
int var; is a integer variable which stores value.
int *ptr; is a pointer variable which stores the address of an integer variable.
int **pptr; is a pointer to a pointer variable which stores the address of a
pointer variable.
#include<stdio.h>

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int var;
int *ptr;
int **pptr;

var = 50;

// take the address of the variable var


ptr = &var;

// taking the address of ptr using address of operator-&


pptr = &ptr;

// take the value using the pptr


printf("\n\nValue of var = %d\n\n", var);

printf("\n\nValue available at *ptr = %d\n\n", *ptr);


WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

printf("\n\nValue available at **pptr = %d\n\n", **pptr);

printf("\n\n\t\t\tCoding is Fun !\n\n\n");


return 0;
}

Output:

C Program to Reverse a String


using Pointer
Below is a program to reverse a string using pointer:
#include <stdio.h>

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

char str[100];
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

char rev[100];
char *sptr = str; // sptr stores the base address of the str
char *rptr = rev; // rptr stores the base address of the reverse

int i = -1;

printf("\n\nEnter a string: ");


scanf("%s", str);

// storing the ending address of str in sptr


while(*sptr)
{
sptr++;
i++; // i is the index of the end location
}

// storing the string str in rev in reverse order


while(i >= 0)
{
/*
First decrementing then using as it stores
the location after the end location due to above while loop
*/
sptr--;
*rptr = *sptr; // storing the value in sptr in rptr
rptr++; // pointing to next location
i--; // decrementing the index
}
/*
String should always end with '\0' so explicitly
putting it at the end of the string
*/
*rptr = '\0';
rptr = rev; // restoring the base address of the reverse string

// storing the reverse string in the original string


while(*rptr)
{
*sptr = *rptr;
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

sptr++;
rptr++;
}

// printing the reverse string


printf("\n\nReverse of the string is: %s ", str);
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}

Output:

C Program to Swap Two Numbers


using Pointers
Below is a program to swap two numbers using pointers.
#include<stdio.h>

int main()
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

int a, b;
int *ptra, *ptrb;
int temp;

printf("Enter value for a: ");


scanf("%d", &a);

printf("\n\nEnter value for b: ");


scanf("%d", &b);

printf("\n\nThe values before swapping are: a = %d b = %d", a, b);

ptra = &a; // to store the location of a


ptrb = &b; // to store the location of b

temp = *ptra; // temp stores the value at location ptra


*ptra = *ptrb; // assigning value at location ptrb to ptra
*ptrb = temp; // assign value of themp to the variable at location ptrb

printf("\n\nThe values after swapping are: a = %d b = %d", a, b);

printf("\n\n\n\n\t\t\tCoding is Fun !\n\n\n");


return 0;

Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

C program for Pointer to a Function


Below is a program on pointer to a function.
Here is the C language tutorial explaining Pointer with Functions → Pointer to a
Function
#include<stdio.h>

int func(int a, int b) // function definition


{
printf("\n\n a = %d \n", a);
printf("\n\n b = %d \n", b);
}

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

// function pointer
int(*fptr)(int , int);

// assign address to function pointer


WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

fptr = func;

// function calling
func(2,3);
fptr(2,3); // calling a function referring to pointer to a function

printf("\n\n\t\t\tCoding is Fun !\n\n\n");


return 0;
}

Output:

Null Pointer Program


Null pointer is a special reserved value of a pointer. A pointer of any type has this
reserved value. Formally, each specific pointer type(int *, char *, etc) has its own
dedicated null-pointer value. Conceptually, when a pointer has that Null value it is
not pointing anywhere.
Void pointer is a specific pointer type. void * which is a pointer that points to
some data location in storage, which doesn't have any specific type.
Null pointer is a value whereas, Void pointer is a type.
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Below is a program on NULL pointer.


#include<stdio.h>

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int *ptr = NULL; // ptr is a NULL pointer

printf("\n\n The value of ptr is: %x ", ptr);


printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}

Output:

C Program for Adding Two


Numbers Using Recursion
Below is a program on adding two numbers using recursion.
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

#include<stdio.h>

int y;

/*
Function to add two numbers and
return the result
*/
int add(int m, int n)
{
if(n == 0)
return m;

/*
Recursion: adding 1, n times and
then at the end adding m to it
*/
y = add(m, n-1) + 1;
return y; // return the result
}

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int a, b, r;
printf("Enter the two numbers:\n");
scanf("%d%d", &a, &b);
r = add(a, b); // function call
printf("\n\nSum of two numbers is: %d\n\n", r);
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}

Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Program to find Factorial of a


Number using Recursion
Below is a program for finding factorial of a given number using recursion.
#include<stdio.h>
// declaring the function
int fact(int);

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int num, f;
printf("\n\nEnter a number: ");
scanf("%d", &num);
f= fact(num);
printf("\n\nFactorial of %d is %d\n\n", num, f);
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}

int fact(int aj)


WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

{
if(aj==1 || aj==0)
return 1;
else
return (aj*fact(aj-1));
}

Output:

Program to print Fibonacci Series


using Recursion
A Fibonacci series is defined as a series in which each number is the sum of the
previous two numbers with 1, 1 being the first two elements of the series.
static keyword is used to initialize the variables only once.
Below is a program to print the fibonacci series using recursion.
#include<stdio.h>
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

// declaring the function


void printFibo(int );

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int k, n;
long int i = 0, j = 1;
printf("Enter the length of the Fibonacci series: ");
scanf("%d", &n);
printf("\n\nfirst %d terms of Fibonacci series are:\n\n\n",n);
printf("%d ", 1);
printFibo(n);
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}

void printFibo(int aj)


{
static long int first = 0, second = 1, sum;
if(aj > 1)
{
sum = first + second;
first = second;
second = sum;
printf("%ld ", sum);
printFibo(aj-1); // recursive call
}
else
{
// after the elements, for line break
printf("\n\n\n");
}
}

Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Program to find Sum of First N


Numbers
Below is a program to find sum of the first n numbers using recursion, where the
value of n is provided by the user.
#include<stdio.h>
// declaring the recursive function
int getSum(int);

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int n, sum;
printf("\n\nEnter the range of n: ");
scanf("%d", &n);
sum = getSum(n);
printf("\n\nThe sum of first %d numbers is %d\n", n, sum);
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

// function definition
int getSum(int aj)
{
/*
static variables hold their values
till the end of the program
*/
static int sum = 0;
if(aj > 0)
{
sum = sum + aj;
getSum(aj-1);
}
return sum ;
}

Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Program to find Sum of digits of a


Number using Recursion
Below is a program to find sum of digits of a given number using recursion.
#include<stdio.h>
#include<conio.h>

//declaring the recursive function


int sumOfDigit(int num);

void main()
{
int num, sum;
clrscr();
printf("Enter a number:\t");
scanf("%d", &num);
sum = sumOfDigit(num);
printf("The sum of digits of %d is: %d", num, sum);
getch();
}

int sumOfDigit(int num)


{
int s, a;
s = s + (num%10);
a = num/10;
if(a > 0)
{
sumOfDigit(a);
}
return s;
}

Enter a number: 108

The sum of digits of 108 is: 9


WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Program to find Palindrome using


Recursion
A Palindrome is a sequence that if reversed looks identical to the original
sequence Eg : abba, level, 999 etc.
Below is a simple C program to find whether the user input number is a
palindrome or not using recursion:
#include<stdio.h>

// declaring the recursive function


int isPal(int );

/*
global declaration to use the same value
in both the functions
*/
int n;

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int palindrome;
printf("\n\nEnter a number to check for Palindrome: ");
scanf("%d", &n);
palindrome = isPal(n);
if(palindrome == 1)
printf("\n\n\n%d is palindrome\n\n", n);
else
printf("\n\n\n%d is not palindrome\n\n", n);

printf("\n\n\t\t\tCoding is Fun !\n\n\n");


return 0;
}

int isPal(int aj)


WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

{
static int sum = 0;
if(aj != 0)
{
sum = sum *10 + aj%10;
isPal(aj/10); // recursive call same as while(n!=0) using loop
}
else if(sum == n)
return 1;
else
return 0;
}

Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

C Program to calculate a Number


raised to the Power of N using
Recursion
Below is a program to calculate the result of a given number, raised to the power
of n using recursion.
#include<stdio.h>

// function prototype declaration


int power(int n1, int n2);

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int base, exp;
printf("Enter base number: ");
scanf("%d", &base);
printf("\n\nEnter Power factor: ");
scanf("%d", &exp);
printf("\n\n\n\t\t\t%d^%d = %d", base, exp, power(base, exp));
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}

int power(int b, int e)


{
if(e == 0)
return 1;

return (b*power(b, e-1));


}

Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

C Program to find the largest


Element in an Array using
Recursion
Below is a program to find the largest array element in a given array using
recursion.
#define is used to initialize a value which is more like a constant.
#include<stdio.h>

#define MAX 100

int getMaxElement(int []); // takes array of int as parameter


int size;

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int arr[MAX], max, i;
printf("\n\nEnter the size of the array: ");
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

scanf("%d", &size);
printf("\n\nEnter %d elements\n\n", size);

for(i = 0; i < size; i++)


{
scanf("%d", &arr[i]);
}

max = getMaxElement(arr); // passing the complete array as parameter


printf("\n\nLargest element of the array is %d\n\n", max);
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}

int getMaxElement(int a[])


{
static int i = 0, max =- 9999; // static int max=a[0] is invalid
if(i < size) // till the last element
{
if(max < a[i])
max = a[i];

i++; // to check the next element in the next iteration


getMaxElement(a); // recursive call
}
return max;
}

Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

C Program to find whether a


Number is Prime Or Composite
using Recursion
 Prime Number: A number that is only divisible by 1 and itself.

 Composite Number: A number that is not a prime number.

Note: 1 is neither prime nor composite.

Below is a program to find whether the user input number is a prime number or a composite
number using recursion.
#include<stdio.h>

// declaring the recursive function


WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

int isPrime(int, int);

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int num, prime;
printf("Enter a positive number to check if Prime: ");
scanf("%d", &num);
prime = isPrime(num, num/2);
if(prime == 1)
{
printf("\n\n%d is a prime number\n\n", num);
}
else
{
printf("\n\n%d is a Composite number\n\n", num);
}
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}

// function definition
int isPrime(int n, int i)
{
if(i == 1)
return 1; // return statement terminates the recursive funtion
else
{
if(n%i == 0)
return 0;
else
isPrime(n, i-1); // recursive call not using return statement
}
}

Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

C Program to find LCM of two


Numbers using Recursion
LCM: Least Common Multiple of two numbers is the number that is a common
multiple of the both the numbers.
Below is a program to find LCM of two numbers using recursion.
#include<stdio.h>

int find_lcm(int, int); // function prototype declaration

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int a, b, lcm;
printf("\n\nEnter 2 integers to find LCM of:\n");
scanf("%d%d", &a, &b);
lcm = find_lcm(a,b); // function call
printf("\n\n LCM of %d and %d is: %d\n\n", a, b, lcm);
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

int find_lcm(int a, int b) // function definition


{
/*
static variable is initialized only once
for each function call
*/
static int temp = 1;
if(temp%a == 0 && temp%b == 0)
{
return temp;
}
else
{
temp++;
find_lcm(a,b);
return temp;
}
}

Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

C Program to find GCD of two


Numbers using Recursion
Greatest Common Divisor(GCD) of two numbers is a number that divides both
of them.
Below is a program to the GCD of the two user input numbers using recursion.
#include<stdio.h>

// declaring the recursive function


int find_gcd(int , int );

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int a, b, gcd;
printf("\n\nEnter two numbers to find GCD of \n");
scanf("%d%d", &a, &b);
gcd = find_gcd(a, b);
printf("\n\nGCD of %d and %d is: %d\n\n", a, b, gcd);
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}

// defining the function


int find_gcd(int x, int y)
{
if(x > y)
find_gcd(x-y, y);

else if(y > x)


find_gcd(x, y-x);
else
return x;
}
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

Output:

Program to Reverse a String Using


Recursion
Below is a program to reverse a user input string using recursion in C language.
#include<stdio.h>
#include<conio.h>

// declaring recursive function


char* reverse(char* str);

void main()
{
int i, j, k;
char str[100];
char *rev;
printf("Enter the string:\t");
scanf("%s", str);
printf("The original string is: %s\n", str);
rev = reverse(str);
printf("The reversed string is: %s\n", rev);
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD

getch();
}

// defining the function


char* reverse(char *str)
{
static int i = 0;
static char rev[100];
if(*str)
{
reverse(str+1);
rev[i++] = *str;
}
return rev;
}

Enter the string: studytonight

The original string is: studytonight

The reverse string is: thginotyduts

You might also like