C Programming

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

Page 1 of 56

3. Logical Operators: --
Ex1) if (m1<40 || m2<40 || m3<40)
Printf (fail);
else
Printf (pass);
Ex2) if ((m1>=40) && (m2>=40) && (m3>=40))
Printf (pass);
else
Printf (fail);

PROGRAMS USING WHILE LOOP


/*program to find factorial of a given integer using WHILE loop*/
#include<stdio.h>
#include<conio.h>
main()
{
int n,fn,i;
clrscr();
printf("enter an integer ");
scanf("%d",&n);
fn=1;
i=1;
while(i<=n)
{
fn=fn*i;
i++;
}

printf("factorial of %d is %d\n",n,fn);
getch();
}

/*program to find ncr using WHILE loop*/


#include<stdio.h>
#include<conio.h>
main()
{
int n,r,k,fn,fr,fk,ncr,i;
clrscr();
printf("enter two integers ");
scanf("%d%d",&n,&r);
fn=1;
i=1;
while(i<=n)
{
fn=fn*i;
i++;
}

Page 1 of 56
Page 2 of 56

printf("factorial of %d is %d\n",n,fn);

fr=1;
i=1;
while(i<=r)
{
fr=fr*i;
i++;
}
printf("factorial of %d is %d\n",r,fr);

k = n - r;
fk = 1;
i = 1;
while(i <= k)
{
fk=fk*i;
i++;
}
printf("factorial of %d is %d\n",k,fk);

ncr=fn/(fr*fk);
printf("ncr= %d\n",ncr);
getch();
}

/*program to find GCD two integers using WHILE loop*/


#include<stdio.h>
#include<conio.h>
main()
{
int m,n,gcd,temp;
clrscr();
printf("enter two integers ");
scanf("%d%d",&m,&n);

while(n>0)
{
temp = m % n;
m = n;
n = temp;
}
gcd=m;
printf("gcd = %d\n",gcd);
getch();
}

Page 2 of 56
Page 3 of 56

/*program to find reverse number of a given number using WHILE loop*/


#include<stdio.h>
#include<conio.h>
main()
{
int num,revnum,d;
clrscr();
printf("enter an integer number ");
scanf("%d",&num);
revnum = 0;
while(num>0)
{
d = num % 10;
revnum = revnum * 10 + d;
num = num / 10;
}
printf("the reverse number = %d\n",revnum);
getch();
}

/*program to find sum of digits of a given integer number


using WHILE loop*/
#include<stdio.h>
#include<conio.h>
main()
{
int num,sum,d;
clrscr();
printf("enter an integer number ");
scanf("%d",&num);
sum = 0;
while(num>0)
{
d = num % 10;
sum = sum + d;
num = num / 10;
}
printf("the sum of digits of the number = %d\n",sum);
getch();
}

/*program to find whether the given number is armstrong number


or not using WHILE loop*/
#include<stdio.h>
#include<conio.h>
main()
{
int num,n,sum,d;

Page 3 of 56
Page 4 of 56

clrscr();
printf("enter an integer number ");
scanf("%d",&num);
n=num;
sum = 0;
while(n>0)
{
d = n % 10;
sum = sum + d*d*d;
n = n / 10;
}
if (num == sum)
printf("the number = %d is armstrong number\n",num);
else
printf("the number = %d is not armstrong number\n",num);

getch();
}

/*program to find whether the given number is armstrong number


or not using WHILE loop*/
#include<stdio.h>
#include<conio.h>
main()
{
int num,n,sum,d;
clrscr();
num=100;
while (num<=1000)
{
n=num;
sum = 0;
while(n>0)
{
d = n % 10;
sum = sum + d*d*d;
n = n / 10;
}
if (num == sum)
printf("the number = %d is armstrong number\n",num);
num++;
}

Page 4 of 56
Page 5 of 56

getch();
}

/*program to find whether the given number is atomorphic or not


using WHILE loop*/
#include<stdio.h>
#include<conio.h>
main()
{
int n,nsq,m,den,num;
clrscr();

printf("enter an integer ");


scanf("%d",&n);
nsq=n*n;
m=n;
den=1;
while(m>0)
{
den=den*10;
m=m/10;
}

num=nsq%den;

if ( n == num)
printf("%d is atomorphic number ",n);
else
printf("%d is not an atomorphic number ",n);

getch();
}

/*program to find whether the given number is prime or not


using WHILE loop*/
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,prime,rem;
clrscr();
printf("enter an integer ");
scanf("%d",&n);
prime = 1; /*-------- 1-TRUE, 0-FALSE*/
i = 2;
while(i<n)
{
rem=n % i;
if (rem == 0)
{

Page 5 of 56
Page 6 of 56

prime=0;
break;
}
i++;
}
if (prime)
printf("%d is prime number\n",n);
else
printf("%d is not prime number\n",n);
getch();
}

/*program to find all the prime numbers lying between 50 and 100
using WHILE loop*/
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,prime,rem;
clrscr();
n=50;
while (n<=100)
{
prime = 1; /*-------- 1-TRUE, 0-FALSE*/
i = 2;
while(i<n)
{
rem=n % i;
if (rem == 0)
{
prime=0;
break;
}
i++;
}
if (prime)
printf("%d is prime number\n",n);
n++;
}
getch();
}

/*program to print first n fibonacci numbers using WHILE loop*/


#include<stdio.h>
#include<conio.h>
main()
{
int n,previous,present,next,count;
clrscr();
printf("enter howmany fibonacci numbers you want");
scanf("%d",&n);
previous=0;
present=1;

Page 6 of 56
Page 7 of 56

printf("%d %d",previous,present);
count=2;
while(count<n)
{
next=previous+present;
printf(" %d",next);
count++;
previous=present;
present=next;
}
getch();
}

/* program to print a piramid


*
**
***
****
using WHILE loop*/
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,j;
clrscr();
printf("enter n");
scanf("%d",&n);
i=1;
while(i<=n)
{
j=1;
while(j<=i)
{
printf("*");
j++;
}
printf("\n");
i++;
}
getch();
}

/* program to print a piramid


****
***
**

Page 7 of 56
Page 8 of 56

*
using WHILE loop*/
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,j;
clrscr();
printf("enter n");
scanf("%d",&n);
i=1;
while(i<=n)
{
j=1;
while(j <= n-i+1)
{
printf("*");
j++;
}
printf("\n");
i++;
}
getch();
}
/* program to print a piramid
1
22
333
4444
using WHILE loop*/
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,j;
clrscr();
printf("enter n");
scanf("%d",&n);
i=1;
while(i<=n)
{
j=1;
while(j <=i)
{
printf("%d",i);
j++;
}
printf("\n");
i++;
}
getch();
}

