C Programs 1
C Programs 1
2
WAP TO COMPARE THE TWO STRINGS ARE
25 IDENTACAL OR NOT WITHOUT USING LIBRARY
FUNCTION.
WAP TO CONVERT THE STRING INTO UPPER
26 CASE WITHOUT USING LIBRARY FUNCTION.
WAP TO CONVERT THE STRING INTO LOWER
27 CASE WITHOUT USING LIBRARY FUNCTION.
WAP TO COPY ONE STRING INTO ANOTHER
28 STRING WITHOUT USING LIBRARY FUNCTION.
WAP TO SWAP TWO NUMBERS USING CALL BY
29 REFERENCE.
WAP TO DISPLAY STUDENT RECORDS WITH
30 ROLL NUM, NAME, COURSE, AGE, & ADDRESS
USING STRUCTURE.
WAP TO DISPLAY 5 EMPLYOEES RECORDS
31 WITH EMP_ID, EMP_NAME, SALARY,
COMPANY, & ADDRESS USING STRUCTURE.
WAP TO DISPLAY 10 BOOKS RECORDS WITH
32 BOOK TITLE, AUTHOR NAME, PRICE, NO OF
PAGES & EDITION USING STRUCTURE.
A FILE NAMED "DATA" CONTAINS A SERIES OF
33 INTERGER NUMBERS A PROGRAM TO READ
THESE NUMBERS AND THEN WRITE ALL ODD
NUMBERS TO A FILE "ODD" AND ALL EVEN
NUMBERS TO A FILE CALLED "EVEN".
WAP TO COPY ONE FILE INTO ANOTHER FILE.
34
35 WAP TO READ THE DETAILS OF A STUDENT (
ROLL, NAME, FEE, DOB, & COURSE) AND THEN
PRINT IT ON THE SCREEN AS WELL AS WRITE IT
INTO A FILE.
3
1. WAP to find sum of two Numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf(" Enter the First number = ");
scanf("%d",&a);
printf(" Enter the second number =");
scanf("%d",&b);
c = a + b;
printf("\n Sum of two number =%d",c);
getch();
}
Output:
4
2. WAP to calculate & Display area of circle.
#include<stdio.h>
#include<conio.h>
void main()
{
float r,area;
clrscr();
printf(" Enter the radius = ");
scanf("%f",&r);
area = 3.14*r*r;
printf("\n Area of circle =%f",area);
getch();
}
Output
5
3. WAP to conver degree centigrade to fahrenheit .
#include<stdio.h>
#include<conio.h>
void main()
{
float celsius,fahrenheit;
clrscr();
printf(" Enter the temp in celsius =");
scanf("%f", &celsius);
fahrenheit = (1.8 * celsius) + 32;
printf("\n the temp of celsius = %f",fahrenheit);
getch();
}
Output
6
4. WAP to calculate the area & perimeter of rectanlge .
#include<stdio.h>
#include<conio.h>
void main()
{
float l,b, area,perimeter;
clrscr();
printf("Enter the length =");
scanf("%f",&l);
printf("enter the breadth =");
scanf("%f",&b);
area = l * b;
perimeter = 2*(l+b);
printf("\n Area of rectangle =%f",area);
printf("\n Perimeter of rectangle =%f",perimeter );
getch();
}
Output
7
5. WAP to display any given natural number even or odd.
#include<stdio.h>
#include<conio.h>
void main()
int n;
clrscr();
scanf("%d",&n);
if(n%2==0)
else
getch();
Output
8
6. WAP to display any year is leap year or not .
#include<stdio.h>
#include<conio.h>
void main()
{
int num;
clrscr();
}
else
{
printf("\n the given year is not a leap year =%d",num);
}
getch();
}
Output
9
7. WAP to display the largest number between two number.
#include<stdio.h>
#include<conio.h>
void main()
int a,b;
clrscr();
scanf("%d",&a);
scanf("%d",&b);
if(a>b)
if(b>a)
else
getch();
10
Output
11
8.WAP to display the largest number among three number .
#include<stdio.h>
#include<conio.h>
int main()
int a,b,c;
clrscr();
scanf("%d%d%d",&a,&b,&c);
if(a>b)
if(a>c)
else
else
if(b>c)
else
getch();
Output
12
9. WAP to swap two numbers without using third number.
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y;
clrscr();
printf("Input value for x & y = ");
scanf("%d%d",&x,&y);
printf("Before swapping the value of x & y : %d %d",x,y);
x=x+y;
y=x-y;
x=x-y;
printf("\n After swapping the value of x & y: %d %d",x,y);
getch();
}
Output
13
10. WAP to display factorial of any given number.
#include<stdio.h>
#include<conio.h>
void main()
{
int num,i=1,f=1;
clrscr();
printf("\n Enter the any number =");
scanf("%d",&num);
while(i<=num)
{
f=f*i;
i=i+1;
}
printf("\n The value of the factorial =%d",f);
getch();
}
Output
14
11. WAP to display any given natural number is prime or not.
#include<stdio.h>
#include<conio.h>
void main()
int n,i,f=0;
clrscr();
scanf("%d",&n);
for(i=2;i<=n/2;i++)
if(n%i==0)
f=1;
break;
if(f==0)
else
getch();
15
Output
16
12. WAP to display the reverse of any given natural number.
#include<stdio.h>
#include<conio.h>
void main()
int n,r,rev=0;
clrscr();
scanf("%d",&n);
while(n>0)
r=n%10;
rev=rev*10+r;
n=n/10;
getch();
Output
17
13. WAP to display the sum of digits of any given natural number.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,sum=0;
clrscr();
printf("\n Enter the number =");
scanf("%d",&n);
while(n!=0)
{
sum =sum +n%10;
n=n/10;
}
printf("\n The sum of digits of number = %d",sum);
getch();
}
Output
18
14. WAP to display any given natural number is paildrome or not.
#include<stdio.h>
#include<conio.h>
void main()
int n,i,m,rev=0;
clrscr();
scanf("%d",&n);
m=n;
while(n>0)
i=n%10;
rev=rev*10+i;
n=n/10;
if(m==rev)
else
getch();
Output
19
15. WAP to display the largest and smallest number of an array..
#include<stdio.h>
#include<conio.h>
void main()
int i, A[3],s,l;
clrscr();
for(i=0;i<4;i++)
scanf("%d",&A[i]);
l=A[0];
s=A[0];
for(i=1;i<4;i++)
if(l<A[i])
l=A[i];
else if(s>A[i])
s=A[i];
getch();
20
Output
21
16. WAP to search the number into an array using linear search.
#include<stdio.h>
#include<conio.h>
void main()
int i,n,A[10],f=0;
clrscr();
for(i=0;i<10;i++)
scanf("%d",&A[i]);
scanf("%d",&n);
for(i=0;i<10;i++)
if(n==A[i])
f=1;
break;
if(f==1)
else
getch();
22
Output
23
17. WAP to search the number into an array using binear search.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,A[10],beg,end,mid;
clrscr();
for(i=0;i<6;i++)
{
printf("enter the number =");
scanf("%d",&A[i]);
}
}
else
{
end=mid-1;
}
mid=(beg+end)/2;
24
}
if(A[mid]==n)
{
}
getch();
}
Output
25
18. WAP to sort the number ofan array.
#include<stdio.h>
#include<conio.h>
void main()
int i,j,t,A[5];
clrscr();
for(i=0;i<5;i++)
scanf("%d",&A[i]);
for(i=0;i<5;i++)
for(j=j+1;j<5;j++)
if(A[i]>A[j])
t=A[i];
A[i]=A[j];
A[j]=t;
for(i=0;i<5;i++)
printf(" %d ",A[i]);
getch();
26
Output
27
19.WAP to display the second and third largest number of an array.
#include<stdio.h>
#include<conio.h>
void main()
int n=0,i=0,l1=0,l2=0,temp=0,array[10];
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&array[i]);
for(i=0;i<n;i++)
printf("%d\t",array[i]);
printf("\n");
l1=array[0];
l2=array[1];
if(l1<l2)
temp=l1;
l1=l2;
l2=temp;
for(i=2;i<n;i++)
if(array[i]>l1)
28
l2=l1;
l1=array[i];
l2=array[i];
getch();
Output
29
20. WAP to display the transpose of 3*3 matrix.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,A[3][3],B[3][3];
clrscr();
printf("enter 3*3 Matrix :");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d", &A[i][j]);
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
B[i][j] = A[i][j];
}
}
printf("The transport of matrix =\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",B[i][j]);
}
printf("\n");
30
}
getch();
Output
31
21. WAP to display the sum oftwo 3*3 matrix.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,A[3][3],B[3][3],C[3][3];
clrscr();
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Enter the matrix A=");
scanf("%d",&A[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Enter the matrix B=");
scanf("%d",&B[i][j]);
}
}
printf("The of matrix A =\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",A[i][j]);
32
}
printf("\n");
}
printf("%d ",B[i][j]);
}
printf("\n");
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
C[i][j]=A[i][j]+B[i][j];
}
}
printf("The Sum of matrix C =\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",C[i][j]);
}
printf("\n");
}
getch()
33
}
output
34
}
35
22. WAP to display the product of two 3*3 matrix.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,A[3][3],B[3][3],C[3][3];
clrscr();
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
{
for(j=0;j<3;j++)
{
printf("Enter the matrix B=");
scanf("%d",&B[i][j]);
}
}
printf("The of matrix A =\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
36
printf("%d ",A[i][j]);
}
printf("\n");
}
printf("The of matrix B =\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",B[i][j]);
}
printf("\n");
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
C[i][j]=0;
for(int k=0;k<3;k++)
{
C[i][j]=C[i][j]+A[i][j]*B[i][j];
}
}
}
printf("The product of matrix C =\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
37
printf("%d ",C[i][j]);
}
printf("\n");
}
getch();
}
Output
38
23.WAP to display the reverse of any given string without using
library function.
#include<stdio.h>
#include<conio.h>
void main()
char A[10];
int i=0,l=0;
clrscr();
gets(A);
while(A[i]!='\0')
l++;
i++;
l=l-1;
while(i>=0)
printf("%c",A[i]);
i--;
getch();
Output
39
24.WAP to concatinate the two string without using library function.
#include<stdio.h>
#include<conio.h>
void main()
char A[10],B[10];
int i=0,j=0;
clrscr();
gets(A);
gets(B);
while(A[i]!='\0')
i++;
while(B[j]!='\0')
A[i]=B[j];
i++;
j++;
A[i]='\0';
puts(A);
getch();
Output
40
25.WAP to compare the two string are identacal or not without using library function.
#include<stdio.h>
#include<conio.h>
void main()
char A[10],B[10];
int i=0,f=0,l,m;
clrscr();
gets(A);
gets(B);
l=strlen(A);
m=strlen(B);
if(l==m)
while(i<l)
if(A[i]==B[l])
i++;
else
break;
if(i==l)
f=1;
41
}
if((f==0)&&(l>m))
printf("the both string are not equal string is greater than string ");
if((f==0)&&(l<m))
printf("the both are not same & string is greater than string ");
else if(f==0)
getch();
Output
42
26.WAP to convert the string into upper case without library function.
#include<stdio.h>
#include<conio.h>
void main()
char A[10],B[10];
int i=0,j=0,l=0;
clrscr();
gets(A);
while(A[i]!='\0')
i++;
l++;
while(j<l)
B[j]=A[j]-32;
else
B[j]=A[j];
j++;
B[j]='\0';
puts(B);
getch();
43
Output
44
27.WAP to convert the string into lower case without library function.
#include<stdio.h>
#include<conio.h>
void main()
char A[10],B[10];
int i=0;
clrscr();
gets(A);
while(A[i]!='\0')
B[i]=A[i]+32;
else
B[i]=A[i];
i++;
B[i]='\0';
puts(B);
getch();
Output
45
28.WAP tocopy one string into another string without library function.
#include<stdio.h>
#include<conio.h>
void main()
{
char A[10],B[10];
int i=0;
clrscr();
printf("enter the string =");
gets(A);
while(A[i]!='\0')
{
B[i]=A[i];
i++;
}
B[i]='\0';
46
29. WAP to swap two numbers using call by reference.
#include<stdio.h>
#include<conio.h>
void swap(int *a , int *b);
void main()
{
int a,b;
clrscr();
printf("enter the value of a & b =");
scanf("%d%d",&a,&b);
swap(&a,&b);
printf("\n a=%d",a);
printf("\n b=%d",b);
getch();
}
*y=t;
}
Output
47
30. WAP to display student records with roll num,name,course,age,& address using structure..
#include<stdio.h>
#include<conio.h>
struct student
int roll_no;
char name[20];
char course[20];
int age;
char address[20];
};
void main()
struct student k;
clrscr();
scanf("%d",&k.roll_no);
scanf("%s",&k.name);
scanf("%s",&k.course);
scanf("%d",&k.age);
scanf("%s",&k.address);
getch();
48
Output
49
31.WAP to display 5 employees records with EMP_ID,EMP_Name,Salary,company,&
address using structure…
#include<stdio.h>
#include<conio.h>
struct employee
int emp_id;
char name[20];
int salary;
char company[20];
char address[20];
};
void main()
int i;
clrscr();
for(i=0;i<2;i++)
scanf("%d",&A[i].emp_id);
scanf("%s",&A[i].name);
scanf("%d",&A[i].salary);
scanf("%s",&A[i].company);
scanf("%s",&A[i].address);
50
for(i=0;i<2;i++)
printf("\n ID =%d",A[i].emp_id);
printf("\n Name=%s",A[i].name);
getch();
Output
51
32. WAP to display 10 books record with bootitle,auther name,price,no.of
pageses,eddition using structure.
#include<stdio.h>
#include<conio.h>
struct book
char booktitle[20];
char authname[20];
int price;
int no_ofpages;
char ed[20];
};
void main()
int i;
clrscr();
for(i=0;i<2;i++)
scanf("%s",&a[i].booktitle);
scanf("%s",&a[i].authname);
printf("enter price=");
scanf("%d",&a[i].price);
printf("enter page=");
scanf("%d",&a[i].no_ofpages);
printf("enter edition=");
scanf("%s",&a[i].ed);
52
for(i=0;i<2;i++)
getch();
Output
53
34.WAP a file Name DATA contains a series of integer numbers code a program to
read this number and then write all ODD numbers to a file to record and all even
number into and Even number into a file to be called number EVEN.
#include<stdio.h>
#include<conio.h>
void main()
FILE *fp1,*fp2,*fp3;
int i,n,m;
clrscr();
fp1=fopen("DATA","w");
if(fp1==NULL)
exit(0);
for(i=1;i<=3;i++)
scanf("%d",&n);
putc(n,fp1);
fclose(fp1);
fp1=fopen("DATA","r");
fp2=fopen("ODD","w");
fp3=fopen("EVEN","w");
while(m=getc(fp1)!=EOF)
if(m<0)
break;
54
else if(m%2==0)
putc(m,fp3);
else
putc(m,fp2);
fclose(fp1);
fclose(fp2);
fclose(fp3);
fp2=fopen("ODD","r");
fp3=fopen("EVEN","r");
while(m=getw(fp2)!=EOF)
printf("%d",m);
while((m=getw(fp3)!=EOF))
printf("%d",m);
fclose(fp2);
fclose(fp3);
getch();
55
Output
56
/* 34.WAP to copy one file to another file.*/
#include<stdio.h>
#include<conio.h>
void main()
FILE *fp1,*fp2;
char ch,file1[20],file2[20];
clrscr();
scanf("%s",&file1);
scanf("%s",&file2);
fp1=fopen("file1","r");
fp2=fopen("file2","w");
if(fp1==NULL)
exit(0);
if(fp2==NULL)
exit(0);
while(ch=fgetc(fp1)!=EOF);
putc(ch,fp2);
fclose(fp1);
fclose(fp2);
getch();
57
}
Output
58
35. WAP to read the details of a student (roll,name,fee,dob,course)and then print it
on the screen as well as white into a file.
#include<stdio.h>
#include<conio.h>
struct student
int roll;
char name[20];
char fee[20];
char dob[30];
char course[10];
};
void main()
FILE *fp;
char filename[20];
int i;
clrscr();
scanf("%s",&filename);
fp=fopen("filename","w");
if(fp==NULL)
exit(0);
for(i=0;i<2;i++)
scanf("%d",&k[i].roll);
59
scanf("%s",&k[i].name);
scanf("%s",&k[i].fee);
scanf("%s",k[i].dob);
scanf("%s",&k[i].course);
for(i=0;i<2;i++)
for(i=0;i<2;i++)
fclose(fp);
getch();
60
Output
61