C Programming
C Programming
C Programming
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);
printf("factorial of %d is %d\n",n,fn);
getch();
}
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();
}
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
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();
}
Page 4 of 56
Page 5 of 56
getch();
}
num=nsq%den;
if ( n == num)
printf("%d is atomorphic number ",n);
else
printf("%d is not an atomorphic number ",n);
getch();
}
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();
}
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();
}
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
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
#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();
}
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();
}
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();
}
Page 13 of 56
Page 14 of 56
}while(num>0);
printf("the reverse number = %d\n",revnum);
getch();
}
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();
num=nsq%den;
if ( n == num)
printf("%d is atomorphic number ",n);
else
printf("%d is not an atomorphic number ",n);
getch();
}
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();
}
do
{
next=previous+present;
printf(" %d",next);
count++;
previous=present;
present=next;
}while(count<n);
getch();
}
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();
}
Page 17 of 56
Page 18 of 56
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();
}
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
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();
}
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 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();
}
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();
}
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();
}
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();
}
Page 26 of 56
Page 27 of 56
/*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]);
}
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();
}
getch();
}
Page 30 of 56
Page 31 of 56
for(i=0;i<n/2;i++)
{
temp=a[i];
a[i]=a[n-i-1];
a[n-i-1]=temp;
}
#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
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
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.
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
Page 34 of 56
Page 35 of 56
printf("%d ",c[i][j]);
printf("\n");
}
getch();
}
Page 35 of 56
Page 36 of 56
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();
}
#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];
Page 37 of 56
Page 38 of 56
printf("\n");
}
getch();
}
getch();
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();
}
Page 40 of 56
Page 41 of 56
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();
}
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();
}
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]);
}
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]);
}
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]);
}
linearsearch(a,n,target);
getch();
}
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();
}
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]);
}
Page 50 of 56
Page 51 of 56
RECURSION
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.
Page 51 of 56
Page 52 of 56
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();
}
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);
}
#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();
}
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