C Programming Practical's (Basic Level Training Programs) Solved
C Programming Practical's (Basic Level Training Programs) Solved
1. WAP that accepts the marks of 5 subjects and finds the sum and percentage marks obtained by the
student.
2. WAP that calculates the Simple Interest and Compound Interest. The Principal, Amount, Rate of
Interest and Time are entered through the keyboard.
4. WAP that accepts the temperature in Centigrade and converts into Fahrenheit using the formula
C/5=(F-32)/9.
6. WAP that checks whether the two numbers entered by the user are equal or not.
10. WAP that accepts marks of five subjects and finds percentage and prints grades according to the
following criteria: Between 90-100%--------------Print „A‟ 80-90%----------------------------Print „B‟ 60-
80%---------------------------Print „C‟ Below 60%----------------------Print „D‟
11. WAP that takes two operands and one operator from the user and perform the operation and prints
the result by using Switch statement.
14. WAP to print sum of even and odd numbers from 1 to N numbers.
20. WAP to convert binary number into decimal number and vice versa.
21. WAP that simply takes elements of the array from the user and finds the sum of these elements. 22.
WAP that inputs two arrays and saves sum of corresponding elements of these arrays in a third array
and prints them.
23. WAP to find the minimum and maximum element of the array.
25. WAP to sort the elements of the array in ascending order using Bubble Sort technique.
27. WAP that finds the sum of diagonal elements of a mxn matrix.
28. WAP to implement strlen (), strcat (),strcpy () using the concept of Functions.
29. Define a structure data type TRAIN_INFO. The type contain Train No.: integer type Train name:
string Departure Time: aggregate type TIME Arrival Time: aggregate type TIME Start station: string End
station: string The structure type Time contains two integer members: hour and minute. Maintain a
train timetable and implement the following operations: (i) List all the trains (sorted according to train
number) that depart from a particular section. (ii) List all the trains that depart from a particular station
at a particular time. (iii) List all he trains that depart from a particular station within the next one hour of
a given time. (iv) List all the trains between a pair of start station and end station.
31. WAP to compare the contents of two files and determine whether they are same or not.
32. WAP to check whether a given word exists in a file or not. If yes then find the number of times it
occurs.
Program-1
#include<conio.h>
#include<stdio.h>
void main()
{
float m,p,c,h,e,s,per;
clrscr();
printf("enter marks of 5 subjects");
scanf("%f%f%f%f%f",&m,&p,&c,&h,&e);
s=m+p+c+h+e;
per=s/5;
printf("Sum=%f out of 500 and percentage=%f",s,per);
getch();
}
Program-2
#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
float p,r,t,si,ci;
clrscr();
printf("enter money,rate of interest and time");
scanf("%f%f%f",&p,&r,&t);
si=(p*r*t)/100;
ci=p*pow((1+r/100),t)-p;
printf("simple interest=%f",si);
printf("compound interest=%f",ci);
getch();
}
Program-3
#include<conio.h>
#include<stdio.h>
void main()
{
float a,c,r;
clrscr();
printf("enter radius");
scanf("%f",&r);
a=3.14*r*r;
c=2*3.14*r;
printf(" area=%f",a);
printf(" circumference=%f",c);
getch();
}
Program-4
#include<conio.h>
#include<stdio.h>
void main()
{
float c,f;
clrscr();
printf("emter temperature in centigrade");
scanf("%f",&c);
f=(9*c)/5+32;
printf("temperature in fahrenheit=%f",f);
getch();
}
Program-5
#include<conio.h>
#include<stdio.h>
void main()
{
float a,b,t;
clrscr();
printf("enter two numbers");
scanf("%f%f",&a,&b);
t=a;
a=b;
b=t;
printf("After swapping a=%f and b=%f",a,b);
getch();
}
Program-6
#include<conio.h>
#include<stdio.h>
void main()
{
float a,b;
clrscr();
printf("enter two numbers");
scanf("%f%f",&a,&b);
if(a==b)
printf("equal");
else
printf("not equal");
getch();
}
Program-7
#include<conio.h>
#include<stdio.h>
void 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 largest",a);
else if(b>=c)
printf("%d is largest",b);
else
printf("%d is largest",c);
getch();
}
Program-8
#include<conio.h>
#include<stdio.h>
void main()
{
int n;
clrscr();
printf("enter number");
scanf("%d",&n);
if(n%2==0)
printf("%d is even",n);
else
printf("%d is odd",n);
getch();
}
Program-9
#include<conio.h>
#include<stdio.h>
void main()
{
int y;
clrscr();
printf("enter year");
scanf("%d",&y);
if(y%100==0)
{
if(y%400==0)
printf("%d is leap year",y);
else
printf("%d is not leap year",y);
}
else
{
if(y%4==0)
printf("%d is leap year",y);
else
printf("%d is not leap year",y);
}
getch();
}
Program-10
#include<conio.h>
#include<stdio.h>
void main()
{
float m,p,c,h,e,s,per;
clrscr();
printf("enter marks of 5 subjects");
scanf("%f%f%f%f%f",&m,&p,&c,&h,&e);
s=m+p+c+h+e;
per=s/5;
if(per>90)
printf("A");
else if(per>80)
printf("B");
else if(per>60)
printf("C");
else
printf("D");
getch();
}
Program-11
#include<conio.h>
#include<stdio.h>
void main()
{
int a,b,r;
char ch;
clrscr();
printf("enter two numbers\n");
scanf("%d%d",&a,&b);
printf("enter + for addition\n");
printf("enter - for subtraction\n");
printf("enter * for multiplication\n");
Program-12
#include<conio.h>
#include<stdio.h>
void main()
{
int n,s=0,i;
clrscr();
printf("enter number");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
s=s+i;
}
printf("sum=%d",s);
getch();
}
Program-13
#include<conio.h>
#include<stdio.h>
void main()
{
int n,f=1,i;
clrscr();
printf("enter number");
scanf("%d",&n);
for(i=n;i>=1;i--)
{
f=f*i;
}
printf("factorial=%d",f);
getch();
}
Program-14
#include<conio.h>
#include<stdio.h>
void main()
{
int n,se=0,so=0,i;
clrscr();
printf("enter number");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i%2==0)
se=se+i;
else
so=so+i;
}
printf("sum of even=%d",se);
printf("sum of odd=%d",so);
getch();
}
Program-15
#include<conio.h>
#include<stdio.h>
void main()
{
int n,a=0,b=1,i,c;
clrscr();
printf("enter how many terms");
scanf("%d",&n);
printf("%d\t%d\t",a,b);
for(i=1;i<=n-2;i++)
{
c=a+b;
printf("%d\t",c);
a=b;
b=c;
}
getch();
}
Program-16
#include<conio.h>
#include<stdio.h>
void main()
{
int n,i;
clrscr();
printf("enter number");
scanf("%d",&n);
for(i=2;i<n;i++)
{
if(n%i==0)
break;
}
if(i==n)
printf("%d is prime",n);
else
printf("%d is notprime",n);
getch();
}
Program-17
#include<conio.h>
#include<stdio.h>
void main()
{
int n,s=0;
clrscr();
printf("enter number");
scanf("%d",&n);
while(n>0)
{
s=s+(n%10);
n=n/10;
}
printf("sum=%d",s);
getch();
}
Program-18
#include<conio.h>
#include<stdio.h>
void main()
{
int n,r=0;
clrscr();
printf("enter number");
scanf("%d",&n);
while(n>0)
{
r=(r*10)+(n%10);
n=n/10;
}
printf("reverse=%d",r);
getch();
}
Program-19
#include<conio.h>
#include<stdio.h>
void main()
{
int n,s,m;
clrscr();
for(n=1;n<500;n++)
{
m=n;
s=0;
while(m>0)
{
s=s+(m%10)*(m%10)*(m%10);
m=m/10;
}
if(s==n)
printf("%d\t",n);
}
getch();
}
Program-20(i)
#include<conio.h>
#include<stdio.h>
void main()
{
int b,d=0,x=1;
clrscr();
printf("enter binary number");
scanf("%d",&b);
while(b>0)
{
d=d+(b%10)*x;
x=x*2;
b=b/10;
}
printf("decimal=%d",d);
getch();
}
Program-20(ii)
#include<conio.h>
#include<stdio.h>
void main()
{
int n,b=0,x=1;
clrscr();
printf("enter number");
scanf("%d",&n);
while(n>0)
{
b=b+(n%2)*x;
x=x*10;
n=n/2;
}
printf("binary=%d",b);
getch();
}
Program-21
#include<conio.h>
#include<stdio.h>
void main()
{
int n,s=0,a[50],i;
clrscr();
printf("enter size of array");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter element in array");
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
s=s+a[i];
}
printf("sum=%d",s);
getch();
}
Program-22
#include<conio.h>
#include<stdio.h>
void main()
{
int n,s=0,a[50],b[50],c[50],i;
clrscr();
printf("enter size of array");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter element in first array");
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
printf("enter element in second array");
scanf("%d",&b[i]);
}
for(i=0;i<n;i++)
{
c[i]=b[i]+a[i];
}
printf("A\tB\tC\n");
for(i=0;i<n;i++)
{
c[i]=b[i]+a[i];
printf("%d\t%d\t%d\n",a[i],b[i],c[i]);
}
getch();
}
Program-23
#include<conio.h>
#include<stdio.h>
void main()
{
int n,l,a[50],i,s;
clrscr();
printf("enter size of array");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter element in array");
scanf("%d",&a[i]);
}
l=a[0];
s=a[0];
for(i=0;i<n;i++)
{
if(l<a[i])
l=a[i];
if(s>a[i])
s=a[i];
}
printf("largest=%d smallest=%d",l,s);
getch();
}
Program-24
#include<conio.h>
#include<stdio.h>
void main()
{
int n,s,a[50],i,p,c=0;
clrscr();
printf("enter size of array");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter element in array");
scanf("%d",&a[i]);
}
printf("enter value to be search");
scanf("%d",&s);
for(i=0;i<n;i++)
{
if(s==a[i])
{
printf("%d is found at %d position\n",s,i);
c++;
}
}
if(c==0)
printf("%d is not found",s);
else
printf("%d is found %d times",s,c);
getch();
}
Program-25
#include<conio.h>
#include<stdio.h>
void main()
{
int a[50],n,i,j,temp;
clrscr();
printf("Enter the size of array: ");
scanf("%d",&n);
printf("Enter the array elements: ");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
for(i=0;i<n-1;++i)
for(j=0;j<(n-1-i);++j)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
Program-26(ii)
#include<conio.h>
#include<stdio.h>
void main()
{
int n, a[100][100], b[100][100], c[100][100], i, j,k;
clrscr();
printf("Enter order of square matrix ");
scanf("%d", &n);
for(i=0; i<n; ++i)
{
for(j=0; j<n; ++j)
{
printf("Enter element in first ");
scanf("%d",&a[i][j]);
}
}
for(i=0; i<n; ++i)
{
for(j=0; j<n; ++j)
{
printf("Enter element in second");
scanf("%d", &b[i][j]);
}
}
for(i=0;i<n;++i)
{
for(j=0;j<n;++j)
{
c[i][j]=0;
for(k=0;k<n;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
printf("\nMultiply of two matrix is: \n\n");
for(i=0;i<n;++i)
{
for(j=0;j<n;++j)
{
printf("%d ",c[i][j]);
}
printf("\n");
}
getch();
}
Program-27
#include<conio.h>
#include<stdio.h>
void main()
{
int n,a[100][100],i,j,s=0;
clrscr();
printf("Enter order of square matrix ");
scanf("%d", &n);
for(i=0; i<n; ++i)
{
for(j=0; j<n; ++j)
{
printf("Enter element in matrix ");
scanf("%d",&a[i][j]);
}
}
for(i=0;i<n;++i)
{
for(j=0;j<n;++j)
{ if((i==j)||(i+j==n-1))
s=s+a[i][j];
}
}
printf("%d",s);
getch();
}
Program-28
#include<conio.h>
#include<stdio.h>
int strlen1(char *);
void strcat1(char *,char *);
void strcpy1(char *,char *);
void main()
{
char str[50],str1[50],str2[120];
int l;
clrscr();
puts("enter first string");
gets(str);
l=strlen1(str);
printf("length of string=%d",l);
printf("\n string two after copy of contents one\n");
strcpy1(str1,str);
puts(str1);
puts("enter third string");
gets(str2);
strcat1(str,str2);
puts(str);
getch();
}
int strlen1(char *p)
{ int c=0;
while(*p!='\0')
{c++;
p++;
}
return(c);
}
void strcpy1(char *p,char *q)
{
while(*q!='\0')
{
*p=*q;
p++;
q++;
}
*p='\0';
}
void strcat1(char *p,char *q)
{
while(*p!='\0')
p++;
while(*q!='\0')
{
*p=*q;
p++;
q++;
}
*p='\0';
}
Program-30
#include<conio.h>
#include<stdio.h>
void main()
{
void swap(int *,int *);
int a,b;
clrscr();
printf("enter two numbers");
scanf("%d%d",&a,&b);
printf("before swap a=%d and b=%d\n",a,b);
swap(&a,&b);
printf("after swap a=%d and b=%d",a,b);
getch();
}
void swap(int *p,int *q)
{
int t;
t=*p;
*p=*q;
*q=t;
}
Program-31
#include<conio.h>
#include<stdio.h>
void main()
{
FILE *p,*q;
int c=0;
char a,b;
clrscr();
p=fopen("a.txt","r");
q=fopen("b.txt","r");
if((p==NULL)||(q==NULL))
{
printf("file cant open");
exit();
}
do
{
a=fgetc(p);
b=fgetc(q);
if(a!=b)
{
c=1;
break;
}
}while((a!=EOF)||(b!=EOF));
if(c==0)
printf("same");
else
printf("not same");
fclose(p);
fclose(q);
getch();
}