Page 8 of 56
Page 9 of 56

/* program to print a piramid


1
121
12321
1234321
using WHILE loop*/
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,j,k;
clrscr();
printf("enter n");
scanf("%d",&n);
i=1;
while(i<=n)
{
j=1;

while(j<=n-i)
{
printf(" ");
j++;
}
k=1;
while(k<=i)
{
printf("%d",k);
k++;
}
k=k-2;
while(k>0)
{
printf("%d",k);
k--;
}
printf("\n");
i++;
}
getch();
}

Page 9 of 56
Page 10 of 56

/* program to print whether the given integer number is perfect or not


using WHILE loop
6 is a perfect number
sum of all its even factors eqals the same
1+2+3=6 */

#include<stdio.h>
#include<conio.h>
main()
{
int n,i,sum,rem;
clrscr();
printf("enter n");
scanf("%d",&n);
sum = 0;
i = 1;
while(i<n)
{
rem=n%i;
if (rem==0)
sum=sum+i;
i++;
}
if (sum==n)
printf("%d is a perfect number \n",n);
else
printf("%d is not a perfect number\n",n);
getch();
}

/* program to find roots of a quadratic equation using WHILE loop*/


#include<stdio.h>
#include <math.h>
#include<conio.h>
main()
{
float a,b,c,disc,x1,x2;
clrscr();
printf("enter coefficients a,b,c");
scanf("%f%f%f",&a,&b,&c);

disc = b*b - 4*a*c;


if (disc==0)
{
x1=(-b)/(2*a);
x2=x1;
printf("roots are real and equal\n");
printf("x1=%f x2=%f\n",x1,x2);
}

Page 10 of 56
Page 11 of 56

else
if (disc>0)
{
x1=(-b+sqrt(disc))/(2*a);
x2=(-b-sqrt(disc))/(2*a);
printf("roots are real and not equal\n");
printf("x1=%f x2=%f\n",x1,x2);

}
else
printf("roots are immaginary\n");
getch();
}

/* program to evaluate the series */


#include<stdio.h>
main()
{
int n,i,den,sign;
float x,sum,num,term;

printf("howmany terms n");


scanf("%d",&n);
printf("enter x");
scanf("%f",&x);
sum=x;
sign=1;
num=x;
den=1;
i=1;
while (i<n)
{
num=num*x*x;
den=den*(2*i)*(2*i+1);
sign=sign*(-1);
term = sign * num / (float)den;
sum=sum+term;
i++;
}
printf("the sum=%f\n",sum);
getch();
}

PROGRAMS USING DO-WHILE LOOP

/* Program to find Largest number from n numbers using DO-WHILE loop*/


#include<stdio.h>
#include<conio.h>
main()
{
int n,i,num,lar;

Page 11 of 56
Page 12 of 56

clrscr();
printf("enter n");
scanf("%d",&n);
i=0;
lar=0;
do
{
printf("enter a number ");
scanf("%d",&num);
if (num>lar)
lar=num;
i++;
}while (i<n);
printf("largest=%d\n",lar);
getch();
}
/*program to find factorial of a given integer using DO-WHILE loop*/
#include<stdio.h>
#include<conio.h>
main()
{
int n,fn,i;
clrscr();
printf("enter an integer ");
scanf("%d",&n);
fn=1;
i=1;
do
{
fn=fn*i;
i++;
}while(i<=n);
printf("factorial of %d is %d\n",n,fn);
getch();
}
/*program to find ncr using DO-WHILE loop*/
#include<stdio.h>
#include<conio.h>
main()
{
int n,r,k,fn,fr,fk,ncr,i;
clrscr();
printf("enter two integers ");
scanf("%d%d",&n,&r);
fn=1;
i=1;
do
{
fn=fn*i;
i++;
}while(i<=n);
printf("factorial of %d is %d\n",n,fn);

fr=1;
i=1;
do
{

Page 12 of 56
Page 13 of 56

fr=fr*i;
i++;
}while(i<=r);
printf("factorial of %d is %d\n",r,fr);

k = n - r;
fk = 1;
i = 1;
do
{
fk=fk*i;
i++;
}while(i <= k);
printf("factorial of %d is %d\n",k,fk);

ncr=fn/(fr*fk);
printf("ncr= %d\n",ncr);
getch();
}
/*program to find GCD of two integers using DO-WHILE loop*/
#include<stdio.h>
#include<conio.h>
main()
{
int m,n,gcd,temp;
clrscr();
printf("enter two integers ");
scanf("%d%d",&m,&n);

do
{
temp = m % n;
m = n;
n = temp;
}while(n>0);
gcd=m;
printf("gcd = %d\n",gcd);
getch();
}

/*program to find reverse number of a given integer number


using DO-WHILE loop*/
#include<stdio.h>
#include<conio.h>
main()
{
int num,revnum,d;
clrscr();
printf("enter an integer number ");
scanf("%d",&num);
revnum = 0;
do
{
d = num % 10;
revnum = revnum * 10 + d;
num = num / 10;

Page 13 of 56
Page 14 of 56

}while(num>0);
printf("the reverse number = %d\n",revnum);
getch();
}

/*program to find sum of digits of a given integer number


using DO-WHILE loop*/
#include<stdio.h>
#include<conio.h>
main()
{
int num,sum,d;
clrscr();
printf("enter an integer number ");
scanf("%d",&num);
sum = 0;
do
{
d = num % 10;
sum = sum + d;
num = num / 10;
}while(num>0);
printf("the sum of digits = %d\n",sum);
getch();
}

/*program to find whether the given number is armstrong number


or not using DO-WHILE loop*/
#include<stdio.h>
#include<conio.h>
main()
{
int num,n,sum,d;
clrscr();
printf("enter an integer number ");
scanf("%d",&num);
n=num;
sum = 0;
do
{
d = n % 10;
sum = sum + d*d*d;
n = n / 10;
}while(n>0);
if (num == sum)
printf("the number = %d is an armstrong number\n",num);
else
printf("the number = %d is not armstrong number\n",num);

getch();
}
/*program to find whether the given number is atomorphic or not
using Do-WHILE loop*/
#include<stdio.h>
#include<conio.h>
main()

Page 14 of 56
Page 15 of 56

{
int n,nsq,m,den,num;
clrscr();

printf("enter an integer ");


scanf("%d",&n);
nsq=n*n;
m=n;
den=1;
do
{
den=den*10;
m=m/10;
}while(m>0);

num=nsq%den;

if ( n == num)
printf("%d is atomorphic number ",n);
else
printf("%d is not an atomorphic number ",n);

getch();
}

