C Practical Q&A
C Practical Q&A
#include <stdio.h>
int main()
scanf("%d",&num);
while(num!=0)
k=num%10;
sum=sum+k;
k=num/10;
num=k;
return 0;
output: working
Ex2:
Write a C program, which takes two integer operands and one operator form the user
performs the operation and then prints the result.(Consider the operators +,-,*, /, % and
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a,b,res,ch;
printf("\t *********************");
printf("\n\tMENU\n");
printf("\t********************");
printf("\n\t(1)ADDITION");
printf("\n\t(2)SUBTRACTION");
printf("\n\t(3)MULTIPLICATION");
printf("\n\t(4)DIVISION");
printf("\n\t(5)REMAINDER");
printf("\n\t(0)EXIT");
printf("\n\t********************");
scanf("%d",&ch);
switch(ch)
case 1:
case 2:
case 3:
case 4:
case 5:
res=a%b; printf("\n Remainder:%d",res); break;
default:
return 0;
output: working
Ex 3:
Write a C program - using gets() and puts() do display the input character.
#include<stdio.h>
#include<string.h>
int main()
scanf ("%s",c);
puts(c);
return 0;
Ex 4:
#include<stdio.h>
int main()
int x, y;
scanf("%d",&x);
scanf("%d",&y);
if(x>y)
else
return 0;
output: working
Ex 5:
#include<stdio.h>
int main()
int n,i,f;
f=1;
for(i=1;i<=n;i++)
f=f*i;
printf("Factorial of %d=%d",n,f);
return 0;
output:working
#include<stdio.h>
int main()
long int n;
scanf("%ld",&n);
printf("Factorial of %ld=%ld",n,fact(n));
return 0;
if(n==0)
return 1;
else
return n*fact(n-1);
}
ouput:working
Ex 6
#include<stdio.h>
#include<string.h>
int main()
char str1[50],str2[50];
int i,j,n,m,l;
scanf ("%s",str1);
scanf("%d",&n);
scanf("%d",&m);
l=strlen(str1);
if(m+n-1<l)
for(i=n-1,j=0;i<m+n-1;i++,j++)
str2[j]=str1[i];
str2[j]='\0';
else
return 0;
}
output:working
Ex 7
a. Write a C program to find both the largest and smallest number in a list of integers using an array
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
scanf("%d",&i);
for(m=0;m<i;m++)
scanf("%d",&a[m]);
max=a[0];
min=a[0];
for(j=1;j<i;j++)
if(a[j]>max)
max=a[j];
if(a[j]<min)
min=a[j];
}
getch();
output: working
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
int i, j;
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&b[i][j]);
printf("\n====Matrix Addition====\n");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
printf("%5d", a[i][j]+b[i][j]);
printf("\n");
getch();
output:working
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
int i, j, k;
//clrscr();
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&b[i][j]);
printf("\n====Matrix Multiplication====\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]+=a[i][k]*b[k][j];
//printing result
for(i=0;i<2;i++)
for(j=0;j<2;j++)
printf("%d\t",c[i][j]);
getch();
}
output: working
Ex 8
Write a C program using structures for reading the employee details like employee name, date of joining
and salary and also to compute total salary outgo for a month
#include<stdio.h>
#include<string.h>
struct Employee
char name[20];
int day;
char month[10];
int year;
float salary;
};
int main()
int total_salary=0;
int n, i, t;
char month[10];
scanf("%d",&n);
for(i=1;i<n;i++)
printf("Input Values\n");
scanf("%s%d%s%d%f",e[i].name,&e[i].day,e[i].month,&e[i].year,&e[i].salary);
printf("%s%d%s%d%f\n",e[i].name,e[i].day,e[i].month,e[i].year,e[i].salary);
scanf("%s", month);
for(i=1;i<=n;i++)
t=strcmp(e[i].month, month);
if(t==0)
total_salary=total_salary+e[i].salary;
return 0;
#include<stdio.h>
#include<string.h>
struct personal
char name[20];
int day;
char month[10];
int year;
float salary;
};
int main()
{
char month[20];
int i, n;
float m, total_salary=0;
scanf("%d",&n);
printf("Input Values\n");
for(i=1;i<=n;i++)
scanf("%s %d %s %d %f",person[i].name,&person[i].day,person[i].month,&person[i].year,&m);
person[i].salary=m;
printf("\nOutput Values\n");
for(i=0;i<=n;i++)
printf("%s %d %s %d
%f\n",person[i].name,person[i].day,person[i].month,person[i].year,person[i].salary);
scanf("%s", month);
for(i=0;i<=n;i++)
if(strcmp(person[i].month, month)==0)
total_salary=total_salary+person[i].salary;
}
printf("Total salary =%f", total_salary);
return 0;
Ex 9: Write a C program to use structure within union, display the structure and length of union
elements.
#include <stdio.h>
#include <conio.h>
struct hai
char c;
long l;
char *p;
};
union bye
char c;
long l;
char *p;
};
int main()
myhai.c=1;
myhai.l=2L;
myhai.p="This is my myhai";
mybye.c=1;
mybye.l=2L;
mybye.p="This is mybye";
return 0;
output:working
#include <stdio.h>
#include <conio.h>
union bye
int c;
float l;
char *p;
mydata;
int main()
mydata.c=5;
mydata.l=12.35;
getch();
return 0;
output: working
Ex 10
#include <stdio.h>
int main()
a=&x;
b=&y;
temp=*b;
*b=*a;
*a=temp;
// getch();
return 0;
//Write a program to read data from the keyboard, write it to a file called INPUT, again read the same
data from the INPUT file, and copy to that another file and also //display it to screen. check if working on
LMS lab, not able to run on online compiler.
#include <stdio.h>
#include<file.h>
main()
FILE *f1,*f2;
char c;
printf("data input\n\n")
f1=fopen("INPUT","w");
while((c=getchar())!=EOF)
putc(c,f1);
fclose(f1);
printf("\ndata output\n\n");
f1=fopen("INPUT","r");
while((c=getc(f1))!=EOF)
printf("%c",c);
fclose(f1);
//f1=fopen(file1,"r");
f1=fopen("INPUT","r");
if(f1==NULL)
printf("no data");
exit(0);
}
f2=fopen("file2", "w");
if(f2==NULL)
printf("cannot open");
exit(0);
while((c=getc(f1))!=EOF)
putc(c, f2);
printf("completed");
fclose(f1);
fclose(f2);
getch();
return 0;