You are on page 1of 18

Experiments using C programs

1. Evaluating Mathematical Formulas

1.1 Area of circle


#include<stdio.h>
#include<conio.h>
main()
{
int r,area;
clrscr();
printf("Enter the radius of the circle : ");
scanf("%d",&r);
area=3.14*r*r;
printf("The area of circle is : %d",area);
getch();
}

1.2 Centigrade to Fahrenheit conversion


#include<stdio.h>
#include<conio.h>
main()
{
float c,f;
clrscr();
printf("Enter the centigrade value : ");
scanf("%f",&c);
f=(9/5)*(c+32);
printf("The Fahrenheit value is : %4.2f",f);
getch();
}

1.3. Find greatest of three numbers


#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c;
clrscr();
printf("Enter three numbers : ");
scanf("%d%d%d",&a,&b,&c);
if((a>b) && (a>c))
printf(" %d is greater",a);
else if(b>c)
printf(" %d is greater",b);
else

printf(" %d is greater",c);
getch();
}

1.4 Find ODD or EVEN Number


#include<stdio.h>
#include<conio.h>
main()
{
int num;
clrscr();
printf("Enter the number : ");
scanf("%d",&num);
if(num%2==0)
printf("The number %d is EVEN",num);
else
printf("The number %d is ODD",num);
getch();
}

1.5 Find Square root of a number


#include<stdio.h>
#include<conio.h>
main()
{
float num,r,a,b,error=0.00001;
clrscr();
printf("Enter the number : ");
scanf("%f",&num);
a=num;
r=a*a;
while( r-num >= error)
{
b=(a+(num/a))/2;
a=b;
r=a*a;
}
printf("The square root of %7.2f is %7.2f",num,a);
getch();
}

1.6 Find roots of a Quadratic equation


#include<stdio.h>
#include<conio.h>
#include<math.h>
main()

{
int a,b,c,d;
float r1,r2;
clrscr();
printf("Enter the co-efficient values a, b, c : ");
scanf("%d%d%d",&a,&b,&c);
d=b*b-4*a*c;
if(d<0)
printf("The roots are imaginary.");
else if(d==0)
{
r1=-b/(2*a);
r2=r1;
printf("The roors are equal r1=%4.2f r2=
%4.2f",r1,r2);
}
else
{
r1=(-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);
printf("The roors are different r1=%4.2f r2=
%4.2f",r1,r2);
}
getch();
}

1.7 Print tomorrow's date


#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int d1,d2,m1,m2,y1,y2;
clrscr();
printf("Enter todays date (dd/mm/yyyy) : ");
scanf("%d%d%d",&d1,&m1,&y1);
if((d1<30) && (m1!=2))
{
d2=d1+1;
m2=m1;
y2=y1;
}
else if((d1==30) && (m1==4)||(m1==6) ||(m1==9)||
(m1==11))
{
d2=1;

m2=m1+1;
y2=y1;
}
else if((d1==31) && (m1==12))
{
d2=1;
m2=1;
y2=y1+1;
}
else if((d1==31) && (m1!=12))
{
d2=1;
m2=m1+1;
y2=y1;
}
else if((d1==29) && (m1==2) && (y1%4==0))
{
d2=1;
m2=3;
y2=y1;
}
else if((d1==28) && (m1==2) && (y1%4!=0))
{
d2=1;
m2=3;
y2=y1;
}
else
{
d2=d1+1;
m2=m1;
y2=y1;
}
printf("Tomorrow's date is (%d/%d/%d)",d2,m2,y2);
getch();
}

1.7A Print Tomorrow's date from


Today's Date
#include<stdio.h>
#include<conio.h>
void main()
{
int d1,d2,m1,m2,y1,y2;
clrscr();

printf("Enter todays date in (DD MM YY) format :


");
scanf("%d%d%d",&d1,&m1,&y1);
if(m1==2)
{
if((y1%4==0)&&(d1==29))
{
d2=1;
m2=m1+1;
y2=y1;
}
else if((y1%4!=0) && (d1==28))
{
d2=1;
m2=m1+1;
y2=y1;
}
else
{
d2=d1+1;
m2=m1;
y2=y1;
}
}
else if(m1==12 && d1==31)
{
d2=1;
m2=1;
y2=y1+1;
}
else if((m1<8 && m1%2==1)||(m1>7 &&
m1%2==0))
{
if(d1==31)
{
d2=1;
m2=m1+1;
y2=y1;
}
else
{
d2=d1+1;
m2=m1;
y2=y1;
}
}

else
{
if(d1==30)
{
d2=1;
m2=m1+1;
y2=y1;
}
else
{
d2=d1+1;
m2=m1;
y2=y1;
}
}
printf("Tomorrows date is : %d / %d /%d
\n",d2,m2,y2);
getch();
}

2. Manipulating Numbers.
2.1 Sum of individual digits of a number
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int n,num,sum,a;
clrscr();
printf("Enter the number : ");
scanf("%d",&num);
sum=0;
n=num;
while(n>0)
{
a=n%10;
sum=sum+a;
n=n/10;
}
printf("The sum of individual digits of %d is
%d",num,sum);
getch();
}

2.2 Reverse the number

2.4 Check Armstrong Number

#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int n,num,rev,a;
clrscr();
printf("Enter the number : ");
scanf("%d",&num);
rev=0;
n=num;
while(n>0)
{
a=n%10;
rev=rev*10+a;
n=n/10;
}
printf("The reverse of %d is %d",num,rev);
getch();
}

#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int n,num,arm,a;
clrscr();
printf("Enter the number : ");
scanf("%d",&num);
arm=0;
n=num;
while(n>0)
{
a=n%10;
arm=arm+pow(a,3);
n=n/10;
}
if(num==arm)
printf("The number %d is armstrong",num);
else
printf("The number %d is not armstrong",num);
getch();
}

2.3 Check Palindrome Number


#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int n,num,rev,a;
clrscr();
printf("Enter the number : ");
scanf("%d",&num);
rev=0;
n=num;
while(n>0)
{
a=n%10;
rev=rev*10+a;
n=n/10;
}
if(num==rev)
printf("The number %d is palindrome",num);
else
printf("The number %d is not palindrome",num);
getch();
}