/*program to find whether the given number is prime or not


using DO-WHILE loop*/
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,prime,rem;
clrscr();
printf("enter an integer ");
scanf("%d",&n);
prime = 1; /*-------- 1-TRUE, 0-FALSE*/
i = 2;
do
{
rem=n % i;
if (rem == 0)
{
prime=0;
break;
}
i++;
}while(i<n);
if (prime)
printf("%d is prime number\n",n);
else
printf("%d is not prime number\n",n);
getch();
}
/*program to find all the prime numbers lying between 50 and 100
using nested DO-WHILE loops*/

Page 15 of 56
Page 16 of 56

#include<stdio.h>
#include<conio.h>
main()
{
int n,i,prime,rem;
clrscr();
n=50;
do
{
prime = 1; /*-------- 1-TRUE, 0-FALSE*/
i = 2;
do
{
rem=n % i;
if (rem == 0)
{
prime=0;
break;
}
i++;
}while(i<n);
if (prime)
printf("%d is prime number\n",n);
n++;
}while (n<=100);
getch();
}

/*program to print first n fibonacci numbers using


DO-WHILE loop*/
#include<stdio.h>
#include<conio.h>
main()
{
int n,previous,present,next,count;
clrscr();
printf("enter howmany fibonacci numbers you want");
scanf("%d",&n);
previous=0;
present=1;
printf("%d %d",previous,present);
count=2;

do
{
next=previous+present;
printf(" %d",next);
count++;
previous=present;
present=next;
}while(count<n);
getch();
}

/* program to print a piramid

Page 16 of 56
Page 17 of 56

*
**
***
**** using DO-WHILE loop*/
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,j;
clrscr();
printf("enter n");
scanf("%d",&n);
i=1;
do
{
j=1;
do
{
printf("*");
j++;
} while(j<=i);
printf("\n");
i++;
}while(i<=n);
getch();
}

/* program to print a piramid


****
***
**
*
using DO-WHILE loop*/
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,j;
clrscr();
printf("enter n");
scanf("%d",&n);
i=1;
do
{
j=1;
do
{
printf("*");
j++;
}while(j <= n-i+1);
printf("\n");
i++;
}while(i<=n);
getch();
}

Page 17 of 56
Page 18 of 56

/* program to print a piramid


1
22
333
4444
using DO-WHILE loop*/
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,j;
clrscr();
printf("enter n");
scanf("%d",&n);
i=1;
do
{
j=1;
do
{
printf("%d",i);
j++;
}while(j <=i);
printf("\n");
i++;
}while(i<=n);
getch();
}

/* program to print a piramid


1
121
12321
1234321
using DO-WHILE loop*/
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,j,k;
clrscr();
printf("enter n");
scanf("%d",&n);
i=1;
do
{
j=0;
do
{
printf(" ");
j++;
}while(j<=n-i);
k=1;
do
{

Page 18 of 56
Page 19 of 56

printf("%d",k);
k++;
}while(k<=i);
k=k-2;
if (k>0)
do
{
printf("%d",k);
k--;
}while(k>0);
printf("\n");
i++;
}while(i<=n);
getch();
}

/* program to print whether the given integer number is perfect or not


using DO-WHILE loop
6 is a perfect number
sum of all its even factors eqals the same
1+2+3=6 */
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,sum,rem;
clrscr();
printf("enter n");
scanf("%d",&n);
sum = 0;
i = 1;
do
{
rem=n%i;
if (rem==0)
sum=sum+i;
i++;
}while(i<n);
if (sum==n)
printf("%d is a perfect number \n",n);
else
printf("%d is not a perfect number\n",n);
getch();
}
/* program to evaluate the series */
#include<stdio.h>
main()
{
int n,i,den,sign;
float x,sum,num,term;

printf("howmany terms n");


scanf("%d",&n);
printf("enter x");
scanf("%f",&x);
sum=x;

Page 19 of 56
Page 20 of 56

sign=1;
num=x;
den=1;
i=1;
do
{
num=num*x*x;
den=den*(2*i)*(2*i+1);
sign=sign*(-1);
term = sign * num / (float)den;
sum=sum+term;
i++;
}while (i<n);
printf("the sum=%f\n",sum);
getch();
}
/*program to find LCM of two integers using DO-WHILE loop*/
#include<stdio.h>
#include<conio.h>
main()
{
int m,n,a,b,gcd,temp,lcm;
clrscr();
printf("enter two integers ");
scanf("%d%d",&m,&n);
a=m;
b=n;
do
{
temp = m % n;
m = n;
n = temp;
}while(n>0);
gcd=m;
lcm=a*b/gcd;
printf("gcd = %d\n",gcd);
printf("lcm = %d\n",lcm);

getch();
}

Page 20 of 56
Page 21 of 56

PROGRAMS USING FOR LOOP

/* Program to find Largest number from n numbers using FOR loop*/


#include<stdio.h>
#include<conio.h>
main()
{
int n,i,num,lar;
clrscr();
printf("enter n");
scanf("%d",&n);
lar=0;
for(i=1;i<=n;i++)
{
printf("enter a number ");
scanf("%d",&num);
if (num>lar)
lar=num;
}
printf("largest number = %d\n",lar);
getch();
}
/* Program to find Largest number and second largest number
from n numbers using FOR loop*/
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,num,lar,slar;
clrscr();
printf("enter n");
scanf("%d",&n);
lar=0;
slar=0;
for(i=1;i<=n;i++)
{
printf("enter a number ");
scanf("%d",&num);
if (num>lar)
{
slar=lar;
lar=num;
}
else

Page 21 of 56
Page 22 of 56

if (num>slar)
slar =num;
}
printf("Largest number = %d\n",lar);
printf("Second largest number = %d\n",slar);
getch();
}
/*program to find factorial of a given integer using FOR loop*/
#include<stdio.h>
#include<conio.h>
main()
{
int n,fn,i;
clrscr();
printf("enter an integer ");
scanf("%d",&n);
fn=1;
for(i=1;i<=n;i++)
fn=fn*i;
printf("factorial of %d is %d\n",n,fn);
getch();
}

/*program to find ncr using FOR loops*/


#include<stdio.h>
#include<conio.h>
main()
{
int n,r,k,fn,fr,fk,ncr,i;
clrscr();
printf("enter two integers ");
scanf("%d%d",&n,&r);
fn=1;
for(i=1;i<=n;i++)
fn=fn*i;
printf("factorial of %d is %d\n",n,fn);

fr=1;
for(i=1;i<=r;i++)
fr=fr*i;
printf("factorial of %d is %d\n",r,fr);

k = n - r;

fk = 1;
for(i=1;i<=k;i++)
fk=fk*i;
printf("factorial of %d is %d\n",k,fk);

ncr=fn/(fr*fk);
printf("ncr= %d\n",ncr);
getch();
}

