Multiplication of two matrices
#include<stdio.h>
#include<conio.h>
int main(){
int a[5][5],b[5][5],c[5][5],i,j,k,sum=0,m,n,o,p;
printf("\nEnter the row and column of first matrix");
scanf("%d %d",&m,&n);
printf("\nEnter the row and column of second matrix");
scanf("%d %d",&o,&p);
if(n!=o){
printf("Matrix mutiplication is not possible");
printf("\nColumn of first matrix must be same as row of second matrix");
}
else{
printf("\nEnter the First matrix->");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nEnter the Second matrix->");
for(i=0;i<o;i++)
for(j=0;j<p;j++)
scanf("%d",&b[i][j]);
printf("\nThe First matrix is\n");
for(i=0;i<m;i++){
printf("\n");
for(j=0;j<n;j++){
printf("%d\t",a[i][j]);
}
}
printf("\nThe Second matrix is\n");
for(i=0;i<o;i++){
printf("\n");
for(j=0;j<p;j++){
printf("%d\t",b[i][j]);
}
}
for(i=0;i<m;i++)
for(j=0;j<p;j++)
c[i][j]=0;
for(i=0;i<m;i++){ //row of first matrix
for(j=0;j<p;j++){ //column of second matrix
sum=0;
for(k=0;k<n;k++)
sum=sum+a[i][k]*b[k][j];
c[i][j]=sum;
}
}
}
printf("\nThe multiplication of two matrix is\n");
for(i=0;i<m;i++){
printf("\n");
for(j=0;j<p;j++){
printf("%d\t",c[i][j]);
}
}
getch();
return 0;
}
TO DELETE "THREE", "BAD" AND "TIME"
#include<stdio.h>
#include<string.h>
int main( )
{
char str[20];
FILE *ptvar1, *ptvar2;
ptvar1=fopen("data1.txt","r");
ptvar2=fopen("data2.txt","w");
while(!feof(ptvar1))
{
fscanf(ptvar1,"%s",str);
if(strcmp(str, "three")==0 || strcmp(str, "bad")==0 || strcmp(str, "time")==0);
continue;
else
fprintf(ptvar2,"%s",str);
}
fclose(ptvar1);
fclose(ptvar2);
return 0;
}
(*the alignments are not proper, please standardize the alignment to run the program)
TO COPY A FILE
#include <stdio.h>
#include <stdlib.h>
main()
{
char ch, source_file[20], target_file[20];
FILE *fp1, *fp2;
printf("Enter name of file to copy\n");
gets(source_file);
fp1 = fopen(source_file, "r");
if( fp1 == NULL )
{
printf("Error\n");
}
printf("Enter name of target file\n");
gets(target_file);
fp2= fopen(target_file, "w");
if( fp2 == NULL )
{
fclose(fp1);
printf("Error\n");
}
while( ( ch = fgetc(fp1) ) != EOF )
fputc(ch, fp2);
printf("File copied successfully.\n");
fclose(fp1);
fclose(fp2);
return 0;
}
To calculate the sum, difference, product, quotient and remainder
#include<stdio.h>
#include<conio.h>
void main()
{ int a,b,sum,dif,pro,q,r;
clrscr();
printf("input enter a first greater number ",a);
scanf("%d",&a);
printf("input enter a second smanller number ",b);
scanf("%d",&b);
sum=a+b;
dif=a-b;
pro=a*b;
q=a/b;
r=a%b;
printf("sum of two no= %d",sum);
printf("\ndiffferenceoftwo no= %d",dif);
printf("\nproductoftwo no= %d",pro);
printf("\nquotientoftwo no= %d",q);
printf("\nremainder of two no= %d",r);
getch();
}
To input any two numbers and finding out the greater one
#include<stdio.h>
#include<conio.h>
void main()
{ int num1, num2;
clrscr();
printf(“Enter the first and second number:\n”);
scanf(“%d%d”, &num1, &num2);
if(num1>num2)
printf(“\n%d is greater than %d”, num1, num2);
else
printf(“\n%d is greater than %d”, num2, num1);
getch();
}
To find out whether the number is odd or even
#include<stdio.h>
#include<conio.h>
void main()
{ int num;
clrscr();
printf(“Enter any number:\n”);
scanf(“%d”,&num);
if((num%2)==0)
printf(“\nThe given number is even”);
else
printf(“\nThe given number is odd”);
getch();
}
To generate the multiplication table
#include<stdio.h>
#include<conio.h>
void main()
{ int i, num;
clrscr();
printf(“Enter any number:”);
scanf(“%d”,&num);
for(i=1;i<=10;i++)
printf(“%d X %d = %d”,num,i,num*i);
getch();\
}
To calculate the percentage and division of a student
#include(stdio.h)
#include(conio.h)
void main()
{ float marks, per;
printf("\nEnter the total marks:");
scanf("%d,&marks);
per=marks/5;
if(per>=80)
{ printf("distinction"); }
else if(per>=60 && per<80)
{ printf("\n first division"); }
else if(per>=50 &&per<60)
{ printf("\nsecond division"); }
else if(per>=40 &&per<50)
{ printf("\n pass division"); }
else
{ printf("\n fail"); }
printf("\nThe percentage is %f",per);
getch();
}
To calculate the area and perimeter of a rectangle
#include<stdio.h>
#include<conio.h>
void main()
{ float l,b,area,perimeter;
printf("Enter length and breadth of rectangle");
scanf("%f%f",&l,&b);
area=l*b;
perimeter=2*(l+b);
printf("\nThe area of rectangle is %f\nThe perimeter of rectangle is
%f",area,perimeter);
getch();
}
To swap the values
#include<conio.h>
#include<stdio.h>
void main()
{ int temp, num1, num2;
clrscr();
printf(“Enter two numbers:\n”);
scanf(“%d%d”,&num1,&num2);
printf(“The given numbers are %d and %d”,num1,num2);
temp=num1;
num1=num2;
num2=temp;
printf(“The numbers after swapping are %d and %d”,num1,num2);
getch();
}
To swap the numbers using bubble sort method
#include<stdio.h>
#include<conio.h>
void main()
{ int i,j,n,temp;
intnum[]={4,9,3,7,1,8,6};
n=7;
clrscr();
for(i=0;i<n;i++)
{ for(j=0;j<n-1;j++)
{ if(num[j]>=num[j+1])
{ temp=num[j];
num[j]=num[j+1];
num[j+1]=temp;
}
}
}
for(i=0;i<n;i++)
printf(“%d\t”,num[i]);
getch();
}
To swap the numbers using selection sort method
#include<stdio.h>
#include<conio.h>
void main()
{ int i,j,n,p,temp,s=0;
intnum[]={4,9,3,7,1,8,6};
n=7;
clrscr();
for(i=0;i<n-1;i++)
{ s=num[i];
for(j=i;j<n;j++)
{ if(num[j]<=s)
{ s=num[j];
p=j;
}
}
temp=num[i];
num[i]=s;
num[p]=temp;
}
for(i=0;i<n;i++)
printf(“%d\t”,num[i]);
getch();
}
To arrange numbers in ascending order
#include<stdio.h>
#include<conio.h>
void main()
{ int i,k,int x[10],temp;
clrscr();
printf("\n Enter the numbers:\n");
for(i=0;i<10;i++)
scanf("%d",&x[i]);
for(i=0;i<10;i++)
{ for(k=0;k<10;k++)
{ if(x[k]>x[k+1])
{ temp=x[k];
x[k]=x[k+1];
x[k+1]=temp;
}
}
}
printf("\nThe numbers in ascending order are:\n”);
for(i=0;i<10;i++)
printf(“%d\t",x[i]);
getch();
}
To find HCF
#include<stdio.h>
#include<conio.h>
void main()
{ int num1,num2,temp;
printf("Enter two numbers:\n");
scanf("%d%d",&num1,&num2);
while(num2!=0)
{ temp=num1%num2;
num1=num2;
num2=temp;
}
printf("\nHCF is %d",num1);
getch();
}
To find the factorial of given number
#include<stdio.h>
#include<conio.h>
void main()
{ int n,i;
longint fact;
clrscr();
fact=1;
printf("enter the number");
scanf("%d", &n);
for(i=1; i<=n; i++)
fact=fact*i;
printf("The factorial of %d is %d", n,fact);
getch();
}
To find the Fibonacci series of given number
#include<stdio.h>
#include<conio.h>
void main()
{ clrscr();
int n, i=0, j=1, k , a;
printf("Enter the number:");
scanf("%d", &n);
printf("\nfibonacci series is 0 1");
for(a=3; a<=n; a++)
{ k=i+j;
i=j;
j=k;
printf("\t %d", k);
}
getch();
}
To reverse the user input integer
#include<stdio.h>
#include<conio.h>
void main()
{ int num, rem, temp=0;
clrscr();
printf(“Enter any number:\n”);
scanf(“%d”,&num);
while(num!=0)
{ rem=num%10;
temp=(temp*10)+rem;
num=num/10;
}
printf(“The reversed number is %d”,temp);
getch();
}
To reverse the user input string
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{ char string1[20], temp;
int len,i,j;
clrscr();
printf(“Enter the string”);
scanf(“%s”,string1);
len=strlen(string1);
for(i=0,j=len-1;i<len,j>(len-1)/2;i++,j--)
{ temp=string1[i];
string1[i]=string1[j];
string1[j]=temp;
}
printf(“The reversed string is %s”,string1);
getch();
}
To calculate the sum of two 2X2 matrices
#include<stdio.h>
#include<conio.h>
void main()
{ int a[2][2],b[2][2],c[2][2],i,j;
for(i=0;i<2;i++)
for(j=0;j<2;j++)
{ printf("Enter the number"); scanf("%d",&a[i][j]);
}
for(i=0;i<2;i++)
{ for(j=0;j<2;j++)
printf("%d\t",a[i][j]); printf("\n");
}
for(i=0;i<2;i++)
for(j=0;j<2;j++)
{ printf("Enter the number"); scanf("%d",&b[i][j]);
}
for(i=0;i<2;i++)
{ for(j=0;j<2;j++)
printf("%d\t",b[i][j]); printf("\n");
}
for(i=0;i<2;i++)
{ for(j=0;j<2;j++)
{ c[i][j]=a[i][j]+b[i][j];
}
}
for(i=0;i<2;i++)
{ for(j=0;j<2;j++)
printf("%d\t",c[i][j]); printf("\n\n");
}
getch();
}
To multiply two 2X2 matrices
#include<stdio.h>
#include<conio.h>
void main()
{ int a[2][2],b[2][2],c[2][2],i,j,k;
for(i=0;i<2;i++)
for(j=0;j<2;j++)
{ printf("Enter the number of matrix a");
scanf("%d",&a[i][j]);
}
for(i=0;i<2;i++)
{ for(j=0;j<2;j++)
printf("%d\t",a[i][j]);
printf("\n"); }
printf("\n\n");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
{ printf("Enter the number of matrix b");
scanf("%d",&b[i][j]);
}
for(i=0;i<2;i++)
{ for(j=0;j<2;j++)
printf("%d\t",b[i][j]);
printf("\n"); }
for(i=0;i<2;i++)
{ for(j=0;j<2;j++)
{ c[i][j]=0;
for(k=0;k<2;k++)
{ c[i][j]=(c[i][j]+(a[i][k]*b[k][j]));
}
}
}
}
}
printf("\n\n");
printf("\nThe multiplication of matrix a and matrix b is:\n");
for(i=0;i<2;i++)
{ for(j=0;j<2;j++)
printf("%d\t",c[i][j]);
printf("\n");
}
getch();
}
To find the power of user input number
#include<stdio.h>
#include<conio.h>
int power(int,int);
void main()
{ int n,p;
clrscr();
printf("enter the base number");
scanf("%d",&n);
printf("Enter the power number");
scanf("%d",&p);
printf("\n%d^%d is %d\n",n,p,power(n,p));
getch();
}
int power(intn,int p)
{ int i;
int c=n;
for(i=1;i<p;i++)
n=n*c;
return n;
}
To display the user input sentences removing all the spaces
#include<stdio.h>
#include<conio.h>
void main()
{ char line[50],c;
int i=0;
printf(“Enter the sentence:\n”);
gets(line);
while((c=(line[i++]))!=‘.’)
{ if(c==‘ ’)
continue;
else
printf(“%c”,c);
}
printf(“.”);
getch();
}
To count the number of words in a sentence
#include<stdio.h>
#include<conio.h>
void main()
{ char line[80],c;
int i=0,j=1;
printf(“Enter the sentence:”);
gets(line);
while((c=(line[i++]))!=‘.’)
{ if(c==‘ ’)
j=j+1;
}
printf(“\n%d”,j);
getch();
}
Use of pointer passing by reference
#include<stdio.h>
#include<conio.h>
void swap(int *,int *);
void main()
{ int a=6,b=10;
swap(&a,&b);
printf("The values after swapping are a=%d,b=%d",a,b);
getch();
}
void swap(int*a,int*b)
{ int *temp;
*temp=*a;
*a=*b;
*b=*temp;
}
Use of pointer passing by value
#include<stdio.h>
#include<conio.h>
void swap(int,int);
void main()
{ int a=6,b=10;
swap(a,b);
getch();
}
void swap(inta,int b)
{ int temp;
temp=a;
a=b;
b=temp;
printf("The value after swapping are a=%d and b=%d",a,b);
}
To enter the details of a book and display it using structure
#include<stdio.h>
#include<conio.h>
void main()
{ struct book
{ charbookname[1];
char author[1];
char publisher[1];
intrate,page;
};
int i;
struct book b1[1];
for(i=0;i<1;i++)
{ printf("\nenter the bookname:");
scanf("%s",b1[i].bookname);
printf("\nenter the author name");
scanf("%s",b1[i].author);
printf("\nenter the publisher name");
scanf("%s",b1[i].publisher);
printf("\nenter the rate");
scanf("%d",&b1[i].rate);
printf("\nenter the page no");
scanf("%d",&b1[i].page);
}
printf("\nbookname\tauthorname\tpublishername\trate\tpageno.");
for(i=0;i<1;i++)
printf("\n%s\t\t%s\t\t%s\t\t%d\t%d\t",b1[i].bookname,b1[i].author,b1[i].publ
isher,b1[i].rate,b1[i].page);
getch();
}
To enter the detail of a person and store the record in a file
#include<stdio.h>
void main()
{ FILE *file;
file=fopen(“abc.txt”, “a+”);
char name[15], add[15];
//reading the file
while(!feof(file))
{ fscanf(file, “\n%s”,name);
printf(“\n%s”,name);
fscanf(file,”\t\t%s”,add);
printf(“\t\t%s”,add);
}
// ading new record
printf(“\n\n Enter name”\n”);
scanf(“%s”,name);
fprintf(file,”\n%s”,name);
printf(“\n Enter addres:\n”);
scanf(“%s”,add);
fprintf(file,”\t\t%s”,add);
fclose(file);
}
To draw a line
#include<stdio.h>
#include<graphics.h>
void main()
{ int gd=DETECT,gm; initgraph(&gd,&gm,”c:\\tc\\bgi”);
line(200,100,150,300);
setcolor(WHITE);
getch();
closegraph();
}
To draw a Circle
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{ int gd=DETECT,gm;
initgraph(&gd,&gm,”c:\\tc\\bgi”);
circle(200,200,100);
setcolor(WHITE);
getch();
closegraph();
}
To draw a rectangle
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{ int gd=DETECT,gm;
initgraph(&gd,&gm,”c:\\tc\\bgi”);
rectangle(200,100,150,300);
setcolor(WHITE);
getch();
closegraph();
}
To draw a triangle
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{ int gd=DETECT,gm;
initgraph(&gd,&gm,”c:\\tc\\bgi”);
line(200,100,200,300);
line(200,100,400,300);
line(200,300,400,300);
setcolor(WHITE);
getch();
closegraph();
}
MORE………………..
#include<stdio.h>
#include<conio.h>
int main(){
int a[5][5],b[5][5],c[5][5],i,j,k,sum=0,m,n,o,p;
printf("\nEnter the row and column of first matrix");
scanf("%d %d",&m,&n);
printf("\nEnter the row and column of second matrix");
scanf("%d %d",&o,&p);
if(n!=o){
printf("Matrix mutiplication is not possible");
printf("\nColumn of first matrix must be same as row of second matrix");
}
else{
printf("\nEnter the First matrix->");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nEnter the Second matrix->");
for(i=0;i<o;i++)
for(j=0;j<p;j++)
scanf("%d",&b[i][j]);
printf("\nThe First matrix is\n");
for(i=0;i<m;i++){
printf("\n");
for(j=0;j<n;j++){
printf("%d\t",a[i][j]);
}
}
printf("\nThe Second matrix is\n");
for(i=0;i<o;i++){
printf("\n");
for(j=0;j<p;j++){
printf("%d\t",b[i][j]);
}
}
for(i=0;i<m;i++)
for(j=0;j<p;j++)
c[i][j]=0;
for(i=0;i<m;i++){ //row of first matrix
for(j=0;j<p;j++){ //column of second matrix
sum=0;
for(k=0;k<n;k++)
sum=sum+a[i][k]*b[k][j];
c[i][j]=sum;
}
}
}
printf("\nThe multiplication of two matrix is\n");
for(i=0;i<m;i++){
printf("\n");
for(j=0;j<p;j++){
printf("%d\t",c[i][j]);
}
}
getch();
return 0;
}
TO DELETE "THREE", "BAD" AND "TIME"
#include<stdio.h>
#include<string.h>
int main( )
{
char str[20];
FILE *ptvar1, *ptvar2;
ptvar1=fopen("data1.txt","r");
ptvar2=fopen("data2.txt","w");
while(!feof(ptvar1))
{
fscanf(ptvar1,"%s",str);
if(strcmp(str, "three")==0 || strcmp(str, "bad")==0 || strcmp(str, "time")==0);
continue;
else
fprintf(ptvar2,"%s",str);
}
fclose(ptvar1);
fclose(ptvar2);
return 0;
}
(*the alignments are not proper, please standardize the alignment to run the program)
TO COPY A FILE
#include <stdio.h>
#include <stdlib.h>
main()
{
char ch, source_file[20], target_file[20];
FILE *fp1, *fp2;
printf("Enter name of file to copy\n");
gets(source_file);
fp1 = fopen(source_file, "r");
if( fp1 == NULL )
{
printf("Error\n");
}
printf("Enter name of target file\n");
gets(target_file);
fp2= fopen(target_file, "w");
if( fp2 == NULL )
{
fclose(fp1);
printf("Error\n");
}
while( ( ch = fgetc(fp1) ) != EOF )
fputc(ch, fp2);
printf("File copied successfully.\n");
fclose(fp1);
fclose(fp2);
return 0;
}
To calculate the sum, difference, product, quotient and remainder
#include<stdio.h>
#include<conio.h>
void main()
{ int a,b,sum,dif,pro,q,r;
clrscr();
printf("input enter a first greater number ",a);
scanf("%d",&a);
printf("input enter a second smanller number ",b);
scanf("%d",&b);
sum=a+b;
dif=a-b;
pro=a*b;
q=a/b;
r=a%b;
printf("sum of two no= %d",sum);
printf("\ndiffferenceoftwo no= %d",dif);
printf("\nproductoftwo no= %d",pro);
printf("\nquotientoftwo no= %d",q);
printf("\nremainder of two no= %d",r);
getch();
}
To input any two numbers and finding out the greater one
#include<stdio.h>
#include<conio.h>
void main()
{ int num1, num2;
clrscr();
printf(“Enter the first and second number:\n”);
scanf(“%d%d”, &num1, &num2);
if(num1>num2)
printf(“\n%d is greater than %d”, num1, num2);
else
printf(“\n%d is greater than %d”, num2, num1);
getch();
}
To find out whether the number is odd or even
#include<stdio.h>
#include<conio.h>
void main()
{ int num;
clrscr();
printf(“Enter any number:\n”);
scanf(“%d”,&num);
if((num%2)==0)
printf(“\nThe given number is even”);
else
printf(“\nThe given number is odd”);
getch();
}
To generate the multiplication table
#include<stdio.h>
#include<conio.h>
void main()
{ int i, num;
clrscr();
printf(“Enter any number:”);
scanf(“%d”,&num);
for(i=1;i<=10;i++)
printf(“%d X %d = %d”,num,i,num*i);
getch();\
}
To calculate the percentage and division of a student
#include(stdio.h)
#include(conio.h)
void main()
{ float marks, per;
printf("\nEnter the total marks:");
scanf("%d,&marks);
per=marks/5;
if(per>=80)
{ printf("distinction"); }
else if(per>=60 && per<80)
{ printf("\n first division"); }
else if(per>=50 &&per<60)
{ printf("\nsecond division"); }
else if(per>=40 &&per<50)
{ printf("\n pass division"); }
else
{ printf("\n fail"); }
printf("\nThe percentage is %f",per);
getch();
}
To calculate the area and perimeter of a rectangle
#include<stdio.h>
#include<conio.h>
void main()
{ float l,b,area,perimeter;
printf("Enter length and breadth of rectangle");
scanf("%f%f",&l,&b);
area=l*b;
perimeter=2*(l+b);
printf("\nThe area of rectangle is %f\nThe perimeter of rectangle is
%f",area,perimeter);
getch();
}
To swap the values
#include<conio.h>
#include<stdio.h>
void main()
{ int temp, num1, num2;
clrscr();
printf(“Enter two numbers:\n”);
scanf(“%d%d”,&num1,&num2);
printf(“The given numbers are %d and %d”,num1,num2);
temp=num1;
num1=num2;
num2=temp;
printf(“The numbers after swapping are %d and %d”,num1,num2);
getch();
}
To swap the numbers using bubble sort method
#include<stdio.h>
#include<conio.h>
void main()
{ int i,j,n,temp;
intnum[]={4,9,3,7,1,8,6};
n=7;
clrscr();
for(i=0;i<n;i++)
{ for(j=0;j<n-1;j++)
{ if(num[j]>=num[j+1])
{ temp=num[j];
num[j]=num[j+1];
num[j+1]=temp;
}
}
}
for(i=0;i<n;i++)
printf(“%d\t”,num[i]);
getch();
}
To swap the numbers using selection sort method
#include<stdio.h>
#include<conio.h>
void main()
{ int i,j,n,p,temp,s=0;
intnum[]={4,9,3,7,1,8,6};
n=7;
clrscr();
for(i=0;i<n-1;i++)
{ s=num[i];
for(j=i;j<n;j++)
{ if(num[j]<=s)
{ s=num[j];
p=j;
}
}
temp=num[i];
num[i]=s;
num[p]=temp;
}
for(i=0;i<n;i++)
printf(“%d\t”,num[i]);
getch();
}
To arrange numbers in ascending order
#include<stdio.h>
#include<conio.h>
void main()
{ int i,k,int x[10],temp;
clrscr();
printf("\n Enter the numbers:\n");
for(i=0;i<10;i++)
scanf("%d",&x[i]);
for(i=0;i<10;i++)
{ for(k=0;k<10;k++)
{ if(x[k]>x[k+1])
{ temp=x[k];
x[k]=x[k+1];
x[k+1]=temp;
}
}
}
printf("\nThe numbers in ascending order are:\n”);
for(i=0;i<10;i++)
printf(“%d\t",x[i]);
getch();
}
To find HCF
#include<stdio.h>
#include<conio.h>
void main()
{ int num1,num2,temp;
printf("Enter two numbers:\n");
scanf("%d%d",&num1,&num2);
while(num2!=0)
{ temp=num1%num2;
num1=num2;
num2=temp;
}
printf("\nHCF is %d",num1);
getch();
}
To find the factorial of given number
#include<stdio.h>
#include<conio.h>
void main()
{ int n,i;
longint fact;
clrscr();
fact=1;
printf("enter the number");
scanf("%d", &n);
for(i=1; i<=n; i++)
fact=fact*i;
printf("The factorial of %d is %d", n,fact);
getch();
}
To find the Fibonacci series of given number
#include<stdio.h>
#include<conio.h>
void main()
{ clrscr();
int n, i=0, j=1, k , a;
printf("Enter the number:");
scanf("%d", &n);
printf("\nfibonacci series is 0 1");
for(a=3; a<=n; a++)
{ k=i+j;
i=j;
j=k;
printf("\t %d", k);
}
getch();
}
To reverse the user input integer
#include<stdio.h>
#include<conio.h>
void main()
{ int num, rem, temp=0;
clrscr();
printf(“Enter any number:\n”);
scanf(“%d”,&num);
while(num!=0)
{ rem=num%10;
temp=(temp*10)+rem;
num=num/10;
}
printf(“The reversed number is %d”,temp);
getch();
}
To reverse the user input string
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{ char string1[20], temp;
int len,i,j;
clrscr();
printf(“Enter the string”);
scanf(“%s”,string1);
len=strlen(string1);
for(i=0,j=len-1;i<len,j>(len-1)/2;i++,j--)
{ temp=string1[i];
string1[i]=string1[j];
string1[j]=temp;
}
printf(“The reversed string is %s”,string1);
getch();
}
To calculate the sum of two 2X2 matrices
#include<stdio.h>
#include<conio.h>
void main()
{ int a[2][2],b[2][2],c[2][2],i,j;
for(i=0;i<2;i++)
for(j=0;j<2;j++)
{ printf("Enter the number"); scanf("%d",&a[i][j]);
}
for(i=0;i<2;i++)
{ for(j=0;j<2;j++)
printf("%d\t",a[i][j]); printf("\n");
}
for(i=0;i<2;i++)
for(j=0;j<2;j++)
{ printf("Enter the number"); scanf("%d",&b[i][j]);
}
for(i=0;i<2;i++)
{ for(j=0;j<2;j++)
printf("%d\t",b[i][j]); printf("\n");
}
for(i=0;i<2;i++)
{ for(j=0;j<2;j++)
{ c[i][j]=a[i][j]+b[i][j];
}
}
for(i=0;i<2;i++)
{ for(j=0;j<2;j++)
printf("%d\t",c[i][j]); printf("\n\n");
}
getch();
}
To multiply two 2X2 matrices
#include<stdio.h>
#include<conio.h>
void main()
{ int a[2][2],b[2][2],c[2][2],i,j,k;
for(i=0;i<2;i++)
for(j=0;j<2;j++)
{ printf("Enter the number of matrix a");
scanf("%d",&a[i][j]);
}
for(i=0;i<2;i++)
{ for(j=0;j<2;j++)
printf("%d\t",a[i][j]);
printf("\n"); }
printf("\n\n");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
{ printf("Enter the number of matrix b");
scanf("%d",&b[i][j]);
}
for(i=0;i<2;i++)
{ for(j=0;j<2;j++)
printf("%d\t",b[i][j]);
printf("\n"); }
for(i=0;i<2;i++)
{ for(j=0;j<2;j++)
{ c[i][j]=0;
for(k=0;k<2;k++)
{ c[i][j]=(c[i][j]+(a[i][k]*b[k][j]));
}
}
}
}
}
printf("\n\n");
printf("\nThe multiplication of matrix a and matrix b is:\n");
for(i=0;i<2;i++)
{ for(j=0;j<2;j++)
printf("%d\t",c[i][j]);
printf("\n");
}
getch();
}
To find the power of user input number
#include<stdio.h>
#include<conio.h>
int power(int,int);
void main()
{ int n,p;
clrscr();
printf("enter the base number");
scanf("%d",&n);
printf("Enter the power number");
scanf("%d",&p);
printf("\n%d^%d is %d\n",n,p,power(n,p));
getch();
}
int power(intn,int p)
{ int i;
int c=n;
for(i=1;i<p;i++)
n=n*c;
return n;
}
To display the user input sentences removing all the spaces
#include<stdio.h>
#include<conio.h>
void main()
{ char line[50],c;
int i=0;
printf(“Enter the sentence:\n”);
gets(line);
while((c=(line[i++]))!=‘.’)
{ if(c==‘ ’)
continue;
else
printf(“%c”,c);
}
printf(“.”);
getch();
}
To count the number of words in a sentence
#include<stdio.h>
#include<conio.h>
void main()
{ char line[80],c;
int i=0,j=1;
printf(“Enter the sentence:”);
gets(line);
while((c=(line[i++]))!=‘.’)
{ if(c==‘ ’)
j=j+1;
}
printf(“\n%d”,j);
getch();
}
Use of pointer passing by reference
#include<stdio.h>
#include<conio.h>
void swap(int *,int *);
void main()
{ int a=6,b=10;
swap(&a,&b);
printf("The values after swapping are a=%d,b=%d",a,b);
getch();
}
void swap(int*a,int*b)
{ int *temp;
*temp=*a;
*a=*b;
*b=*temp;
}
Use of pointer passing by value
#include<stdio.h>
#include<conio.h>
void swap(int,int);
void main()
{ int a=6,b=10;
swap(a,b);
getch();
}
void swap(inta,int b)
{ int temp;
temp=a;
a=b;
b=temp;
printf("The value after swapping are a=%d and b=%d",a,b);
}
To enter the details of a book and display it using structure
#include<stdio.h>
#include<conio.h>
void main()
{ struct book
{ charbookname[1];
char author[1];
char publisher[1];
intrate,page;
};
int i;
struct book b1[1];
for(i=0;i<1;i++)
{ printf("\nenter the bookname:");
scanf("%s",b1[i].bookname);
printf("\nenter the author name");
scanf("%s",b1[i].author);
printf("\nenter the publisher name");
scanf("%s",b1[i].publisher);
printf("\nenter the rate");
scanf("%d",&b1[i].rate);
printf("\nenter the page no");
scanf("%d",&b1[i].page);
}
printf("\nbookname\tauthorname\tpublishername\trate\tpageno.");
for(i=0;i<1;i++)
printf("\n%s\t\t%s\t\t%s\t\t%d\t%d\t",b1[i].bookname,b1[i].author,b1[i].publ
isher,b1[i].rate,b1[i].page);
getch();
}
To enter the detail of a person and store the record in a file
#include<stdio.h>
void main()
{ FILE *file;
file=fopen(“abc.txt”, “a+”);
char name[15], add[15];
//reading the file
while(!feof(file))
{ fscanf(file, “\n%s”,name);
printf(“\n%s”,name);
fscanf(file,”\t\t%s”,add);
printf(“\t\t%s”,add);
}
// ading new record
printf(“\n\n Enter name”\n”);
scanf(“%s”,name);
fprintf(file,”\n%s”,name);
printf(“\n Enter addres:\n”);
scanf(“%s”,add);
fprintf(file,”\t\t%s”,add);
fclose(file);
}
To draw a line
#include<stdio.h>
#include<graphics.h>
void main()
{ int gd=DETECT,gm; initgraph(&gd,&gm,”c:\\tc\\bgi”);
line(200,100,150,300);
setcolor(WHITE);
getch();
closegraph();
}
To draw a Circle
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{ int gd=DETECT,gm;
initgraph(&gd,&gm,”c:\\tc\\bgi”);
circle(200,200,100);
setcolor(WHITE);
getch();
closegraph();
}
To draw a rectangle
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{ int gd=DETECT,gm;
initgraph(&gd,&gm,”c:\\tc\\bgi”);
rectangle(200,100,150,300);
setcolor(WHITE);
getch();
closegraph();
}
To draw a triangle
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{ int gd=DETECT,gm;
initgraph(&gd,&gm,”c:\\tc\\bgi”);
line(200,100,200,300);
line(200,100,400,300);
line(200,300,400,300);
setcolor(WHITE);
getch();
closegraph();
}
MORE………………..