2.5 Count no of times a digit present in the


number
#include<stdio.h>
#include<conio.h>
main()
{
int n,num,count,a,d;
clrscr();
printf("Enter the number : ");
scanf("%d",&num);
printf("Enter the digit to be counted..");
scanf("%d",&d);
n=num;
count=0;
while(n>0)
{
a=n%10;
if(a==d)
count++;
n=n/10;

}
printf("The digit %d is found %d times in
%d",d,count,num);
getch();
}

2.6 Digit a Number using ascending order


#include<stdio.h>
#include<conio.h>
main()
{
int a[10], num, len,t,i,j;
clrscr();
printf("Enter the number ");
scanf("%d", &num);
len=0;
while(num > 0)
{
a[len]=num%10;
num=num/10;
len++;
}
for( i=0;i<len;i++)
{
for( j=i;j<len;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf("THe number after ordering the digit :");
for( i=0;i<len;i++)
printf("%d",a[i]);
getch();
}

3. Calculate sum of series.


3.1 Sum of 'n' Numbers
#include<stdio.h>
#include<conio.h>
main()

{
int i,n,sum;
clrscr();
printf("Enter the value of N : ");
scanf("%d", &n);
sum=0;
for( i=1;i<=n;i++)
sum+=i;
printf("The sum of first %d numbers is :
%d",n,sum);
getch();
}

3.2 Sum of squares upto n^2


#include<stdio.h>
#include<conio.h>
main()
{
int i,n,sum;
clrscr();
printf("Enter the value of N : ");
scanf("%d", &n);
sum=0;
for( i=1;i<=n;i++)
sum+=i*i;
printf("The sum of first %d squares is : %d",n,sum);
getch();
}

3.3 Sum of series -x + (x^3/3!) - (x^5/5!)


+...
#include<stdio.h>
#include<conio.h>
#include<math.h>
int fact(int m)
{
int i,f=1;
for(i=1;i<=m;i++)
f=f*i;
return f;
}
void main()
{
int i,s;
float sum,n,x;

clrscr();
printf("Enter the values of x and n : ");
scanf("%f%f",&x,&n);
sum=0.0;
s=1;
for(i=1;i<=n;i+=2)
{
s=s*-1;
sum=sum+((pow(x,i)/fact(i))*s);
}
printf("\nThe sum of series %2.0f ^ %2.0f is :
%f",x,n,sum);
getch();
}

3.4 Sum of series 1 - (x^2/2!) + (x^4/4!)+...


#include<stdio.h>
#include<conio.h>
#include<math.h>
int fact(int m)
{
int i,f=1;
for(i=1;i<=m;i++)
f=f*i;
return f;
}
void main()
{
int i,s;
float sum,n,x;
clrscr();
printf("Enter the values of x and n : ");
scanf("%f%f",&x,&n);
sum=0.0;
s=-1;
for(i=0;i<=n;i+=2)
{
s=s*-1;
sum=sum+((pow(x,i)/fact(i))*s);
}
printf("\nThe sum of series %2.0f ^ %2.0f is :
%5.4f",x,n,sum);
getch();
}

4. Generate Numbers
4.1 Generate Multiples of a Number
#include<stdio.h>
#include<conio.h>
main()
{
int i,n;
clrscr();
printf("Enter the number : ");
scanf("%d",&n);
printf("The Multiples of %d are : \n",n);
for( i=n;i<=50;i+=n)
printf("%5d",i);
getch();
}

4.2 Generate numbers divisible by 2 not


by 3 and 5
#include<stdio.h>
#include<conio.h>
main()
{
int i;
clrscr();
printf("The numbers divisble by 2 not by 3 & 5
are : \n");
for( i=1;i<=100;i++)
{
if((i%2==0) && (i%3!=0) && (i%5!=0))
printf("%5d", i);
}
getch();
}

4.3 Generate prime numbers between


50-100
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main ()
{
int prime,i,j;
clrscr();
printf("Prime numbers between 50-100 are : \n");
for (i=50;i<=100;i++)

{
prime=1;
for(j=2;j<i;j++)
{
if(i%j==0)
{
prime=0;
break;
}
}
if(prime==1)
printf("%5d", i);
}
getch();
}

4.4 Generate first '6' twin prime numbers


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main ()
{
int prime, count=0,prev=1;
int n;
clrscr();
prime=2;
printf("First Six Twin primes : \n");
do
{
for ( n=2; n < prime; n++ )
if (prime%n==0 )
break;
if ( n==prime )
if((prev+2)==prime)
{
printf(" ( %5d,%5d )\n",
prev,prime);
prev=prime;
count++;
}
else
prev=prime;
prime++;
}while(count<6);
getch();

5. Write a C Program to perform


following Number Conversions.
5.1 Decimal - Binary conversion
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main ()
{
long int num, n, bin;
int i,a;
clrscr();
printf("Enter the Decimal Number : ");
scanf("%ld",&num);
n=num;
bin=0;
i=0;
while(n>0)
{
a=n%2;
bin=bin+(pow(10,i)*a);
n=n/2;
i++;
}
printf("The Binary equivalent of %ld is %ld
",num,bin);
getch();
}
5.2 Decimal - Octal conversion
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main ()
{
long int num, n, oct;
int i,a;
clrscr();
printf("Enter the Decimal Number : ");
scanf("%ld",&num);
n=num;
oct=0;
i=0;
while(n>0)

{
a=n%8;
oct=oct+(pow(10,i)*a);
n=n/8;
i++;
}
printf("The Octal equivalent of %ld is %ld
",num,oct);
getch();
}

5.3 Number conversion DECIMAL - HEX


#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<ctype.h>
#include<string.h>
void main()
{
int n,dec,i,a;
char *hex;
clrscr();
printf("Enter the Decimal number : ");
scanf("%d",&dec);
i=0;
n=dec;
while(n>0)
{
a=n%16;
if(a<10)
hex[i]=(char)(a+48);
else
hex[i]=(char)(a+55);
n=n/16;
i++;
}
hex[i]='\0';
strrev(hex);
printf("The Hex equivalent of %d is %s",dec,hex);
getch();
}

5.4 BINARY - GRAY CODE


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

int bin(int n)
{
int i,a,m;
m=0;
i=0;
while(n>0)
{
a=n%2;
m=m+a*pow(10,i);
n=n/2;
i++;
}
return m;
}
void main()
{
int dec,gray,a;
//float sum,n,x;
clrscr();
printf("Enter the Decimal number : ");
scanf("%d",&dec);
a=dec >> 1;
gray=dec ^ a;
printf("The gray equivalent of %d is
%d",bin(dec),bin(gray));
getch();
}

5.5 Gray code - Binary conversion


#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int gray, a,dec=0,d[10],i,n;
clrscr();
printf("Enter the gray code : ");
scanf("%d",&gray);
i=0;
n=gray;
while(n>0)
{
d[i]=n%10;
i++;
n=n/10;
}

i--;
dec=d[i]*pow(10,i);
a=d[i];
do
{
a=a^d[i-1];
dec=dec+(a*pow(10,i-1));
i--;
}while(i>0);
printf("The binary equivalent of gray code %d is
%d",gray, dec);
getch();
}

6. Programs using Arrays


6.1 Sort numbers in Ascending and
Decending order
#include<stdio.h>
#include<conio.h>
main()
{
int a[10],i,j,t,n;
clrscr();
printf("Enter no of Elements : ");
scanf("%d",&n);
printf("Enter the numbers to be sorted.. :\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(a[i] > a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
printf("\nThe Ascending order of the numbers
are: \n");
for(i=0;i<n;i++)
printf("%5d",a[i]);
printf("\nThe Decending order of the numbers
are: \n");
for(i=n-1;i>=0;i--)
printf("%5d",a[i]);

getch();
}

6.2 Sum of ODD positioned number and


even positioned numbers
#include<stdio.h>
#include<conio.h>
main()
{
int a[10],i,n,oddsum=0,evensum=0;
clrscr();
printf("Enter no of Elements : ");
scanf("%d",&n);
printf("Enter the numbers :\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
if(i%2!=0)
evensum+=a[i];
else
oddsum+=a[i];
}
printf("\nThe Sum of ODD positioned numbers is :
%3d \n",oddsum);
printf("\nThe Sum of EVEN positioned numbers is :
%3d \n",evensum);
getch();
}

6.3 Find a number in the list


#include<stdio.h>
#include<conio.h>
main()
{
int a[10],i,n,num,pos,found=0;
clrscr();
printf("Enter no of Elements : ");
scanf("%d",&n);
printf("Enter the Elements :\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the number to be find : ");
scanf("%d",&num);
for(i=0;i<n;i++)

if(num==a[i])
{
found=1;
pos=i;
break;
}
if(found)
printf("\nThe number %d is found at position %d
of the list.\n",num,pos+1);
else
printf("\nThe number %d is not found in the
list.\n",num);
getch();
}

6.4 Swap two array content with out using


third array
#include<stdio.h>
#include<conio.h>
main()
{
int a[10],b[10],i,t,n;
clrscr();
printf("Enter no of Elements : ");
scanf("%d",&n);
printf("Enter the Elements of Array - 1 :\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the Elements of Array - 2 :\n");
for(i=0;i<n;i++)
scanf("%d",&b[i]);
for(i=0;i<n;i++)
{
t=a[i];
a[i]=b[i];
b[i]=t;
}
printf("\nThe content of Array - 1 after swaping
:\n");
for(i=0;i<n;i++)
printf("%5d",a[i]);
printf("\nThe content of Array - 2 after swaping
:\n");
for(i=0;i<n;i++)
printf("%5d",b[i]);
getch();

7. Programs using Matrix.


7.1 Row sum and column sum 4x4 from
3x3
#include<stdio.h>
#include<conio.h>
void main()
{
int mat[4][4],i,j;
clrscr();
printf("Enter the matrix... :\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&mat[i][j]);
mat[i][j]=0;
mat[3][i]=0;
}
mat[3][3]=0;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
mat[i][3]=mat[i][3]+mat[i][j];
mat[3][j]=mat[3][j]+mat[i][j];
}
printf("The revised matrix ..:\n");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
printf("%5d",mat[i][j]);
printf("\n");
}
getch();
}

7.2 Matrix ADDITION, SUBTRACTION


& MULTIPLICATION
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],sum[3][3],sub[3][3],mul[3]
[3],i,j,k;
clrscr();

printf("Enter the matrix...A :\n");


for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("Enter the matrix...B :\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
sum[i][j]=sub[i][j]=mul[i][j]=0;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
sum[i][j]=a[i][j]+b[i][j];
sub[i][j]=a[i][j]-b[i][j];
for(k=0;k<3;k++)
mul[i][j]=mul[i][j]+(a[i][k]*b[k][j]);
}
printf("The SUM OF MATRIX A & B:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%5d",sum[i][j]);
printf("\n");
}
printf("The SUBTRACTION OF MATRIX A &
B:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%5d",sub[i][j]);
printf("\n");
}
printf("The PRODUCT OF MATRIX A & B:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%5d",mul[i][j]);
printf("\n");
}
getch();
}

7.3 Tranpose of a Matrix


#include<stdio.h>
#include<conio.h>
void main()
{
int mat[3][3],i,j;
clrscr();
printf("Enter the matrix :\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&mat[i][j]);
printf("The transpose of a Matrix is :\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%5d",mat[j][i]);
printf("\n");
}
getch();
}

7.4 Maximum in Row and Column


#include<stdio.h>
#include<conio.h>
void main()
{
int mat[4][4],i,j;
clrscr();
printf("Enter the matrix... :\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&mat[i][j]);
mat[i][j]=0;
mat[3][i]=0;
}
mat[3][3]=0;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
if(mat[i][3] < mat[i][j])
mat[i][3]=mat[i][j];
if(mat[3][j] < mat[i][j])
mat[3][j]=mat[i][j];
}

printf("The Maximum values..:\n");


for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
printf("%5d",mat[i][j]);
printf("\n");
}
getch();
}

7.5 Weighted arithmetic mean


//Calculate GPA
#include<stdio.h>
#include<conio.h>
void main()
{
int grade[5],credit[5],i;
float cg,w,gpa;
clrscr();
cg=0.0;
w=0.0;
printf("Enter credit values... :\n");
for(i=0;i<5;i++)
scanf("%d",&credit[i]);
printf("Enter grade values... :\n");
for(i=0;i<5;i++)
scanf("%d",&grade[i]);
for(i=0;i<5;i++)
{
cg+=grade[i]*credit[i];
w+=credit[i];
}
gpa=(float)cg/w;
printf("The weighted arithemetic mean (GPA) :
%3.2f\n",gpa);
getch();
}

8. Programs Using Strings.


8.1 Palindrome string
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()

{
char *str,i,j,len;
clrscr();
printf("Enter the string .. : ");
scanf("%s",str);
i=0;
j=strlen(str)-1;
len=j;
/* while((i<j) && (str[i]==str[j]))
{
i++; j--;
}
*/
for(i=0,j=len;(i<j) && (str[i]==str[j]);i++,j--)
{
}
if(i<j)
printf("The string %s is not palindrome",str);
else
printf("The string %s is palindrome",str);
getch();
}

8.2 String reverse and comparison


without using build-in functions
#include<stdio.h>
#include<conio.h>
int getlength(char *s)
{
int i=0;
while(s[i]!='\0')
{
i++;
}
return i;
}
char* reverse(char *str)
{
char *rev;
int i,j,len;
len=getlength(str);
i=0;
j=len-1;
while(i<len)
{

rev[i]=str[j];
i++;j--;
}
rev[i]='\0';
return rev;
}
int equal(char *s1, char *s2)
{
int i,len;
if(getlength(s1)!=getlength(s2))
return 0;
len=getlength(s1);
i=0;
while((i<len) && (s1[i]==s2[i]))
{
i++;
}
if(i<len)
return 0;
return 1;
}
void main()
{
char *str1,*str2,i,j,len;
clrscr();
printf("Enter the string - 1 : ");
scanf("%s",str1);
printf("Enter the string - 2 : ");
scanf("%s",str2);
if(equal(str1,str2))
printf("\nThe strings %s and %s are
equal\n",str1,str2);
else
printf("The strings %s and %s are not
equal\n",str1,str2);
printf("\n The reverse of %s is
%s\n",str1,reverse(str1));
printf("\n The reverse of %s is
%s\n",str2,reverse(str2));
getch();
}

8.3 Count characters, digits, words,


vowels, consonants
#include<stdio.h>
#include<conio.h>
void main()
{
char txt[80];
int nv=0,nc=0,nd=0,ns=0,no=0;
void input(char line[],int *pv,int *pc,int *pd,int
*ps,int *po);
clrscr();
printf("Enter line of texxt..\n");
gets(txt);
input(txt,&nv,&nc,&nd,&ns,&no);
printf("\n vowels..%d",nv);
printf("\n consonents..%d",nc);
printf("\n digits..%d",nd);
printf("\n spaces..%d",ns);
printf("\n words...%d",ns+1);
printf("\n other chars..%d",no);
getch();
}
void input(char txt[],int *pv,int *pc,int *pd,int
*ps,int *po)
{
char c;
int i=0;
while((c=toupper(txt[i]))!='\0')
{
if(c=='A'||c=='E'||c=='I'||c=='O'||c=='U')
++*pv;
else if(c>='A' && c<=c<='Z')
++*pc;
else if(c>='0' && c<='9')
++*pd;
else if(c==' '||c=='\t')
++*ps;
else
++*po;
++i;
}
}

9. Programs using Structures and


Unions.
9.1 Swap numbers using functions
#include<stdio.h>
#include<conio.h>
void swap(int *a, int *b)
{
int t;
t=*a;
*a=*b;
*b=t;
}
void main()
{
int a,b;
clrscr();
printf("Enter the value of A : ");
scanf("%d",&a);
printf("Enter the value of B : ");
scanf("%d",&b);
swap(&a,&b);
printf("The values of A & B after swaping : \n");
printf("A = %d B = %d",a,b);
getch();
}

9.2 Multiplication without using '*'


#include<stdio.h>
#include<conio.h>
int mul(int a, int b)
{
if(b==0)
return 0;
return a+mul(a,b-1);
}
void main()
{
int a,b;
clrscr();
printf("Enter the value of A : ");
scanf("%d",&a);
printf("Enter the value of B : ");
scanf("%d",&b);

printf("The product of %d and %d is : %d


\n",a,b,mul(a,b));
getch();
}

9.3 Find factorial of a Number


#include<stdio.h>
#include<conio.h>
long int fact(int n)
{
if(n<1)
return 1;
return n*fact(n-1);
}
void main()
{
int num;
clrscr();
printf("Enter the number : ");
scanf("%d",&num);
printf("The factorial of %d! : %ld
\n",num,fact(num));
getch();
}

9.4 Generate fibonacci series


#include<stdio.h>
#include<conio.h>
void fibo(int a, int b)
{
if(b<100)
{
printf("%5d",a);
fibo(b,a+b);
}
}
void main()
{
clrscr();
printf("The Fibonacci sequence are :\n");
fibo(0,1);
getch();
}

9.5 Find nCr value


#include<stdio.h>
#include<conio.h>
long int fact(int n)
{
if(n<1)
return 1;
return n*fact(n-1);
}
void main()
{
int n,r,nc;
clrscr();
printf("Enter the value of n : ");
scanf("%d",&n);
printf("Enter the value of r : ");
scanf("%d",&r);
nc=fact(n)/(fact(n-r)*fact(r));
printf("The %dC%d value is : %d \n",n,r,nc);
getch();
}

10.Programs using Structures and


Unions.
10.1 Employee Payroll Processing
#include<stdio.h>
#include<conio.h>
struct employee
{
int eno;
char ename[20];
long int basic;
float da,hra,ta,pf,lic,itax;
float gross,ded,netpay;
}e[10];
void main()
{
int i,n;
clrscr();
printf("Enter No of Employees : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n\tEnter details of Employee :
%d\n\n",i+1);
printf("\nEnter Employee No : ");

scanf("%d",&e[i].eno);
printf("\nEnter Employee Name : ");
scanf("%s",&e[i].ename);
printf("\nEnter Basic Salary : ");
scanf("%ld",&e[i].basic);
e[i].da=(e[i].basic*35)/100;
e[i].hra=(e[i].basic*8)/100;
e[i].ta=(e[i].basic*5)/100;
e[i].pf=(e[i].basic*10)/100;
e[i].lic=(e[i].basic*3)/100;
e[i].itax=(e[i].basic*2)/100;
e[i].gross=e[i].basic+e[i].da+e[i].hra+e[i].ta;
e[i].ded=e[i].pf+e[i].lic+e[i].itax;
e[i].netpay=e[i].gross-e[i].ded;
}
for(i=0;i<n;i++)
{
printf("\n==============================
=========\n");
printf("\t PAY SLIP No : %d",i+1);
printf("\n---------------------------------------\n");
printf(" EMP_No : %d",e[i].eno);
printf("\tNAME : %s",e[i].ename);
printf("\n---------------------------------------\n");
printf(" BASIC : %8.2f",(float)e[i].basic);
printf("\n---------------------------------------\n");
printf(" DA : %8.2f",e[i].da);
printf("\tPF : %7.2f\n",e[i].pf);
printf(" HRA : %8.2f",e[i].hra);
printf("\tLIC : %7.2f\n",e[i].lic);
printf(" T.A. : %8.2f",e[i].ta);
printf("\tITAX : %7.2f",e[i].itax);
printf("\n---------------------------------------\n");
printf(" GROSS : %8.2f",e[i].gross);
printf(" DEDUCTION : %7.2f",e[i].ded);
printf("\n==============================
=========\n");
printf("\t\tNETPAY : Rs. %8.2f",e[i].netpay);
printf("\n==============================
=========\n\n");
}
getch();
}
OUTPUTL:
Enter No of Employees : 2

Enter Employee No : 9822


Enter Employee Name : Sam
Enter Basic Salary : 7000
Enter details of Employee : 2

Enter Employee No : 9833


Enter Employee Name : Began
Enter Basic Salary : 5000

//Studnet Mark List processing

10.2 Display Students Mark List using


Structures.
#include<stdio.h>
#include<conio.h>
struct stud

{
int rno;
char name[20];
int m1, m2,m3;
float tot, avg;
char grade;
}s[10];
void main()
{
int i,n;
char c;
clrscr();
printf("Enter No of students : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n\tEnter details of Student : %d\n\n",i+1);
printf("Enter Student No : ");
scanf("%d",&s[i].rno);
printf("Enter Student Name : ");
scanf("%s",s[i].name);
printf("Enter Mark - 1 : ");
scanf("%d",&s[i].m1);
printf("Enter Mark - 2 : ");
scanf("%d",&s[i].m2);
printf("Enter Mark - 3 : ");
scanf("%d",&s[i].m3);
s[i].tot=s[i].m1+s[i].m2+s[i].m3;
s[i].avg=s[i].tot/3;
if((s[i].m1<45)||(s[i].m2<45)||(s[i].m3<45))
s[i].grade='U';
else if(s[i].avg>=75)
s[i].grade='S';
else if(s[i].avg>=60)
s[i].grade='A';
else if(s[i].avg>=45)
s[i].grade='B';
else
s[i].grade='U';
}
printf("\n--------------------------------------------------------------------\n");
printf("\t\t\tSTUDNET MARK DETAILS");
printf("\n--------------------------------------------------------------------\n");

printf("Roll.No STUDENT NAME MARK-1


MARK-2 MARK-3 TOTAL AVERAGE GRADE
");
printf("\n--------------------------------------------------------------------\n");
for(i=0;i<n;i++)
{
printf("%5d",s[i].rno);
printf("%15s",s[i].name);
printf("%8d",s[i].m1);
printf("%8d",s[i].m2);
printf("%8d",s[i].m3);
printf("%9.0f",s[i].tot);
printf("%8.2f",s[i].avg);
printf("%7c\n",s[i].grade);
}
printf("--------------------------------------------------------------------\n");
getch();
}
OUTPUT:
Enter No of students : 4
Enter details of Student : 1
Enter
Enter
Enter
Enter
Enter

Student No : 22
Student Name : Ganesh.B
Mark - 1
: 67
Mark - 2
: 56
Mark - 3
: 89

Enter details of Student : 2


Enter
Enter
Enter
Enter
Enter

Student No : 33
Student Name : Sam
Mark - 1
: 89
Mark - 2
: 80
Mark - 3
: 97

Enter details of Student : 3

Enter Student No : 43

Enter
Enter
Enter
Enter

Student Name : Kalaiselvi


Mark - 1
: 98
Mark - 2
: 97
Mark - 3
: 94

Enter details of Student : 4


Enter
Enter
Enter
Enter
Enter

Student No : 34
Student Name : Subha
Mark - 1
: 89
Mark - 2
: 83
Mark - 3
: 87

You might also like