Page 22 of 56
Page 23 of 56

/*program to find whether the given number is prime or not


using FOR loop*/
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,prime,rem;
clrscr();
printf("enter an integer ");
scanf("%d",&n);
prime = 1; /*-------- 1-TRUE, 0-FALSE*/
for(i=2;i<n;i++)
{
rem=n % i;
if (rem == 0)
{
prime=0;
break;
}
}
if (prime)
printf("%d is prime number\n",n);
else
printf("%d is not prime number\n",n);
getch();
}

/*program to find all the prime numbers lying between 50 and 100
using nested FOR loops*/
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,prime,rem;
clrscr();
for(n=50;n<=100;n++)
{
prime = 1; /*-------- 1-TRUE, 0-FALSE*/
for(i=2;i<=n/2;i++)
{
rem=n % i;
if (rem == 0)
{
prime=0;
break;
}
}
if (prime)
printf("%d is prime number\n",n);
}
getch();
}

/*program to print first n fibonacci numbers using FOR loop*/

Page 23 of 56
Page 24 of 56

#include<stdio.h>
#include<conio.h>
main()
{
int n,previous,present,next,count;
clrscr();
printf("enter howmany fibonacci numbers you want");
scanf("%d",&n);
previous=0;
present=1;
printf("%d %d",previous,present);
for(count=3;count<=n;count++)
{
next=previous+present;
printf(" %d",next);
previous=present;
present=next;
}
getch();
}

/* program to print a piramid


*
**
***
**** using FOR loop*/
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,j;
clrscr();
printf("enter n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
printf("*");
printf("\n");
}
getch();
}

/* program to print a piramid


****
***
**
*
using FOR loop*/
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,j;
clrscr();

Page 24 of 56
Page 25 of 56

printf("enter n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i+1;j++)
printf("*");
printf("\n");
}
getch();
}

/* program to print a piramid


1
22
333
4444
using FOR loop*/
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,j;
clrscr();
printf("enter n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
printf("%d",i);
printf("\n");
}
getch();
}

/* program to print a piramid


1
121
12321
1234321
using FOR loop*/
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,j,k;
clrscr();
printf("enter n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
printf(" ");

for(k=1;k<=i;k++)
printf("%d",k);

Page 25 of 56
Page 26 of 56

for(k=k-2;k>0;k--)
printf("%d",k);
printf("\n");
}
getch();
}

/* program to evaluate the series using FOR loop*/


#include<stdio.h>
main()
{
int n,i,den,sign;
float x,sum,num,term;

printf("howmany terms n");


scanf("%d",&n);
printf("enter x");
scanf("%f",&x);
sum=x;
sign=1;
num=x;
den=1;
for(i=2;i<=n;i++)
{
num=num*x*x;
den=den*(2*i-2)*(2*i-1);
sign=sign*(-1);
term = sign * num / (float)den;
sum=sum+term;
}
printf("the sum=%f\n",sum);
getch();
}

/* program to create a single dimensional array


find sum of the elements */
#include<stdio.h>
main()
{
int a[10],i,n,sum;
clrscr();
printf("enter size" );
scanf("%d",&n);
sum=0;
for (i=0;i<n;i++)
{
printf("enter array element ");
scanf("%d",&a[i]);
sum=sum+a[i];
}

Page 26 of 56
Page 27 of 56

printf("the array elements are\n");


for (i=0;i<n;i++)
printf("%d\n",a[i]);
printf("the sum = %d\n",sum);
getch();
}

