PROGRAMS
1.Find sum of two numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,s;
clrscr();
printf("Enter two no: ");
scanf("%d%d",&a,&b);
s=a+b;
printf("sum=%d",s);
getch();
}
Output:Enter two no: 5
6
sum=11
2.Program to
find area and circumference of circle.
#include<stdio.h>
#include<conio.h>
void main()
{
int r;
float pi=3.14,area,ci;
clrscr();
printf("enter radius of circle:
");
scanf("%d",&r);
area=pi*r*r;
printf("area of circle=%f",area);
ci=2*pi*r;
printf("circumference=%f",ci);
getch();
}
Output:enter radius of a circle: 5
area of circle=78.000
circumference=31.4
#include<stdio.h>
void main()
{
int i,j,sum=0;
for(i=1;i<=3;i++)
{
for(j=0;j<=i-1;j++)
{
sum+=1;
printf("%d",sum);
}
printf("\n");
}
}
1)WRITE A PROGRAM TO READ X,Y COORDINATES OF THREE POINTS AND THEN CALCULATE THE AREA OF THE TRIANGLE FORMED BY THEM AND PRINT THE COORDINATES OF THE THREE POINTS AND THE AREA OF THE TRIANGLE.WHAT WILL BE THE OUTPUT FROM YOUR PROGRAM IF THE THREE GIVEN POINTS ARE IN A STRAIGHT LINE?
#include<stdio.h>
#include<conio.h>
void main()
{
int x1,x2,x3,y1,y2,y3;
float area;
clrscr();
printf("Enter first coordinates:");
scanf("%d%d",&x1,&y1);
printf("Enter second coordinates:");
scanf("%d%d",&x2,&y2);
printf("Enter third coordinates:");
scanf("%d%d",&x3,&y3);
area=(0.5)*((x1*(y2-y3))+(x2*(y3-y1))+(x3*(y1-y2)));
if(area==0)
printf("Given three points(%d,%d)(%d,%d)(%d,%d) are collinear and area is %f",x1,y1,x2,y2,x3,y3,area);
else if(area<0)
{
area=area*(-1);
printf("Given three points(%d,%d)(%d,%d)(%d,%d) form a triangle and area is %f",x1,y1,x2,y2,x3,y3,area);
}
else
printf("Given three points(%d,%d)(%d,%d)(%d,%d) form a triangle and area is %f",x1,y1,x2,y2,x3,y3,area);
getch();
}
2)WRITE A PROGRAM TO FIND THE ROOTS OF QUADRATIC EQUATION USING "IF-ELSE” AND “SWITCH” STATEMENTS:
#include<stdio.h>
#include<conio.h>
#include<math.h>
main( )
{
float a,b,c,D,r1,r2,a1,a2;
int value;
clrscr();
printf("Enter the values of a,b,c");
scanf("%f%f%f",&a,&b,&c);
if(a==0)
{
printf("It is not a quadratic equation");
}
else
{
D=(b*b)-(4*a*c);
if(D>0)
value=1;
else if(D<0)
value=2;
else
value=3;
switch(value)
{
case 1: printf("\nThe roots are real and distinct:");
r1=(-b+sqrt(D))/(2*a);
r2=(-b-sqrt(D))/(2*a);
printf("\nThe roots are:r1=%f\t r2=%f",r1,r2);
break;
case 2:printf("\nThe roots are imaginary and distenct:");
a1=(-b)/(2*a);
a2= (sqrt(fabs(D)))/(2*a);
printf("\nThe roots are r1=%f+i%f \t r2=%f-i%f",a1,a2,a1,a2);
break;
case 3:printf("\nThe roots are real and same:");
r1=r2=(-b)/(2*a);
printf("\nThe roots are r1=%f \t r2=%f",r1,r2);
break;
}
}
getch();
3)WRITE A PROGRAM WHICH GENERATES ONE HUNDRED RANDOM INTEGERS IN THE RANGE OF 1 TO 100,STORE THEM IN AN ARRAY AND THEN PRINTS THE AVERAGE.WRITE THREE VERSIONS OF THE PROGRAM USING DIFFERENT LOOP CONSTRUCTIONS(e.g for , while ,do while) :
USING FOR LOOP:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int a[150],i,sum=0,avg;
clrscr();
randomize();
for(i=1;i<=100;i++)
{
a[i]=random(100);
sum=sum+a[i];
printf(“%d\t”,a[i]);
}
avg=sum/100;
printf(“\nAverage of 100 random numbers is %d”,avg);
getch();
}
USING WHILE:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int a[150],i=1,sum=0,avg;
clrscr();
randomize();
while(i<=100)
{
a[i]=random(100);
sum=sum+a[i];
printf(“%d\t”,a[i]);
i++;
}
avg=sum/100;
printf(“\nAverage of 100 random numbers is %d”,avg);
getch();
}
USING DO WHILE:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int a[150],i=1,sum=0,avg;
clrscr();
randomize();
do
{
a[i]=random(100);
sum=sum+a[i];
printf(“%d\t”,a[i]);
i++;
}
while(i<=100);
avg=sum/100;
printf(“Average of 100 random numbers is %d”,avg);
getch();
}
4)WRITE A PROGRAM FOR MATRIX MULTIPLICATION OF SQUARE MATRIX:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],i,j,k,m,n,p,q;
clrscr();
printf("Enter the order of first matrix:");
scanf(" %d %d",&m,&n);
printf("Enter the order of second matrix:");
scanf("%d %d",&p,&q);
if((m==n)&&(n==p))
{
printf("Enter first matrix elements:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
printf("Enter second matrix elements:\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
}
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
}
printf("Resultant matrix elements:\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf(" %d ",c[i][j]);
}
printf(" \n");
}
}
else
printf("\n Not a Square Matrix and Matrix multiplication is not possible ");
getch();
}
5) WRITE A PROGRAM TO FIND MAX & MIN ELEMENTS WITH THEIR POSITIONS IN A GIVEN ARRAY AND THEN SORT THE ABOVE ARRAY:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a[10],b[10],i,j,n,large,small,pos1=1,pos2=1,temp;
clrscr();
printf("Enter how many values u want:");
scanf("%d",&n);
printf("Enter elements into array a:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
large=small=a[0];
for(i=0;i<n;i++)
{
if(a[i]>large)
{
large=a[i];
pos1=i+1;
}
else if(a[i]<small)
{
small=a[i];
pos2=i+1;
}
}
printf("\nthe largest element is:%d\tpos=%d",large,pos1);
printf("\nthe small element is:%d\tpos=%d",small,pos2);
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\the sorting order is given below:");
for(i=0;i<n;i++)
printf("%d",a[i]);
getch();
}
6)WRITE A Program to insert an element into an array:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,n,pos,item;
clrscr();
printf("Enter how many values u want:");
scanf("%d",&n);
printf("Enter elements into array a:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter which element u want to insert:");
scanf("%d",&item);
printf("Enter position for that element:");
scanf("%d",&pos);
for(i=n;i>=pos;i--)
{
a[i]=a[i-1];
}
a[pos-1]=item ;
printf("\nAfter insertion the array elements are:");
for(i=0;i<=n;i++)
printf(" %d ",a[i]);
getch();
}
7) Write a Function for Transposing a Square Matrix IN PLACE.(In PLACE MEANS THAT YOU ARE NOT ALLOWED TO HAVE FULL TEMPORARY MATRIX).
#include<stdio.h>
#include<conio.h>
main()
{
int a[10][10],m,n,i,j;
void trans(int a[10][10],int,int);
clrscr();
printf("Enter Order of Matrix A:\n");
scanf("%d %d",&m,&n);
if(m==n)
{
printf("Enter The Elements into Matrix A:\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
trans(a,m,n);
}
else
printf("Not a Square Matrix");
getch();
}
void trans(int a[10][10],int m,int n)
{
int i,j;
printf("The Transpose of Matrix A is:\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf("%3d",a[j][i]);
printf("\n");
}
}
8) Write a Program to Print Fibonacci Series Using Function.
#include<stdio.h>
#include<conio.h>
main()
{
void fib(int);
int n;
clrscr();
printf("Enter how many terms u want:");
scanf("%d",&n);
fib(n);
getch();
}
void fib(int n)
{
int a=0,b=1,c,i;
printf("The Fibonacci Series is:%2d %2d",a,b);
for(i=2;i<n;i++)
{
c=a+b;
a=b;
b=c;
printf("%4d",c);
}
}
9) Write a Program to Find the Factorial of a Given Number Using Recursions.
#include<stdio.h>
#include<conio.h>
main()
{
long int fact(int);
long int f,n;
clrscr();
printf("Enter The Number for Factorial:");
scanf("%ld",&n);
f=fact(n);
printf("The Factorial of %ld is:%ld",n,f);
getch();
}
long int fact(int n)
{
long int result;
if((n==0)||(n==1))
return 1;
else
result=n*fact(n-1);
return result;
}
10) Write a Program to Find “nCr” Using NON RECURSIVE Functions WHILE FINDING THE FACTORIAL VALUE USE RECURSIVE FUNCTIONS.
#include<stdio.h>
#include<conio.h>
main()
{
int fact(int);
float ncr(int,int);
int n,r;
clrscr();
printf("Enter n and r values for ncr:");
scanf("%d%d",&n,&r);
if(n<r)
printf("ncr is not possible");
else
printf("%dC%d is %.2f",n,r,ncr(n,r));
getch();
}
float ncr(int n,int r)
{
float result;
result=(float)fact(n)/(fact(n-r)*fact(r));
return (result);
}
int fact(int n)
{
int f;
if((n==0)||(n==1))
return 1;
else
f=n*fact(n-1);
return f;
}
11) Write a program to find whether the given String is Palindrome or Notwithout using string functions.
#include<stdio.h>
#include<conio.h>
main()
{
char a[50],b[50];
int length=0,i=0,j=0,flag;
clrscr();
printf("Enter The String:");
scanf("%s",a);
while(a[i]!='\0')
{
i++;
}
length=i;
printf("String Length is:%d",length);
for(i=length-1;i>=0;i--)
{
b[j]=a[i];
j++;
}
b[j]='\0';
for(i=0;i<length;i++)
{
if(b[i]==a[i])
flag=0;
else
{
flag=1;
break;
}
}
if(flag==0)
printf("\nThe String is Palindrome");
else
printf("\nThe String is Not Palindrome");
printf("\nThe Reverse String of %s is %s",a,b);
getch();
}
12) Given an Array of Strings write a program to Sort the Strings in Dictionary Order.
#include<stdio.h>
# include<conio.h>
# include<string.h>
main( )
{
char names[50][20], temp[20];
int n,i,j;
clrscr( ):
printf(“ \n How many names U want:”);
scanf(“%d”, &n);
printf(“ \n Enter the %d names one by one \n”,n);
for(i=0; i<n; i++)
scanf(“%s”, names[ i ]);
for(i=0; i<n-1; i++)
{
for(j=i+1; j<n; j++)
{
if(strcmp(names[ i ], names[ j ]) > 0)
{
strcpy(temp, names[ i ]);
strcpy(names[ i ], names[ j ]);
strcpy(names[ j ] , temp);
}
}
}
printf(“ \n The Names in Dictionary Order:”);
for(i=0; i<n; i++)
printf(“\n %s”, names[ i ]);
getch( );
}
13) DEVELOP A PROGRAM TO IMPLEMENT A STRUCTURE TO READ AND DISPLAY THE NAME , DATE OF BIRTH AND SALARY OF n EMPLOYERS
#include<stdio.h>
void main()
{
int n,i;
struct employee
{
char name[50];
int day,month,year;
long int salary;
}emp[10];
int i;
clrscr();
printf(“Enter the number of employees\n”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
printf("The name of %d employee:",i);
scanf("%s",emp[i].name);
printf("The Birth date(d m y):");
scanf("%d",&emp[i].day);
scanf("%d",&emp[i].month);
scanf("%d",&emp[i].year);
printf("Salary:");
scanf("%ld",&emp[i].salary);
}
printf("\n\t\tThe Employee Details");
printf("\n\t\t-------------------------");
printf("\n----\t----\t----------\t-------");
printf("\nS.No\tName\tBirth Date\tSalary");
printf("\n----\t----\t----------\t-------");
for(i=1;i<=n;i++)
{
printf("\n%d",i);
printf("\t%s",emp[i].name);
printf("\t%3d-%3d-3d",emp[i].day,emp[i].month,emp[i].year);
printf("\t%ld",emp[i].salary); } getch(); }
14) DEVELOP A PROGRAM TO DISPLAY THE NAME ,MARKS ,IN FIVE SUBJECTS AND TOTAL MARKS OF TEN STUDENTS(USING ARRAY OF STRUCTURES)
#include<stdio.h>
void main()
{
struct student
{
int sno;
char name[50];
int sb1,sb2,sb3,sb4,sb5;
}std[10];
int i;
clrscr();
for(i=1;i<=10;i++)
{
printf("student %d name:",i);
scanf("%s",std[i].name);
printf("Enter five subjects marks:");
scanf("%d%d%d%d%d",&std[i].sb1,&std[i].sb2,&std[i].sb3,&std[i].sb4,&std[i].sb5);
}
printf("\n\t\tThe Student Details");
printf("\n\t\t-------------------------");
printf("\nS.No\tName\tsub1\tsub2\tsub3\tsub4\tsub5\tTotal");
printf("\n----\t----\t----\t----\t----\t----\t----\t-----");
for(i=1;i<=10;i++)
{
printf("\n%d",i);
printf("\t%s ",std[i].name);
printf(" %3d\t%3d\t%3d\t%3d\t%3d",std[i].sb1,std[i].sb2,std[i].sb3,std[i].sb4,std[i].sb5);
printf("\t%3d",(std[i].sb1+std[i].sb2+std[i].sb3+std[i].sb4+std[i].sb5));
}
getch();
}
15) DEVELOP A PROGRAM TO READ AND WRITE A FILE
15.1
#include<stdio.h>
void main()
{
FILE *fp;
char ch;
clrscr();
fp=fopen("tsfile.txt","w");
printf("Enter any data:");
do
{
ch=getchar();
putc(ch,fp);
}while(ch!='\n');
fclose(fp);
fp=fopen("tsfile.txt","r");
if(fp==NULL)
printf("\nError cant open the file");
else
printf("The data in file is:");
do
{
ch=getc(fp);
putchar(ch);
}while(ch!='\n');
fclose(fp);
getch();
}
15.2 /*write a program for read write a file*/
#include<stdio.h>
void main()
{
FILE *fp;
char name[20];
unsigned int marks;
clrscr();
fp=fopen("217","a");
printf("Enter student's name and marks:\n");
printf("Use ctrl+Z to stop entry\n");
while((scanf("%s %u",name,&marks))!=EOF)
fprintf(fp,"%s %u",name,marks);
fclose(fp);
printf("\n");
fp=fopen("217","rt");
printf(" Name Marks \n");
printf("___________________\n");
while((fscanf(fp,"%s %u",name,&marks))!=EOF)
printf(" %-10s %3u \n",name,marks);
fclose(fp);
getch()
16) DEVELOP A PROGRAM TO CREATE AND COUNT NUMBER OF CHARACTERS IN A FILE.
#include<stdio.h>
void main()
{
FILE *fp;
int c,co=0;
clrscr();
printf("Enter the chars:\n");
printf("Press ctrl+Z to stop entry\n");
fp=fopen("enter.txt","w");
while((c=getchar())!=EOF)
fputc(c,fp);
fclose(fp);
printf("\n");
fp=fopen("enter.txt","r");
while((c=fgetc(fp))!=EOF)
{
printf("%c",(char)c);
++co;
}
fclose(fp);
printf("\nThe no of chars in the file are %d",co);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j;
clrscr();
printf("Enter the last limit\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",i);
}
}scanf(" ");
getch();
}
Write program in c to show the pattern 1 23 456...
#include<conio.h>
void main()
{
int n,i,j;
clrscr();
printf("Enter the last limit\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",i);
}
}scanf(" ");
getch();
}
Write program in c to show the pattern 1 23 456...
#include<stdio.h>
void main()
{
int i,j,sum=0;
for(i=1;i<=3;i++)
{
for(j=0;j<=i-1;j++)
{
sum+=1;
printf("%d",sum);
}
printf("\n");
}
}
1)WRITE A PROGRAM TO READ X,Y COORDINATES OF THREE POINTS AND THEN CALCULATE THE AREA OF THE TRIANGLE FORMED BY THEM AND PRINT THE COORDINATES OF THE THREE POINTS AND THE AREA OF THE TRIANGLE.WHAT WILL BE THE OUTPUT FROM YOUR PROGRAM IF THE THREE GIVEN POINTS ARE IN A STRAIGHT LINE?
#include<stdio.h>
#include<conio.h>
void main()
{
int x1,x2,x3,y1,y2,y3;
float area;
clrscr();
printf("Enter first coordinates:");
scanf("%d%d",&x1,&y1);
printf("Enter second coordinates:");
scanf("%d%d",&x2,&y2);
printf("Enter third coordinates:");
scanf("%d%d",&x3,&y3);
area=(0.5)*((x1*(y2-y3))+(x2*(y3-y1))+(x3*(y1-y2)));
if(area==0)
printf("Given three points(%d,%d)(%d,%d)(%d,%d) are collinear and area is %f",x1,y1,x2,y2,x3,y3,area);
else if(area<0)
{
area=area*(-1);
printf("Given three points(%d,%d)(%d,%d)(%d,%d) form a triangle and area is %f",x1,y1,x2,y2,x3,y3,area);
}
else
printf("Given three points(%d,%d)(%d,%d)(%d,%d) form a triangle and area is %f",x1,y1,x2,y2,x3,y3,area);
getch();
}
2)WRITE A PROGRAM TO FIND THE ROOTS OF QUADRATIC EQUATION USING "IF-ELSE” AND “SWITCH” STATEMENTS:
#include<stdio.h>
#include<conio.h>
#include<math.h>
main( )
{
float a,b,c,D,r1,r2,a1,a2;
int value;
clrscr();
printf("Enter the values of a,b,c");
scanf("%f%f%f",&a,&b,&c);
if(a==0)
{
printf("It is not a quadratic equation");
}
else
{
D=(b*b)-(4*a*c);
if(D>0)
value=1;
else if(D<0)
value=2;
else
value=3;
switch(value)
{
case 1: printf("\nThe roots are real and distinct:");
r1=(-b+sqrt(D))/(2*a);
r2=(-b-sqrt(D))/(2*a);
printf("\nThe roots are:r1=%f\t r2=%f",r1,r2);
break;
case 2:printf("\nThe roots are imaginary and distenct:");
a1=(-b)/(2*a);
a2= (sqrt(fabs(D)))/(2*a);
printf("\nThe roots are r1=%f+i%f \t r2=%f-i%f",a1,a2,a1,a2);
break;
case 3:printf("\nThe roots are real and same:");
r1=r2=(-b)/(2*a);
printf("\nThe roots are r1=%f \t r2=%f",r1,r2);
break;
}
}
getch();
3)WRITE A PROGRAM WHICH GENERATES ONE HUNDRED RANDOM INTEGERS IN THE RANGE OF 1 TO 100,STORE THEM IN AN ARRAY AND THEN PRINTS THE AVERAGE.WRITE THREE VERSIONS OF THE PROGRAM USING DIFFERENT LOOP CONSTRUCTIONS(e.g for , while ,do while) :
USING FOR LOOP:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int a[150],i,sum=0,avg;
clrscr();
randomize();
for(i=1;i<=100;i++)
{
a[i]=random(100);
sum=sum+a[i];
printf(“%d\t”,a[i]);
}
avg=sum/100;
printf(“\nAverage of 100 random numbers is %d”,avg);
getch();
}
USING WHILE:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int a[150],i=1,sum=0,avg;
clrscr();
randomize();
while(i<=100)
{
a[i]=random(100);
sum=sum+a[i];
printf(“%d\t”,a[i]);
i++;
}
avg=sum/100;
printf(“\nAverage of 100 random numbers is %d”,avg);
getch();
}
USING DO WHILE:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int a[150],i=1,sum=0,avg;
clrscr();
randomize();
do
{
a[i]=random(100);
sum=sum+a[i];
printf(“%d\t”,a[i]);
i++;
}
while(i<=100);
avg=sum/100;
printf(“Average of 100 random numbers is %d”,avg);
getch();
}
4)WRITE A PROGRAM FOR MATRIX MULTIPLICATION OF SQUARE MATRIX:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],i,j,k,m,n,p,q;
clrscr();
printf("Enter the order of first matrix:");
scanf(" %d %d",&m,&n);
printf("Enter the order of second matrix:");
scanf("%d %d",&p,&q);
if((m==n)&&(n==p))
{
printf("Enter first matrix elements:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
printf("Enter second matrix elements:\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
}
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
}
printf("Resultant matrix elements:\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf(" %d ",c[i][j]);
}
printf(" \n");
}
}
else
printf("\n Not a Square Matrix and Matrix multiplication is not possible ");
getch();
}
5) WRITE A PROGRAM TO FIND MAX & MIN ELEMENTS WITH THEIR POSITIONS IN A GIVEN ARRAY AND THEN SORT THE ABOVE ARRAY:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a[10],b[10],i,j,n,large,small,pos1=1,pos2=1,temp;
clrscr();
printf("Enter how many values u want:");
scanf("%d",&n);
printf("Enter elements into array a:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
large=small=a[0];
for(i=0;i<n;i++)
{
if(a[i]>large)
{
large=a[i];
pos1=i+1;
}
else if(a[i]<small)
{
small=a[i];
pos2=i+1;
}
}
printf("\nthe largest element is:%d\tpos=%d",large,pos1);
printf("\nthe small element is:%d\tpos=%d",small,pos2);
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\the sorting order is given below:");
for(i=0;i<n;i++)
printf("%d",a[i]);
getch();
}
6)WRITE A Program to insert an element into an array:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,n,pos,item;
clrscr();
printf("Enter how many values u want:");
scanf("%d",&n);
printf("Enter elements into array a:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter which element u want to insert:");
scanf("%d",&item);
printf("Enter position for that element:");
scanf("%d",&pos);
for(i=n;i>=pos;i--)
{
a[i]=a[i-1];
}
a[pos-1]=item ;
printf("\nAfter insertion the array elements are:");
for(i=0;i<=n;i++)
printf(" %d ",a[i]);
getch();
}
7) Write a Function for Transposing a Square Matrix IN PLACE.(In PLACE MEANS THAT YOU ARE NOT ALLOWED TO HAVE FULL TEMPORARY MATRIX).
#include<stdio.h>
#include<conio.h>
main()
{
int a[10][10],m,n,i,j;
void trans(int a[10][10],int,int);
clrscr();
printf("Enter Order of Matrix A:\n");
scanf("%d %d",&m,&n);
if(m==n)
{
printf("Enter The Elements into Matrix A:\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
trans(a,m,n);
}
else
printf("Not a Square Matrix");
getch();
}
void trans(int a[10][10],int m,int n)
{
int i,j;
printf("The Transpose of Matrix A is:\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf("%3d",a[j][i]);
printf("\n");
}
}
8) Write a Program to Print Fibonacci Series Using Function.
#include<stdio.h>
#include<conio.h>
main()
{
void fib(int);
int n;
clrscr();
printf("Enter how many terms u want:");
scanf("%d",&n);
fib(n);
getch();
}
void fib(int n)
{
int a=0,b=1,c,i;
printf("The Fibonacci Series is:%2d %2d",a,b);
for(i=2;i<n;i++)
{
c=a+b;
a=b;
b=c;
printf("%4d",c);
}
}
9) Write a Program to Find the Factorial of a Given Number Using Recursions.
#include<stdio.h>
#include<conio.h>
main()
{
long int fact(int);
long int f,n;
clrscr();
printf("Enter The Number for Factorial:");
scanf("%ld",&n);
f=fact(n);
printf("The Factorial of %ld is:%ld",n,f);
getch();
}
long int fact(int n)
{
long int result;
if((n==0)||(n==1))
return 1;
else
result=n*fact(n-1);
return result;
}
10) Write a Program to Find “nCr” Using NON RECURSIVE Functions WHILE FINDING THE FACTORIAL VALUE USE RECURSIVE FUNCTIONS.
#include<stdio.h>
#include<conio.h>
main()
{
int fact(int);
float ncr(int,int);
int n,r;
clrscr();
printf("Enter n and r values for ncr:");
scanf("%d%d",&n,&r);
if(n<r)
printf("ncr is not possible");
else
printf("%dC%d is %.2f",n,r,ncr(n,r));
getch();
}
float ncr(int n,int r)
{
float result;
result=(float)fact(n)/(fact(n-r)*fact(r));
return (result);
}
int fact(int n)
{
int f;
if((n==0)||(n==1))
return 1;
else
f=n*fact(n-1);
return f;
}
11) Write a program to find whether the given String is Palindrome or Notwithout using string functions.
#include<stdio.h>
#include<conio.h>
main()
{
char a[50],b[50];
int length=0,i=0,j=0,flag;
clrscr();
printf("Enter The String:");
scanf("%s",a);
while(a[i]!='\0')
{
i++;
}
length=i;
printf("String Length is:%d",length);
for(i=length-1;i>=0;i--)
{
b[j]=a[i];
j++;
}
b[j]='\0';
for(i=0;i<length;i++)
{
if(b[i]==a[i])
flag=0;
else
{
flag=1;
break;
}
}
if(flag==0)
printf("\nThe String is Palindrome");
else
printf("\nThe String is Not Palindrome");
printf("\nThe Reverse String of %s is %s",a,b);
getch();
}
12) Given an Array of Strings write a program to Sort the Strings in Dictionary Order.
#include<stdio.h>
# include<conio.h>
# include<string.h>
main( )
{
char names[50][20], temp[20];
int n,i,j;
clrscr( ):
printf(“ \n How many names U want:”);
scanf(“%d”, &n);
printf(“ \n Enter the %d names one by one \n”,n);
for(i=0; i<n; i++)
scanf(“%s”, names[ i ]);
for(i=0; i<n-1; i++)
{
for(j=i+1; j<n; j++)
{
if(strcmp(names[ i ], names[ j ]) > 0)
{
strcpy(temp, names[ i ]);
strcpy(names[ i ], names[ j ]);
strcpy(names[ j ] , temp);
}
}
}
printf(“ \n The Names in Dictionary Order:”);
for(i=0; i<n; i++)
printf(“\n %s”, names[ i ]);
getch( );
}
13) DEVELOP A PROGRAM TO IMPLEMENT A STRUCTURE TO READ AND DISPLAY THE NAME , DATE OF BIRTH AND SALARY OF n EMPLOYERS
#include<stdio.h>
void main()
{
int n,i;
struct employee
{
char name[50];
int day,month,year;
long int salary;
}emp[10];
int i;
clrscr();
printf(“Enter the number of employees\n”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
printf("The name of %d employee:",i);
scanf("%s",emp[i].name);
printf("The Birth date(d m y):");
scanf("%d",&emp[i].day);
scanf("%d",&emp[i].month);
scanf("%d",&emp[i].year);
printf("Salary:");
scanf("%ld",&emp[i].salary);
}
printf("\n\t\tThe Employee Details");
printf("\n\t\t-------------------------");
printf("\n----\t----\t----------\t-------");
printf("\nS.No\tName\tBirth Date\tSalary");
printf("\n----\t----\t----------\t-------");
for(i=1;i<=n;i++)
{
printf("\n%d",i);
printf("\t%s",emp[i].name);
printf("\t%3d-%3d-3d",emp[i].day,emp[i].month,emp[i].year);
printf("\t%ld",emp[i].salary); } getch(); }
14) DEVELOP A PROGRAM TO DISPLAY THE NAME ,MARKS ,IN FIVE SUBJECTS AND TOTAL MARKS OF TEN STUDENTS(USING ARRAY OF STRUCTURES)
#include<stdio.h>
void main()
{
struct student
{
int sno;
char name[50];
int sb1,sb2,sb3,sb4,sb5;
}std[10];
int i;
clrscr();
for(i=1;i<=10;i++)
{
printf("student %d name:",i);
scanf("%s",std[i].name);
printf("Enter five subjects marks:");
scanf("%d%d%d%d%d",&std[i].sb1,&std[i].sb2,&std[i].sb3,&std[i].sb4,&std[i].sb5);
}
printf("\n\t\tThe Student Details");
printf("\n\t\t-------------------------");
printf("\nS.No\tName\tsub1\tsub2\tsub3\tsub4\tsub5\tTotal");
printf("\n----\t----\t----\t----\t----\t----\t----\t-----");
for(i=1;i<=10;i++)
{
printf("\n%d",i);
printf("\t%s ",std[i].name);
printf(" %3d\t%3d\t%3d\t%3d\t%3d",std[i].sb1,std[i].sb2,std[i].sb3,std[i].sb4,std[i].sb5);
printf("\t%3d",(std[i].sb1+std[i].sb2+std[i].sb3+std[i].sb4+std[i].sb5));
}
getch();
}
15) DEVELOP A PROGRAM TO READ AND WRITE A FILE
15.1
#include<stdio.h>
void main()
{
FILE *fp;
char ch;
clrscr();
fp=fopen("tsfile.txt","w");
printf("Enter any data:");
do
{
ch=getchar();
putc(ch,fp);
}while(ch!='\n');
fclose(fp);
fp=fopen("tsfile.txt","r");
if(fp==NULL)
printf("\nError cant open the file");
else
printf("The data in file is:");
do
{
ch=getc(fp);
putchar(ch);
}while(ch!='\n');
fclose(fp);
getch();
}
15.2 /*write a program for read write a file*/
#include<stdio.h>
void main()
{
FILE *fp;
char name[20];
unsigned int marks;
clrscr();
fp=fopen("217","a");
printf("Enter student's name and marks:\n");
printf("Use ctrl+Z to stop entry\n");
while((scanf("%s %u",name,&marks))!=EOF)
fprintf(fp,"%s %u",name,marks);
fclose(fp);
printf("\n");
fp=fopen("217","rt");
printf(" Name Marks \n");
printf("___________________\n");
while((fscanf(fp,"%s %u",name,&marks))!=EOF)
printf(" %-10s %3u \n",name,marks);
fclose(fp);
getch()
16) DEVELOP A PROGRAM TO CREATE AND COUNT NUMBER OF CHARACTERS IN A FILE.
#include<stdio.h>
void main()
{
FILE *fp;
int c,co=0;
clrscr();
printf("Enter the chars:\n");
printf("Press ctrl+Z to stop entry\n");
fp=fopen("enter.txt","w");
while((c=getchar())!=EOF)
fputc(c,fp);
fclose(fp);
printf("\n");
fp=fopen("enter.txt","r");
while((c=fgetc(fp))!=EOF)
{
printf("%c",(char)c);
++co;
}
fclose(fp);
printf("\nThe no of chars in the file are %d",co);
getch();
}
3.Program to find the simple interest:
#include<stdio.h>
#include<conio.h>
void main()
{
int p,r,t,si;
clrscr();
printf("enter principle, Rate of
interest & time to find simple interest: ");
scanf("%d%d%d",&p,&r,&t);
si=(p*r*t)/100;
printf("simple intrest= %d",si);
getch();
}
Output:
enter principle, rate of interest &
time to find simple interest: 500
5
2
simple interest=50
4.Program to convert temperature from degree centigrade to
Fahrenheit:
#include<stdio.h>
#include<conio.h>
void main()
{
float c,f;
clrscr();
printf("enter temp in centigrade:
");
scanf("%f",&c);
f=(1.8*c)+32;
printf("temp in
Fahrenheit=%f",f);
getch();
}
Output
enter temp in centigrade: 32
temp in Fahrenheit=89.59998
5.Program to calculate sum of 5 subjects & find
percentage.
#include<stdio.h>
#include<conio.h>
void main()
{
int s1,s2,s3,s4,s5,sum,total=500;
float per;
clrscr();
printf("enter marks of 5 subjects:
");
scanf("%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5);
sum=s1+s2+s3+s4+s5;
printf("sum=%d",sum);
per=(sum*100)/total;
printf("percentage=%f",per);
getch();
}
Output:
enter marks of 5 subjects: 60
65
50
60
60
sum=300
percentage=60.000
6.Program to show swap of two no’s without using third variable
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("enter value for a & b:
");
scanf("%d%d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("after swapping the value of a
& b: %d %d",a,b);
getch();
}
Output:
enter value for a & b: 4
5
after swapping the value of a & b: 5 4
7.Program to reverse a given number:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,a,r=0;
clrscr();
printf("enter any no to get its
reverse: ");
scanf("%d",&n);
while(n>=1)
{
a=n%10;
r=r*10+a;
n=n/10;
}
printf("reverse=%d",r);
getch();
}
Output:
enter any no to get its reverse: 456
reverse=654
8.Program to find gross salary:
#include<stdio.h>
#include<conio.h>
void main()
{
int gs,bs,da,ta;
clrscr();
printf("enter basic salary: ");
scanf("%d",&bs);
da=(10*bs)/100;
ta=(12*bs)/100;
gs=bs+da+ta;
printf("gross salary=%d",gs);
getch();
}
Output:
enter basic salary: 100
gross salary=122
9.Program to print a table of any number:
#include<stdio.h>
#include<conio.h>
void main()
{
int gs,bs,da,ta;
clrscr();
printf("enter basic salary: ");
scanf("%d",&bs);
da=(10*bs)/100;
ta=(12*bs)/100;
gs=bs+da+ta;
printf("gross salary=%d",gs);
getch();
}
Output: enter a no to know table: 2
2*1=2
2*2=4
2*3=6
2*4=8
2*5=10
2*6=12
2*7=14
2*8=16
2*9=18
2*10=20
10.Program to find greatest in 3 numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("enter value of a, b & c:
");
scanf("%d%d%d",&a,&b,&c);
if((a>b)&&(a>c))
printf("a is greatest");
if((b>c)&&(b>a))
printf("b is greatest");
if((c>a)&&(c>b))
printf("c is greatest");
getch();
}
Output:
enter value for a, b& c: 5
7
4
b is greatest
11.Program to show the use of conditional operator:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("enter value for a & b:
");
scanf("%d%d",&a,&b);
(a>b)?printf("a is
greater"):printf("b is greater");
getch();
}
Output:
enter value for a & b: 5
7
b is greater
12.Program to find that entered year is leap year or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("enter any year: ");
scanf("%d",&n);
if(n%4==0)
printf("year is a leap year");
else
printf("year is not a leap
year");
getch();
}
Output:
enter any year: 1947
year is not a leap year
13.Program to find whether given no is even or odd.
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("enter any no: ");
scanf("%d",&n);
if(n%2==0)
printf("no is even");
else
printf("no is odd");
getch();
}
Output:enter any no: 5
no is odd
14.Program to shift inputed data by two bits to the left.
#include<stdio.h>
#include<conio.h
void main()
{
int x,y;
clrscr();
printf("Read the integer from keyboard
:- ");
scanf("%d",&x);
x<<=3;
y=x;
printf("\nThe left shifted data is =
%d ",y);
getch();
}
Output:
Read the integer from keyboard :- 2
The left shifted data is = 16
15.Program to use switch statement. Display Monday to Sunday:
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf("enter m for Monday\nt
for Tuesday\nw for Wednesday\nh
for Thursday\nf for Friday\ns
for Saturday\nu for Sunday);
scanf("%c",&ch);
switch(ch)
{
case 'm':
case 'M':
printf("monday");
break;
case 't':
case 'T':
printf("tuesday");
break;
case 'w':
case 'W':
printf("wednesday");
break;
case 'h':
case 'H':
printf("thusday");
break;
case 'f':
case 'F':
printf("friday");
break;
case 's':
case 'S':
printf("saturday");
break;
case 'u':
case 'U':
printf("sunday");
break;
default :
printf("wrong input");
break;
}
getch();
}
Output:enter m for Monday
t for Tuesday
w for Wednesday
h for Thursday
f for Friday
s for Saturday
u for Sunday: f
friday
16.Program to display arithmetic operator using switch case.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,n,s,m,su,d;
clrscr();
printf("enter two no's : ");
scanf("%d%d",&a,&b);
printf("enter 1 for sum\n2 for
multiply\n3for subtraction\n4 for division: ");
scanf("%d",&n);
switch(n)
{
case 1:
s=a+b;
printf("sum=%d",s);
break;
case 2:
m=a*b;
printf("multiply=%d",m);
break;
case 3:
su=a-b;
printf("subtraction=%d",su);
break;
case 4:
d=a/b;
printf("divission=%d",d);
break;
default:
printf("wrong input");
break;
}
getch();
}
Output:
enter two no’s: 8
4
enter 1 for sum
2 for multiply
3 for subtraction
4 for division: 1
sum=12
17.Program to display first 10 natural no & their sum.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,sum=0;
clrscr();
for(i=1;i<=10;i++)
{
printf("%d no is= %d\n",i,I);
sum=sum+i;
}
printf("sum =%d",sum);
getch();
}
Output:
1 no is=1
2 no is=2
3 no is=3
4 no is=4
5 no is=5
6 no is=6
7 no is=7
8 no is=8
9 no is=9
10 no is=10
sum=55
18.Program to print stars
Sequence1:---------------------------------------------------
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
printf("*");
printf("\n");
}
getch();
}
Output:
*
**
***
****
*****
19.Program to print stars
Sequence2------------------------------------------------------.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k;
clrscr();
for(i=1;i<=5;i++)
{
for(j=5;j>=i;j--)
printf(" ");
for(k=1;k<=i;k++)
printf("*");
printf("\n");
}
getch();
}
Output:
*
**
***
****
*****
20.Program to print stars Sequence3.----------------------
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k;
clrscr();
for(i=1;i<=3;i++)
{
for(j=3;j>=i;j--)
printf(" ");
{
for(k=1;k<=i*2-1;k++)
printf("*");
}
printf("\n");
}
getch();
}
Output:
*
***
*****
21.Program to print Fibonacci series up to 100.
#include<stdio.h>
#include<conio.h>
void main()
{
int a=1,b=1,c=0,i;
clrscr();
printf("%d\t%d\t",a,b);
for(i=0;i<=10;i++)
{
c=a+b;
if(c<100)
{
printf("%d\t",c);
}
a=b;
b=c;
}
getch();
}
Output:
1 1 2 3 5 8 13 21 34 55 89
22.Program to find factorial of a number.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,fact=1;
clrscr();
printf("Enter any no: ");
scanf("%d",&n);
for(i=n;i>=1;i--)
{
fact=fact*i;
}
printf("Factorial=%d",fact);
getch();
}
Output:
Enter a no: 5
Factorial=120
23.Program to find whether given no is a prime no or
not----------------------------------------.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,r=0;
clrscr();
printf("Enter any no: ");
scanf("%d",&n);
for(i=2;i<=n-1;i++)
{
if(n%i==0)
r=1;
break;
}
if(r==0)
printf("prime no");
else
printf("Not prime");
getch();
}
Output:
Enter any no: 16
Not prime
24.Program to display sum of series 1+1/2+1/3+……….+1/n.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,sum=0;
clrscr();
printf("Enter any no: ");
scanf("%d",&n);
printf("1");
for(i=2;i<=n-1;i++)
printf(" 1/%d +",i);
for(i=1;i<=n;i++)
sum=sum+i;
printf(" 1/%d",n);
printf("\nSum=1/%d",sum+1/n);
getch();
}
Output:
Enter any no: 7
1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 + 1/7
Sum=1/28
25.Program to add two number using
pointers----------------------------------------------------------
#include<stdio.h>
#include<conio.h>
void main()
{
int *p1,*p2,sum;
clrscr();
printf("enter two no's: ");
scanf("%d%d",&*p1,&*p2);
sum=*p1+*p2;
printf("sum=%d",sum);
getch();
}
Output:
enter two no’s: 10
20
sum=30
26.Program to show sum of 10 elements of array & show the
average-----------------------------.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,sum=0;
float av;
clrscr();
printf("enter elements of an aaray:
");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
for(i=0;i<10;i++)
sum=sum+a[i];
printf("sum=%d",sum);
av=sum/10;
printf(“average=%.2f”,av);
getch();
}
Output:
enter elements of an array: 4
5
6
1
2
3
5
5
4
7
sum=42
average=4.22
27.Program to find the maximum no in an array.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],max,i;
clrscr();
printf("enter element for the array:
");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
max=a[0];
for(i=1;i<5;i++)
{
if(max<a[i])
max=a[i];
}
printf("maximum no= %d",max);
getch();
}
Output:
enter elements for array: 5
4
7
1
2
maximum no= 7
28.Program to display series and find sum of 1+3+5+……..+n.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,sum=0;
clrscr();
printf("Enter any no: ");
scanf("%d",&n);
for(i=1;i<n;i=i+2)
{
printf("%d+",i);
sum=sum+i;
}
printf("%d",n);
printf("\nsum=%d",sum+n);
getch();
}
Output:
Enter any no: 7
1+3+5+7
Sum=16
29.Program to display a
matrix----------------------------------------------------------
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][2],b[3][2],i,j;
clrscr();
printf("enter value for a matrix:
");
for(i=0;i♥;i++)
{
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);
}
printf("enter value for b matrix:
");
for(i=0;i♥;i++)
{
for(j=0;j<2;j++)
scanf("%d",&b[i][j]);
}
printf("\na matrix is\n\n");
for(i=0;i♥;i++)
{
for(j=0;j<2;j++)
{
printf(" %d ",a[i][j]);
}
printf("\n");
}
printf("\nb matrix is\n\n");
for(i=0;i♥;i++)
{
for(j=0;j<2;j++)
{
printf(" %d ",b[i][j]);
}
printf("\n");
}
getch();
}
Output:
enter value for a matrix: 7
8
9
4
5
6
enter value for b matrix: 3
2
1
4
5
6
a matrix is
7 8
9 4
5 6
b matrix is
3 2
1 4
5 6
30.Program to find sum of two
matrices----------------------------------------------------------.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][2],b[3][2],c[3][2],i,j;
clrscr();
printf("Enter value for 1 matrix:
");
for(i=0;i♥;i++)
{
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);
}
printf("Enter value for 2 matrix:
");
for(i=0;i♥;i++)
{
for(j=0;j<2;j++)
scanf("%d",&b[i][j]);
}
for(i=0;i♥;i++)
{
for(j=0;j<2;j++)
c[i][j]=a[i][j]+b[i][j];
}
printf("Sum of matrix is\n");
for(i=0;i♥;i++)
{
for(j=0;j<2;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
}
Output:
Enter value for 1 matrix: 1
2
3
4
5
6
Enter value for 2 matrix: 4
5
6
1
3
2
Sum of matrix is
5 7
9 5
8 8
31.Program to find subtraction of two
matrices------------------------------------------------
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],b[5],c[5],i;
clrscr();
printf("enter value for array a
");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
printf("enter value for array b
");
for(i=0;i<5;i++)
scanf("%d",&b[i]);
for(i=0;i<5;i++)
c[i]=a[i]-b[i];
printf("subtraction");
for(i=0;i<5;i++)
printf(" %d ",c[i]);
getch();
}
Output:
enter value for array a: 7
8
9
4
5
enter value for array b: 4
5
6
1
2
subtraction 3 3 3 3 3
32.Program to find multiplication of two matrices--------------------------------------------------.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][2],b[3][2],c[3][2],i,j;
clrscr();
printf("enter value for 1 matrix:
");
for(i=0;i♥;i++)
{
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);
}
printf("enter value for 2 matrix:
");
for(i=0;i♥;i++)
{
for(j=0;j<2;j++)
scanf("%d",&b[i][j]);
}
for(i=0;i♥;i++)
{
for(j=0;j<2;j++)
c[i][j]=a[i][j]*b[i][j];
}
printf("matrix is\n");
for(i=0;i♥;i++)
{
for(j=0;j<2;j++)
{
printf(" %d ",c[i][j]);
}
printf("\n");
}
getch();
}
Output:enter value for 1 matrix: 7
8
9
4
5
6
enter value for 2 matrix: 3
2
1
2
5
6
matrix is
21 16
9 8
25 36
33.Program to find transpose of a
matrix--------------------------------------------------------------------.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][2],b[2][3],i,j;
clrscr();
printf("Enter value for matrix:
");
for(i=0;i♥;i++)
{
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);
}
printf("Matrix:\n");
for(i=0;i♥;i++)
{
for(j=0;j<2;j++)
printf(" %d ",a[i][j]);
printf("\n");
}
for(i=0;i♥;i++)
{
for(j=0;j<2;j++)
b[j][i]=a[i][j];
}
printf("Transpose matrix:\n");
for(i=0;i<2;i++)
{
for(j=0;j♥;j++)
printf(" %d ",b[i][j]);
printf("\n");
}
getch();
}
Output:Enter value for matrix: 4
5
6
1
2
3
Matrix:
4 5
6 1
2 3
Transpose matrix:
4 6 2
5 1 3
34.Program to find square of a number using
functions---------------------------------------------------------------.
#include<stdio.h>
#include<conio.h>
void main()
{
int rev(int);
int r,a;
clrscr();
printf("enter any no: ");
scanf("%d",&a);
r=rev(a);
printf("square is : %d",r);
getch();
}
int rev(int x)
{
return(x*x);
}
Output:
enter any no: 5
square is : 25
35.Program to swap two numbers using functions----------------------------------------------------------------------.
#include<stdio.h>
#include<conio.h>
void main()
{
void swap(int,int);
int a,b,r;
clrscr();
printf("enter value for a&b:
");
scanf("%d%d",&a,&b);
swap(a,b);
getch();
}
void swap(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
printf("after swapping the value for a
& b is : %d %d",a,b);
}
Output:
enter value for a&b: 4
5
after swapping the value for a & b : 5
4
36.Program to find factorial of a number using
functions---------------------------------------------------------------
#include<stdio.h>
#include<conio.h>
void main()
{
int a,f;
int fact(int);
clrscr();
printf("enter a no: ");
scanf("%d",&a);
f=fact(a);
printf("factorial= %d",f);
getch();
}
int fact(int x)
{
int fac=1,i;
for(i=x;i>=1;i--)
fac=fac*i;
return(fac);
}
Output:
enter a no: 5
factorial=120
37.Program to show table of a number using functions.
#include<stdio.h>
#include<conio.h>
void main()
{
void table();
clrscr();
table();
getch();
}
void table()
{
int n,i,r;
printf("enter a no to know table:
");
scanf("%d",&n);
for(i=1;i<=10;i++)
{
r=n*i;
printf("%d*%d=%d\n",n,i,r);
}
}
Output:
enter a no to know table: 2
2*1=2
2*2=4
2*3=6
2*4=8
2*5=10
2*6=12
2*7=14
2*8=16
2*9=18
2*10=20
38.Program to show call by value……………………………………………………………………..
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,swap();
clrscr();
a=5;
b=10;
printf("value of a=%d & value of
b=%d before swap ",a,b);
swap(a,b);
printf("\nvalue of a =%d & b=%d
after swap",a,b);
getch();
}
int swap(int x,int y)
{
int temp;
temp=x;
x=y;
y=temp;
}
Output:
value of a=5 & value of b=10 before
swap
value of a=5 & b=10 after swap
39.Program to show call by reference……………………………………………………………….
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,*aa,*bb,swap();
clrscr();
a=5;
b=10;
aa=&a;
bb=&b;
printf("value of a= %d & value of
b=%d before swap",a,b);
swap(aa,bb);
printf("\nvalue of a=%d & b=%d
after swap",a,b);
getch();
}
int swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
Output:
value of a= 5 & value of b=10 before
swap
value of a=10 & b=5 after swap
40.Program to find largest of two numbers using
functions…………………………………………..
#include<stdio.h>
#include<conio.h>
void main()
{
void max();
clrscr();
max();
getch();
}
void max()
{
int a[5],max,n,i;
printf("How many no's you want to
enter: ");
scanf("%d",&n);
printf("Enter element for the array:
");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
max=a[0];
for(i=1;i<5;i++)
{
if(max<a[i])
max=a[i];
}
printf("maximum no= %d",max);
Output:
How many no’s you want to enter: 4
Enter element for array: 4
5
6
1
maximum no: 6
41.Program to find factorial of a number using recursion.
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("enter number: ");
scanf("%d",&n);
if(n<0)
printf("invalid number");
else
printf("%d!=%d",n,fact(n));
getch();
}
int fact(int x)
{
if(x==0)
return 1;
else
return(x*fact(x-1));
Output:
enter number: 5
5!=12
42.Program to find whether a string is palindrome or not.
#include<stdio.h>
#include<conio.h>
void main()
{
char s1[20],s2[20];
clrscr();
printf("enter a string: ");
scanf("%s",s1);
strcpy(s2,s1);
strrev(s2);
if(strcmp(s1,s2)==0)
printf("string is a palindrome");
else
printf("not a palindrome
string");
getch();
}
Output:
enter a string: abc
not a palindrome string
43.c program to print the number of digits in a decimal number
#include<stdio.h>
int main()
{
int n,count=0,x;
printf("ENter the decimal
number\n");
scanf("%d",&n);
x=n;
while(n>0)
{
count++;
n=n/10;
}
printf("Number %d has %d
digits\n",x,count);
}
output
ENter the decimal number
2345
Number 2345 has 4 digits
44.reading of five book details and print using structure
#include<stdio.h>
#include<conio.h>
struct book
{
int page;
char bname[20];
float price;
}b[5];
void main()
{
int i;
clrscr();
for(i=0;i<5;i++)
{
printf("enter the bookname");
scanf("%s",b[i].bname);
printf("enter the no.of book
pages");
scanf("%d",&b[i].page);
printf("enter book price");
scanf("%f",&b[i].price);
}
for(i=0;i<5;i++)
{
printf("%dbook name
=%s",i+1,b[i].bname);
printf("book page=%d",b[i].page);
printf("book
price=%f",b[i].price);
}
getch();
45.Enter book details using structure
#include<stdio.h>
#include<conio.h>
struct book
{
int pages;
char bname[20];
float price;
}b;
void main()
{
clrscr();
printf("enter the pages");
scanf("%d",&b.pages);
printf("enter the b.name");
scanf("%s",&b.bname);
printf("enter the price");
scanf("%f",&b.price);
}
46.Print any five integre numbers in a file useing putw and
fgetw functions*/
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
int i;
int number;
clrscr();
fp=fopen("number","w");
for(i=0;i<5;i++)
{
printf("Enter the number");
scanf("%d",&number);
putw(i,fp);
}
fclose(fp);
getch();
}
46.print data into file using fprintf function
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char name[20];
char course[20];
int age;
fp=fopen("sample.dat","w");
printf("Enter the name");
gets(name);
printf("Enter course");
gets(course);
printf("Enter age");
scanf("%d",&age);
fprintf(fp,"%s %s %d
",name,course,age);
fclose(fp);
}
47.Write data into file using fwrite and fread functions
#include<stdio.h>
void main()
{
FILE *fp;
char str[]="This is my first
program";
char str1[30];
if((fp=fopen("kiran.txt","w+"))==NULL)
{
printf("Unable to open file");
exit(0);
}
fwrite(str,strlen(str)+1,1,fp);
fseek(fp,SEEK_SET,0);
fread(str1,strlen(str)+1,1,fp);
printf("%s",str1);
fclose(fp);
getch();
}
48.find file size using fseek and ftell functions
#include <stdio.h>
int filesize(FILE *stream);
void main()
{
FILE *stream;
int len;
stream = fopen("MYFILE.TXT",
"w+");
fprintf(stream, "This is a
test");
len=filesize(stream);
printf("Filesize of MYFILE.TXT is %d
bytes\n",len);
fclose(stream);
getch();
}
int filesize(FILE *stream)
{
int length;
fseek(stream, 0L,2);
length = ftell(stream);
return length;
}
49. to count no. of characters,no. of blanks,no. of words &
no.
of lines in a multi line string
#include <stdio.h>
#include <conio.h>
void main()
{
char c,choice;
int nc=0,nb=0,nw=1,nl=1,count,i/*,flag=1*/;
clrscr();
scanf("%c",&choice);
printf("ENTER STRING:- ");
printf("
String will be terminated when you press
Ctrl-Z.");
printf("
STRING:- ");
while ((c=getchar())!=EOF)
{
switch(1)
{
case 1:
if (c==EOF||c==' '||c=='')
;
else
nc=nc+1;
case 2:
if (c==' ')
{
nc=nc+1;
nb=nb+1;
while((c=getchar())==' ')
{
nb=nb+1;
nc=nc+1;
}
if (c!=' ')
{
nc=nc+1;
nw=nw+1;
}
}
case 3:
if(c=='')
{
nc=nc+1;
nb=nb+1;
nw=nw+1;
nl=nl+1;
}
}//SWITCH
}//WHILE
printf("no. of characters is
%d",nc);
printf("no. of blanks is %d",nb);
printf("no. of words is %d",nw);
printf("no. of lines is %d",nl);
fflush(stdin);
printf ("Do you want to
continue?(y/n):- ");
scanf("%c",&choice);
getch();
}
50.Program to Calculate the Pascal triangle
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10];
int i,j,c,n;
clrscr();
printf("Enter how many lines do you want");
scanf("%d",&n);
a[1][1]=1;
printf("%5d",a[1][1]);
a[2][1]=1;a[2][2]=2;a[2][3]=1;
printf("%d %d
%d",a[2][1],a[2][2],a[2][3]);
for(i=3;i<=n;i++)
{
a[i][1]=1;
printf("%d",a[i][1]);
j=2;c=3;
while(j<=i)
{
a[i][j]=a[i-1][c-1]+a[i-1][c-2];
printf("%5d",a[i][j]);
c=c+1;
j=j+1;
}
a[i][j]=1;
printf("%d",a[i][j]);
}
}
51.Program to Convert Numbers into Words
#include<stdio.h>
void pw(long,char[]);
char *one[]={" ","
one"," two"," three"," four","
five"," six"," seven","
eight"," Nine","
ten"," eleven"," twelve"," thirteen","
fourteen","
fifteen"," sixteen","
seventeen"," eighteen"," nineteen"};
char *ten[]={" ","
"," twenty"," thirty"," forty","
fifty"," sixty","
seventy"," eighty","
ninety"};
void main()
{
long n;
clrscr();
printf("
Enter any 9 digit no: ");
scanf("%9ld",&n);
if(n<=0)
printf("Enter numbers greater than
0");
else
{
pw((n/10000000),"crore");
pw(((n/100000)%100),"lakh");
pw(((n/1000)%100),"thousand");
pw(((n/100)%10),"hundred");
pw((n%100)," ");
}
getch();
}
void pw(long n,char ch[])
{
(n>19)?printf("%s %s
",ten[n/10],one[n%10]):printf("%s ",one[n]);
if(n)printf("%s ",ch);
}
52.Program for Prime Number Generation
--------------------------------------------------------------------------------------------------------------
#include <stdio.h>
main()
{
int n,i=1,j,c;
clrscr();
printf("Enter Number Of Terms");
printf("Prime Numbers Are
Follwing");
scanf("%d",&n);
while(i<=n)
{
c=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
c++;
}
if(c==2)
printf("%d ",i)
i++;
}
getch();
Ur’s
MANI VARA PRASADA REDDY.