/*program to create a single dimensional array find large and small of the
array elements */
#include<stdio.h>
main()
{
int a[10],i,n,large,small;
clrscr();
printf("enter size" );
scanf("%d",&n);
for (i=0;i<n;i++)
{
printf("enter array element ");
scanf("%d",&a[i]);
}
large=a[0];
small=a[0];
for (i=1;i<n;i++)
{
if(a[i]>large)
large=a[i];
if(a[i]<small)
small=a[i];
}
printf("the array elements are\n");
for (i=0;i<n;i++)
printf("%d\n",a[i]);
printf("the largest = %d\n",large);
printf("the smallest = %d\n",small);
getch();
}
/*program to create a single dimensional array find standard
deviation */
#include<stdio.h>
#include<math.h>
main()
{
int a[10],i,n;
float sum,mean,sd;
clrscr();

Page 27 of 56
Page 28 of 56

printf("enter size" );
scanf("%d",&n);
for (i=0;i<n;i++)
{
printf("enter array element ");
scanf("%d",&a[i]);
}
sum=0;
for (i=0;i<n;i++)
sum=sum+(float)a[i];
mean = sum / (float)n;
sum=0;
for (i=0;i<n;i++)
{
sum=sum+((float)a[i] - mean) * ((float)a[i] - mean);
}
sd=sqrt(sum/(float)n);
printf("the array elements are\n");
for (i=0;i<n;i++)
printf("%d\n",a[i]);
printf("mean = %f\n",mean);
printf("the standard deviation = %f\n",sd);
getch(); }
/* program to create a single dimensional array and arrange the
elements in sorted order */
#include<stdio.h>
#include<math.h>
main()
{
int a[10],i,j,n,temp;
clrscr();
printf("enter size" );
scanf("%d",&n);

for (i=0;i<n;i++)
{
printf("enter array element ");
scanf("%d",&a[i]);
}
for (i=0;i<n-1;i++)
for (j=0;j<n-i-1;j++)
if (a[j] > a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
printf("the sorted array elements are\n");
for (i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
}
/* program to create a single dimensional array and search for
an element -- linear search Technique */
#include<stdio.h>
#include<math.h>

Page 28 of 56
Page 29 of 56

main()
{
int a[10],i,n,x,pos,found;
clrscr();
printf("enter size" );
scanf("%d",&n);

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

printf("enter the target element ");


scanf("%d",&x);
found=0; /*1-found 0-not found*/
for (i=0;i<n;i++)
if (a[i] == x)
{
found=1;
pos=i;
break;
}
if (found)
printf("%d found at position %d\n",x,pos);
else
printf("%d not found\n",x);

getch();
}
/* program to create a single dimensional array and search for
an element -- binary search Technique */
#include<stdio.h>
#include<math.h>
main()
{
int a[10],i,n,x,low,high,mid,found;
clrscr();
printf("enter size" );
scanf("%d",&n);
printf("enter array elements in ascending order\n");
for (i=0;i<n;i++)
scanf("%d",&a[i]);
printf("enter the target element ");
scanf("%d",&x);
found=0; /*1-found 0-not found*/
low=0;
high=n-1;
while((low<=high)&&(!found))
{
mid = (low + high)/2;
if (a[mid] == x)
{
found=1;
break;
}
else

Page 29 of 56
Page 30 of 56

if (x>a[mid])
low=mid+1;
else
high=mid-1;
}
if (found)
printf("%d found at position %d\n",x,mid);
else
printf("%d not found\n",x);

getch();
}

/*program to convert a decimal number into its binary


equivalent*/
#include<stdio.h>
main()
{
int n,a[10],i,rem;
clrscr();
printf("enter a decimal number:" );
scanf("%d",&n);
i=0;
while (n>0)
{
rem=n%2;
a[i]=rem;
n=n/2;
i++;
}
printf("the binary equivalent =");
for(--i;i>=0;i--)
printf("%d",a[i]);

getch();
}

/*program to convert a decimal number into its Hexa decimal


equivalent*/
#include<stdio.h>
main()
{
static char hex[16]={'0','1','2','3','4','5','6','7','8','9',
'A','B','C','D','E','F'};
int n,a[10],i,rem;
clrscr();
printf("enter a decimal number:" );
scanf("%d",&n);
i=0;
while (n>0)
{
rem=n%16;
a[i]=rem;
n=n/16;
i++;
}

Page 30 of 56
Page 31 of 56

printf("the Hexa decimal equivalent =");


for(--i;i>=0;i--)
printf("%c",hex[a[i]]);
getch();
}

/*program to reversing the elements of an array*/


#include<stdio.h>
main()
{
int n,a[10],i,temp;
clrscr();
printf("enter size of the array:" );
scanf("%d",&n);
printf("enter the elements of an array\n");
for (i=0;i<n;i++)
scanf("%d",&a[i]);

for(i=0;i<n/2;i++)
{
temp=a[i];
a[i]=a[n-i-1];
a[n-i-1]=temp;
}

printf("the reverse array\n");


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

/*program to rotate the elements of an array 1 position


to its right*/

#include<stdio.h>
main()
{
int n,a[10],i,temp;
clrscr();
printf("enter size of the array:" );
scanf("%d",&n);
printf("enter the elements of an array\n");
for (i=0;i<n;i++)
scanf("%d",&a[i]);

Page 31 of 56
Page 32 of 56

printf("the array before rotating 1 position to its right\n");


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

temp=a[n-1];
for(i=n-1;i>0;i--)
a[i]=a[i-1];
a[0]=temp;
printf("\nthe array after rotating 1 position to its right\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
getch();
}

/*program to rotate the elements of an array m positions to its


right*/
#include<stdio.h>
main()
{
int n,a[10],i,j,temp,m;
clrscr();
printf("enter a size of the array:" );
scanf("%d",&n);
printf("enter the elements of an array\n");
for (i=0;i<n;i++)
scanf("%d",&a[i]);
printf("howmany positions you want to rotate:");
scanf("%d",&m);
printf("the array before rotating \n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
for(j=1;j<=m;j++)
{
temp=a[n-1];
for(i=n-1;i>0;i--)
a[i]=a[i-1];
a[0]=temp;
}
printf("\nthe array after rotating 1 position to its right\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
getch();
}

TWO-DIMENSIONAL ARRAYS:

So far we have looked at arrays with only one dimension. It is also possible for
arrays to have two or more dimensions. The two dimensional array is also called a
matrix.

Page 32 of 56
Page 33 of 56

Consider the following data table

Sub1 sub2 sub3 sub4

Student1 10 20 30 40

Student2 5 7 8 15

Student3 3 2 4 50
34
The table contains a total of 12 values, i.e in each line. We can think of this table as a
matrix consisting of 3 rows and 4 columns.
Each row represents the marks obtained in 4 subjects by a particular student. Each
column represents the marks obtained in a particular subject.
In mathematics, we represent a particular value in a matrix by using two subscripts,
such as vij. Here v denotes the entire matrix and vij refers to the value in the ith row and jth
column.
For example in the above table v23 refers to the value 50.
C allows us to define such tables of items by using two-dimensional arrays, the above
table can be defined in C as
Int v[3][4];
General form:
Type array_name [row-size] [col-size];
Two-dimensional arrays are stored in memory as shown below.

col 0 col 1 col 2

[0][0] [0][1] [0][2]


row 0

[1][0] [1][1] [1][2]


row 1

[2][0] [2][1] [2][2]


row 2

[3][0] [3][1] [3][2]


row 3

Unlike most other languages, which use one pair of parenthesis with commas to
separate array sizes, C places each size in its own set of brackets

Page 33 of 56
Page 34 of 56

Initializing two dimensional arrays:


/*program to create a two dimensional array of size mxn
and print it in matrix form*/
#include<stdio.h>
main()
{
int a[5][5],m,n,i,j;
clrscr();
printf("enter a size of the array:" );
scanf("%d%d",&m,&n);
printf("enter the elements of two dimensional array\n");
for (i=0;i<m;i++)
for (j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("The two dimensional array\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
getch();
}

/* program to add two matrices */


#include<stdio.h>
main()
{
int a[5][5],b[5][5],c[5][5],m,n,i,j;
clrscr();
printf("enter a size of the array :" );
scanf("%d%d",&m,&n);
printf("enter the elements of matrix A\n");
for (i=0;i<m;i++)
for (j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("enter the elements of matrix B\n");
for (i=0;i<m;i++)
for (j=0;j<n;j++)
scanf("%d",&b[i][j]);
for (i=0;i<m;i++)
for (j=0;j<n;j++)
c[i][j]=a[i][j]+b[i][j];
printf("The two dimensional array\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)

Page 34 of 56
Page 35 of 56

printf("%d ",c[i][j]);
printf("\n");
}
getch();
}

/*program to multiply two matrices matrix A of size M N


matrix B of size P Q Resultant matrix of size M x Q */
#include<stdio.h>
main()
{
int a[5][5],b[5][5],c[5][5],m,n,p,q,i,j,k;
clrscr();
printf("enter a size of the array A:" );
scanf("%d%d",&m,&n);
printf("enter the elements of matrix A\n");
for (i=0;i<m;i++)
for (j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("enter a size of the array B:" );
scanf("%d%d",&p,&q);
printf("enter the elements of matrix B\n");
for (i=0;i<p;i++)
for (j=0;j<q;j++)
scanf("%d",&b[i][j]);
if (n==p)
{
for (i=0;i<m;i++) /*initialisation of C*/
for (j=0;j<q;j++)
c[i][j]=0;
for (i=0;i<m;i++)
for (j=0;j<n;j++)
for (k=0;k<q;k++)
c[i][k]=c[i][k]+a[i][j]*b[j][k];
printf("The Resultant array\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
printf("%d ",c[i][j]);
printf("\n");
}
}
else
printf("Matrix multiplication is not possible\n");
getch();
}

Page 35 of 56
Page 36 of 56

/* program to interchange two rows of a matrix */


#include<stdio.h>
main()
{
int a[5][5],m,n,i,j,r1,r2,temp;
clrscr();
printf("enter a size of the array A:" );
scanf("%d%d",&m,&n);
printf("enter the elements of matrix A\n");
for (i=0;i<m;i++)
for (j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("enterthe two rows:" );
scanf("%d%d",&r1,&r2);
for (i=0;i<n;i++)
{
temp=a[r1][i];
a[r1][i]=a[r2][i];
a[r2][i]=temp;
}
printf("The matrix after interchange\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
getch();
}

/* program to interchange two columns of a matrix */


#include<stdio.h>
main()
{
int a[5][5],m,n,i,j,c1,c2,temp;
clrscr();
printf("enter a size of the array A:" );
scanf("%d%d",&m,&n);
printf("enter the elements of matrix A\n");
for (i=0;i<m;i++)
for (j=0;j<n;j++)

Page 36 of 56
Page 37 of 56

scanf("%d",&a[i][j]);
printf("enterthe two columns:" );
scanf("%d%d",&c1,&c2);
for (i=0;i<m;i++)
{
temp=a[i][c1];
a[i][c1]=a[i][c2];
a[i][c2]=temp;
}
printf("The matrix after interchange\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d ",a[i][j]);
printf("\n");
}

getch();
}

/*program to create pascals triangle


1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
if we denote rows by i and columns by j then any element in the
triangle is given by
p[i][j]=p[i-1][j-1]+p[i-1][j]
*/

#include<stdio.h>
main()
{
int a[10][15],i,j;
clrscr();

for (i=0;i<10;i++)
a[i][0]=1;

for (j=1;j<15;j++)
a[0][j]=0;

for (i=1;i<10;i++)
for (j=1;j<15;j++)
a[i][j]=a[i-1][j-1]+a[i-1][j];

printf("The pascals triangle\n");


for(i=0;i<10;i++)
{
for(j=0;j<15;j++)
if (a[i][j]!=0)
printf("%d ",a[i][j]);

Page 37 of 56
Page 38 of 56

printf("\n");
}

getch();
}

/*Program to sort a given row of a matrix*/


#include <stdio.h>
main()
{
int a[5][5],m,n,i,j,r,temp;
clrscr();
printf("enter size of the array");
scanf("%d%d",&m,&n);
printf("enter array elements ");
for(i=0;i<m;i++)
for (j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("which row u want to sort ");
scanf("%d",&r);
/* sorting the elements of a row */
for(i=0;i<n-1;i++)
for (j=0;j<n-i-1;j++)
if ( a[r][j] > a[r][j+1] )
{
temp=a[r][j];
a[r][j]=a[r][j+1];
a[r][j+1]=temp;
}
printf("The array elements \n");
for(i=0;i<m;i++)
{
for (j=0;j<n;j++)
printf("%4d",a[i][j]);
printf("\n");
}

getch();

Programs using Functions


/* program to add two numbers using a function */
#include<stdio.h>
int adding(x,y)

Page 38 of 56
Page 39 of 56

int x,y;
{
int res;
res = x+y;
return(res);
}
main()
{
int a,b,c;
clrscr();
printf("enter a,b");
scanf("%d%d",&a,&b);
c=adding(a,b); /* Function Call */
printf("the sum of %d and %d is %d\n",a,b,c);
getch();
}
/*program to find factorial of a given number using a function*/
#include<stdio.h>
int factorial(x)
int x;
{
int i,f;
f=1;
for(i=1;i<=x;i++)
f=f*i;
return(f);
}
main()
{
int n,fn;
clrscr();
printf("enter n");
scanf("%d",&n);
fn=factorial(n); /* Function Call */
printf("the factorial %d is %d\n",n,fn);
getch();
}
/*program to find GCD of two numbers using a function*/
#include<stdio.h>
int gcd(u,v)
int u,v;
{
int temp;
while(v>0)
{
temp=u%v;
u=v;
v=temp;
}
return(u);
}

Page 39 of 56
Page 40 of 56

main()
{
int m,n,g;
clrscr();
printf("enter m and n");
scanf("%d%d",&m,&n);
g=gcd(m,n); /* Function Call */
printf("the gcd of %d and %d is %d\n",m,n,g);
getch();
}

/*program to find largest of three numbers using a function*/


#include<stdio.h>
int large(x,y,z)
int x,y,z;
{
int big;
big=x;
if (y>big)
big=y;
if(z>big)
big=z;
return(big);
}
main()
{
int a,b,c,l;
clrscr();
printf("enter a,b,c");
scanf("%d%d%d",&a,&b,&c);
l=large(a,b,c); /*-------------- Function Call */
printf("the largest number is %d\n",l);
getch();
}

/*program to find largest of three numbers using a function*/


#include<stdio.h>
int large(x,y,z)
int x,y,z;
{
int big;
big=x;
if (y>big)
big=y;
if(z>big)
big=z;
return(big);
}
main()
{
int a,b,c,l;
clrscr();
printf("enter a,b,c");
scanf("%d%d%d",&a,&b,&c);
l=large(a,b,c); /*-------------- Function Call */

Page 40 of 56
Page 41 of 56

printf("the largest number is %d\n",l);


getch();
}

/* Program to find whether the given number is prime or not


using a function which returns 1 if its argument is a prime
number and returns 0 otherwise
*/
#include<stdio.h>
int isprime(x)
int x;
{
int prime,i,rem;
prime = 1; /*------1-True,0-False*/
i=2;
while (i<x)
{
rem = x % i;
if (rem==0)
{
prime=0;
break;
}
i++;
}
return(prime);
}

main()
{
int n,p;
clrscr();
printf("enter n");
scanf("%d",&n);
p=isprime(n); /*-------------- Function Call */
if (p==1)
printf("%d is prime\n",n);
else
printf("%d is not prime\n",n);
getch();
}
/*Program to find sum of digits of an integer using a function */
#include<stdio.h>
int summing(x)
int x;
{
int digit,sum=0;
while (x>0)
{
digit = x % 10;
sum=sum + digit;
x=x/10;
}
return(sum);

Page 41 of 56
Page 42 of 56

}
main()
{
int n,s;
clrscr();
printf("enter a number");
scanf("%d",&n);
s=summing(n);
printf("sum of digits of %d is %d\n",n,s);
getch();
}

/* Program to find whether the given integer number is armstrong


or not with the use of a function */
#include<stdio.h>
int summing(x)
int x;
{
int digit,sum=0;
while (x>0)
{
digit = x % 10;
sum=sum + digit*digit*digit;
x=x/10;
}
return(sum);
}
main()
{
int n,s;
clrscr();
printf("enter a number");
scanf("%d",&n);
s=summing(n);
if (s==n)
printf("%d is an armstrong number\n",n);
else
printf("%d is not an armstrong number\n",n);
getch();
}
/* Program to print all prime numbers lying between 50 and 100
using a function which returns 1 if its argument is a prime
number and returns 0 otherwise*/
#include<stdio.h>
int isprime(x)
int x;
{
int prime,i,rem;
prime = 1; /*------1-True,0-False*/
i=2;
while (i<x)
{
rem = x % i;
if (rem==0)
{
prime=0; break;

Page 42 of 56
Page 43 of 56

}
i++;
}
return(prime);
}
main()
{
int n,p;
clrscr();
n=50;
while (n<=100)
{
p=isprime(n); /*-------------- Function Call */
if (p==1)
printf("%d is prime\n",n);
n++;
}
getch();
}
/* Program to print all the armstrong numbers lying between
100 and 1000 using a function*/
#include<stdio.h>
int summing(x)
int x;
{
int digit,sum=0;
while (x>0)
{
digit = x % 10;
sum=sum + digit*digit*digit;
x=x/10;
}
return(sum);
}
main()
{
int n,s;
clrscr();
n=100;
while (n<=1000)
{
s=summing(n);
if (s==n)
printf("%d is an armstrong number\n",n);
n++;
}
getch();
}
/*program to find whether the given number is atomorphic or not
using a function*/
#include<stdio.h>
#include<conio.h>
int isatomorphic(x)
int x;
{
int xsq,y,den,num;

Page 43 of 56
Page 44 of 56

xsq=x*x;
y=x;
den=1;
while(y>0)
{
den=den*10; y=y/10;
}
num=xsq%den;
if (x==num)
return(1);
else
return(0);
}
main()
{
int n,res;
clrscr();
printf("enter an integer ");
scanf("%d",&n);
res=isatomorphic(n);
if (res)
printf("%d is atomorphic number ",n);
else
printf("%d is not an atomorphic number ",n);
getch();
}/* program pass a single dimensional array to a function to find
sum.
return sum to the calling function */
#include<stdio.h>
int addition(x,m)
int x[],m;
{
int i,s=0;
for(i=0;i<m;i++)
s=s+x[i];
return(s);
}
main()
{
int a[10],i,n,sum;
clrscr();
printf("enter size" );
scanf("%d",&n);
for (i=0;i<n;i++)
{
printf("enter array element ");
scanf("%d",&a[i]);
}
printf("the array elements are\n");
for (i=0;i<n;i++)
printf("%d\n",a[i]);
sum=addition(a,n);
printf("the sum of array elements = %d\n",sum);
getch();
}
/* program to find largest element from an array using a function*/
#include<stdio.h>

Page 44 of 56
Page 45 of 56

int largest(x,m)
int x[],m;
{
int i,l=0;
for (i=0;i<m;i++)
if(x[i]>l)
l=x[i];
return (l);
}
main()
{
int a[10],i,n,large;
clrscr();
printf("enter size" );
scanf("%d",&n);
for (i=0;i<n;i++)
{
printf("enter array element ");
scanf("%d",&a[i]);
}
large=largest(a,n);
printf("the largest = %d\n",large);
getch();
}

/* program to create a single dimensional array find standard


deviation */
#include<stdio.h>
#include<math.h>
float mean(a,n)
int a[],n;
{
int i;
float sum=0,m;
for (i=0;i<n;i++)
sum=sum+(float)a[i];
m = sum / (float)n;
return(m);
}
float standard(a,n)
int a[],n;
{
int i;
float sum,sd,m;
m=mean(a,n);
sum=0;
for (i=0;i<n;i++)
{
sum=sum+((float)a[i] - m) * ((float)a[i] - m);
}

Page 45 of 56
Page 46 of 56

sd=sqrt(sum/(float)n);
return(sd);
}
main()
{
int a[10],i,n;
float sum,mean,sd;
clrscr();
printf("enter size" );
scanf("%d",&n);
for (i=0;i<n;i++)
{
printf("enter array element ");
scanf("%d",&a[i]);
}

printf("the array elements are\n");


for (i=0;i<n;i++)
printf("%d\n",a[i]);
sd=standard(a,n);
printf("the standard deviation = %f\n",sd);
getch();
}

/*program to arrange array elements in sorted order using a function


*/
#include<stdio.h>
#include<math.h>
sorting(a,n)
int a[],n;
{
int i,j,temp;

for (i=0;i<n-1;i++)
for (j=0;j<n-i-1;j++)
if (a[j] > a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}

Page 46 of 56
Page 47 of 56

}
main()
{
int a[10],i,j,n,temp;
clrscr();
printf("enter size" );
scanf("%d",&n);

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

sorting(a,n); /* function call */

printf("the sorted array elements are\n");


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

/* program to create a single dimensional array and search for an


element -- linear search
*/#include<stdio.h>#include<math.h>linearsearch(a,n,x)int a[],n,x;{
int found,i,pos; found=0; /*1-found 0-not found*/
for (i=0;i<n;i++)
if (a[i] == x)
{
found=1;
pos=i;
break;

Page 47 of 56
Page 48 of 56

}
if (found)
printf("%d found at position %d\n",x,pos);
else
printf("%d not found\n",x);

}
main()
{
int a[10],i,n,target;
clrscr();
printf("enter size" );
scanf("%d",&n);

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

printf("enter the target element ");


scanf("%d",&target);

linearsearch(a,n,target);

getch();
}

/* binary search using function */


#include<stdio.h>
#include<math.h>
binarysearch(a,n,tar)
int a[],n,tar;
{
int low,high,mid,found;
found=0; /*1-found 0-not found*/

Page 48 of 56
Page 49 of 56

low=0;
high=n-1;
while((low<=high)&&(!found))
{
mid = (low + high)/2;
if (a[mid] == tar)
{
found=1;
break;
}
else
if (tar > a[mid])
low=mid+1;
else
high=mid-1;
}
if (found)
printf("%d found at position %d\n",tar,mid);
else
printf("%d not found\n",tar);

}
main()
{
int a[10],i,n,target;
clrscr();
printf("enter size" );
scanf("%d",&n);
printf("enter array elements in ascending order\n");
for (i=0;i<n;i++)
scanf("%d",&a[i]);
printf("enter the target element ");
scanf("%d",&target);
binarysearch(a,n,target);
getch();
}

/*program to arrange strings sorted order using a function */


#include<stdio.h>

Page 49 of 56
Page 50 of 56

#include<string.h>

void sorting(a,n)
char a[10][10];
int n;
{
int i,j;
char temp[10];

for (i=0;i<n-1;i++)
for (j=0;j<n-i-1;j++)
if (strcmp(a[j],a[j+1])>0)
{
strcpy(temp,a[j]);
strcpy(a[j],a[j+1]);
strcpy(a[j+1],temp);
}
}
main()
{
char a[10][10];
int i,j,n;
clrscr();
printf("Howmany number of strings" );
scanf("%d",&n);

for (i=0;i<n;i++)
{
printf("enter array element ");
scanf("%s",a[i]);
}

sorting(a,n); /* function call */

printf("the sorted array elements are\n");


for (i=0;i<n;i++)
printf("%s\n",a[i]);
getch();
}

Page 50 of 56
Page 51 of 56

RECURSION

Recursion is a special case of process, where a function calls itself.


A function is called recursive if a statement within the body of a function calls the
same function.
factorial(x)
int x;
{
if (x = =1)
return(1);
else
return(x * factorial(x-1));
}

When writing recursive functions, you must have an If stmt some where in the
recursive function to force the function to return with out recursive call being executed. If
you do not do this and you call the function, you will fall in an indefinite loop, and will
never return from the called function.

/*program to find factorial of a given number using a Recursive


function*/
#include<stdio.h>
int factorial(x)
int x;
{
if (x<=1)
return(1);
else
return(x*factorial(x-1));
}
main()
{
int n,fn;
clrscr();
printf("enter n");
scanf("%d",&n);
fn=factorial(n); /* Function Call */
printf("the factorial %d is %d\n",n,fn);
getch();
}
In case the value of n is 4, main() would call factorial() with 4
as its actual argument, and factorial() will send back the
computed value. But before sending the computed value,

Page 51 of 56
Page 52 of 56

factorial() calls factorial() and waits for a value to be


returned.

factorial(4) returns 4*factorial(3) 4*6 = 24


factorial(3) returns 3*factorial(2) 3*2 = 6
factorial(2) returns 2*factorial(1) 2*1 = 2
factorial(1) returns 1 Back substitution

/*program to find GCD of two numbers using a Recursive function*/


#include<stdio.h>
int gcd(int u,int v)
{
if (v==0)
return(u);
else
return(gcd(v,u%v));
}
main()
{
int m,n,g;
clrscr();
printf("enter m and n");
scanf("%d%d",&m,&n);
g=gcd(m,n); /*---------- Function Call */
printf("the gcd of %d and %d is %d\n",m,n,g);
getch();
}
/*program to find ncr using a Recursive Function */
#include<stdio.h>
int ncr(n,r)
int n,r;
{
if (r==1)
return(n);
else
if (r==0)
return(1);
else
if (n==r)
return(1);
else
return(ncr(n-1,r)+ncr(n-1,r-1));
}
main()
{
int n,r,res;
clrscr();
printf("enter n,r");
scanf("%d%d",&n,&r);
res=ncr(n,r); /*-------- Function Call */
printf("the ncr= %d\n",res);

Page 52 of 56
Page 53 of 56

getch();
}
/* Program to find sum of digits of an integer using a Recursive
function */
#include<stdio.h>
int digitsum(x)
int x;
{
if (x<9)
return(x);
else
return( (x%10) + digitsum(x/10) );
}
main()
{
int n,s;
clrscr();
printf("enter a number");
scanf("%d",&n);
s=digitsum(n);
printf("sum of digits of %d is %d\n",n,s);
getch();
}
/* Program to evaluate the following series x-x3/3!+x5/5!------
using a Recursive function */
#include<stdio.h>
#include <math.h>
int fact(n)
int n;
{
if (n<=1)
return (1);
else
return(n*fact(n-1));
}

float funct(x,n)
int x,n;
{
float num;
int den;
float term;
if (n==1)
return((float)x);
else
{
num=pow(x,2*n-1);
den=fact(2*n-1);
term=pow(-1,n-1)*num/(float)den;
return(term+funct(x,n-1));
}
}
main()

Page 53 of 56
Page 54 of 56

{
int x,n;
float s;
clrscr();
printf("enter x and n");
scanf("%d%d",&x,&n);
s=funct(x,n);
printf("result %f \n",s);
getch();
}

/* Program to find nth fibonacci number


using a Recursive function */
#include<stdio.h>
#include <math.h>
int fib(n)
int n;
{
if (n==1)
return(0);
else
if (n==2)
return(1);
else
return(fib(n-1)+fib(n-2));
}
main()
{
int n,f;
clrscr();
printf("which term you want");
scanf("%d",&n);
f=fib(n);
printf("the %d th fibonacci term is %d \n",n,f);
getch();
}

PASSING ARRAYS TO FUNCTIONS

Like the values of simple variables, it is possible to pass the values of an array to
a function. To pass an array to a called function, it is sufficient to list the name of the
array, without any subscripts, and the size of the array as arguments.
largest (a, n)
will pass all the elements contained in the array a of size n.
#include<stdio.h>

Page 54 of 56
Page 55 of 56

largest (a,n)
int a[],n;
{
int i,large;
large=0;
i=0;
while(i<n)
{
if (a[i]>large)
large=a[i];
i++;
}
return(large);
}
main( )
{
static int a[10] = {10,5,3,15,8,7,2,3,18,20};
int l;
l=largest(a,10);
printf(largest = %d\n,l);
}

/*program to find factorial of a given number


using a multiple programmes*/
/* programme file containing main() function */

#include<stdio.h>
#include "mul2.c"
main()
{
int n,fn;
clrscr();
printf("enter n");
scanf("%d",&n);
fn=factorial(n); /* Function Call */
printf("the factorial %d is %d\n",n,fn);
getch();
}

/* programme file containing function factorial */

int factorial(x)
int x;
{
int i,f;

Page 55 of 56
Page 56 of 56

f=1;
for(i=1;i<=x;i++)
f=f*i;
return(f);
}

Page 56 of 56

You might also like