Computer
Computer
#include<stdio.h>
int main()
int a,b,c;
scanf("%d%d",&a,&b);
c=sum(a,b);
return 0;
int p;
p=a+b;
return (p);
}
● Write C program to know a number is even or odd using function named evenodd().
#include<stdio.h>
void even_odd(int n);
main()
{
int n;
printf("Enter a number :");
scanf("%d",&n);
even_odd(n);
}
void even_odd(int n)
{
if(n%2==0)
printf("Even");
else
printf("Odd");
}
● Write C program to print the greatest value among three numbers using a function int great().
We have to use return statement.
#include<stdio.h>
int great(int a, int, int c);
int main()
{
int a,b,c,d;
printf("Enter three numbers : ");
scanf("%d%d%d",&a,&b,&c);
d=great(a,b,c);
printf("greatest number is %d",d);
return 0;
}
}
● Write C program to know a number is prime or composite using function.
#include<stdio.h>
void test(int n);
main()
{
int n;
printf("Enter a number :");
scanf("%d",&n);
test(n);
}
void test(int n)
{
int i,c=0 ;
for(i=1;i<=n;i++)
{
if(n%i==0)
c++;
}
if(c==2)
printf("Prime");
else
printf("not prime or composite");
}
● Write C program to find sum of series 1,2,3,…..200 using function. Assume yourself function
name. It returns an integer value.
#include<stdio.h>
int sum();
int main()
return 0;
int sum()
int s=0,i;
for(i=1;i<=200;i++)
s=s+i;
return s;
}
● Suppose a function void matrix_sum(int a[][],int b[][]).Here, we have passed array as
parameter. Use this function to find sum of matrices.
#include<stdio.h>
void matrix_sum(int a[][3],int b[][3]);
main()
{
int a[3][3],b[3][3],i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Enter b[%d][%d] : ",i,j);
scanf("%d",&a[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Enter b[%d][%d] : ",i,j);
scanf("%d",&b[i][j]);
}
}
matrix_sum(a,b);
}
void matrix_sum(int a[][3],int b[][3])
{
int c[3][3],i,j;
printf("\n sum of two 3x3 matrices:
● Write C program to sort ‘n’ number of strings using function. Pass strings as parameter.
#include<stdio.h>
#include<string.h>
void sort(char str[][20],int n);
main()
{
char str[50][20];
int i,n;
printf(" How many strings : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf(" Enter str[%d] : ",i+1);
scanf("%s",str[i]);
}
sort(str,n);
}
void sort( char str[][20],int n)
{
int i,j;
char temp[20];
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(str[i],str[j])>0)
{
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}
}
printf(" Sorted strings are : \n");
for(i=0;i<n;i++)
printf(" %s\n ",str[i]);
}
● Write C program to find factorial value of a number using recursive function.
#include<stdio.h>
int fact(int);
main()
{
int n,ans;
printf( "Enter any number : ");
scanf("%d",&n);
ans=fact(n);
printf(" Factorial of %d is %d ",n,ans);
}
int fact(int n)
{
if(n<=1)
return (1);
else
return(n*fact(n-1));
}
● Write C program to input id, name and grade for 10 students. Then print them. Use array of
structure concept.
#include<stdio.h>
struct student
{
int id;
char name[30];
int grade;
} s[10];
main()
{
int i;
for(i=0;i<10;i++)
{
printf("Enter student id,name and grade : ");
scanf("%d%s%d",&s[i].id,s[i].name,&s[i].grade);
}
printf("-------Output-------\n");
printf("ID\t Name\t Grade\n");
for(i=0;i<10;i++)
{
printf("%d\t %s\t %d\n",s[i].id,s[i].name,s[i].grade);
}
}
● Write C program to input 20 employees name, post and salary. Then search a record of an
employee on the basis of name.
#include<stdio.h>
#include<string.h>
struct employee
{
char name[30];
char post[30];
int salary;
} e[20];
main()
{
int i,flag=0;
char search_name[30];
for(i=0;i<20;i++)
{
printf("Enter employee name,post and salary : ");
scanf("%s%s%d",e[i].name,e[i].post,&e[i].salary);
}
printf("Enter name to search: ");
scanf("%s",search_name);
for(i=0;i<20;i++)
{
if(strcmp(e[i].name,search_name)==0)
printf("%s\t %s\t %d\n",e[i].name,e[i].post,e[i].salary);
flag=1;
}
if(flag==0)
printf("Record not found");
}
● We have two structures as given below.
Struct employee
{
Char ename[100];
Char eaddress[100];
};
Struct salarydetails
{
Char pos[100];
float salary;
struct employee detail;
}payinfo;
1. Write C program to input employees’ details with salary then print them. Use nested struct
concept.
#include<stdio.h>
struct employee
{
char ename[100];
char eaddress[100];
};
struct salarydetails
{
char post[100];
float salary;
struct employee detail;
}payinfo;
main()
{
printf("Enter employee name,address,post and salary : ");
scanf("%s%s%s%f",payinfo.detail.ename,payinfo.detail.eaddress,payinfo.post,&payinfo.salary);
printf("Employee Name : %s\n",payinfo.detail.ename);
printf("Address : %s\n",payinfo.detail.eaddress);
printf("Post: %s\n",payinfo.post);
printf("Salary: %f",payinfo.salary );
}
● Write C program to input id, name and address of 20 students using struct. Then print them in
sorted format on the basis of name.
#include<stdio.h>
#include<string.h>
struct student
{
int id;
char name[30];
char address[30];
}s[20];
int main()
{
struct student temp;
int i,j;
for(i=0;i<20;i++)
{
printf("Enter id, name and address: ");
scanf("%d%s%s",&s[i].id,s[i].name,s[i].address);
}
for(i=0;i<19;i++)
{
for(j=i+1;j<20;j++)
{
if(strcmp(s[i].name,s[j].name)>0)
{
temp=s[i];
s[i]=s[j];
s[j]=temp;
}
}
}
printf("Records in Sorted order by Name \n");
for(i=0;i<20;i++)
{
printf("%d\t %s\t %s\n ",s[i].id,s[i].name,s[i].address);
}
return 0;
}
● Write C program to input any 10 teacher’s id, name and subject using ‘typedef’ and structure.
Then print them on screen.
#include<stdio.h>
typedef struct teacher
{
int id;
char name[50];
char subject[50];
}t;
main()
{
t s[10];
int i;
for(i=0;i<10;i++)
{
printf("Enter [%d] teacher id, name and subject : ",i+1);
scanf("%d%s%s",&s[i].id,s[i].name,s[i].subject);
}
printf("ID\t Name\t Subject\n");
for(i=0;i<10;i++)
{
printf("%d\t%s\t%s\n",s[i].id,s[i].name,s[i].subject);
}
}
Write C program using function and structure to calculate sum of two distances measured in terms of
kilometers and meters.
For example,
If we input following data
Kilometer meter
23 445
45 756
69 201
#include<stdio.h>
struct distance
{
int km;
int m;
}d1,d2,total;
void sum(struct distance dis1,struct distance dis2);
main()
{
printf("Enter first distance : ");
Go to top
scanf("%d%d",&d1.km,&d1.m);
printf("Enter second distance : ");
scanf("%d%d",&d2.km,&d2.m);
sum(d1,d2);
return 0;
}
void sum(struct distance dis1,struct distance dis2)
{
total.m=(dis1.m+dis2.m)%1000;
total.km=dis1.km+dis2.km+(dis1.m+dis2.m)/1000;
printf("Total km=%d m=%d",total.km,total.m);
}
● Write C program to input student id, name and grade and print them. Use union concept.
#include<stdio.h>
union student
{
int id;
char name[30];
int grade;
} s;
main()
{
printf("Enter student id: ");
scanf("%d",&s.id);
printf("Student ID: %d\n ",s.id);
printf("Enter name : ");
scanf("%s",s.name);
printf("Name : %s\n",s.name);
printf("Enter grade : ");
scanf("%d",&s.grade);
printf("Grade : %d\n",s.grade);
}
● Write C program to perform arithmetic calculations (sum, difference, multiplication and division)
of two numbers using pointers.
#include<stdio.h>
main()
int num1,num2,sum,diff,mul,div,*x,*y;
scanf("%d%d",&num1,&num2);
x=&num1;
y=&num2;
sum=*x + *y;
diff=*x - *y;
mul=*x * *y;
div=(*x) / (*y);
printf("Sum=%d\n",sum);
printf("Difference=%d\n",diff);
printf("Multiplication=%d\n",mul);
printf("Division=%d\n",div);
}
● Write C program to know a number is even or odd using pointer.
#include<stdio.h>
main()
int num,*n;
scanf("%d",&num);
n=#
if(*n % 2==0)
printf("even");
else
printf("odd");
}
Write C program to find sum and average of ‘n’ natural numbers using pointer.
#include<stdio.h>
main()
{
int *p;
int n,i,sum=0,avg=0;
p=&n;
printf("Enter value of n: ");
scanf("%d",&n);
p=&n;
for(i=1;i<=*p;i++)
{
sum=sum+i;
}
avg=sum/(*p);
printf("Sum=%d\n",sum);
printf("Average=%d\n",avg);
}
● Write C program by using array to input 10 elements and print them. Use array as pointer.
#include<stdio.h>
main()
int i,arr[10];
for(i=0;i<10;i++)
scanf("%d",(arr+i));
for(i=0;i<10;i++)
printf("%d\n",*(arr+i));
}
● Write C program to input 10 elements and print maximum and minimum value. Use array as
pointer
#include<stdio.h>
main()
int i,arr[10],max,min;
for(i=0;i<10;i++)
scanf("%d",(arr+i));
max=*(arr+0);
min=*(arr+0);
for(i=0;i<10;i++)
if(*(arr+i)>max)
max=*(arr+i);
if(*(arr+i)<min)
min=*(arr+i);
}
● Write C program to swap two values using call by reference and call by value.
#include<stdio.h>
main()
{
int *p;
int num[10],i,j,temp;
for(i=0;i<10;i++)
{
printf("Enter num[%d] : ",i+1);
scanf("%d",(num+i));
}
for(i=0;i<9;i++)
{
for(j=i+1;j<10;j++)
{
if(*(num+i)>*(num+j))
{
temp=*(num+i);
*(num+i)=*(num+j);
*(num+j)=temp;
}
}
}
printf("Sorted in Ascending Order \n");
for(i=0;i<10;i++)
{
printf("%d\n",*(num+i));
}
}
● Write C program to print multiplication table of a number using pointer.
#include<stdio.h>
main()
{
int num,i;
int *p ;
printf(" Enter any number : ") ;
scanf("%d",&num);
p = #
for(i=1;i<=10;i++)
{
printf(" %d\n ",(*p * i)) ;
}
}
● Write C program to input a sentence and store data in a data file “file.txt”. And print them on
screen. Use getc() and putc() functions.
#include<stdio.h>
main()
{
FILE *fp;
char ch;
printf("\n Input data (to exit press ctrl+z)=");
fp=fopen("f.txt","w");
while((ch=getchar())!=EOF){
putc(ch,fp);
}
fclose(fp);
printf("\n Output data\n ");
fp=fopen("f.txt","r");
while((ch=getc(fp))!=EOF){
putchar(ch);
}
fclose(fp);
}
● Write C program to store some ‘n’ natural numbers in a data file and print them on screen. Use
getw() function. Also print their average.
#include<stdio.h>
main()
{
FILE *fp;
int i,n,sum=0,avg=0;
fp=fopen("file.txt","w");
printf("Enter the value of n: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
putw(i,fp);
}
fclose(fp);
printf("Press enter to view numbers :");
printf("\n Output data\n ");
fp=fopen("file.txt","r");
while((i=getw(fp))!=EOF){
sum=sum+i;
printf(" %d\n ",i);
}
avg=sum/n;
fclose(fp);
printf("\n Average = %d ",avg);
}
● Write C program to store student’s name and address in a data file “student.txt”. Use fprintf()
function. Then read contents of data file and print them on screen. Use fscanf() function.
#include<stdio.h>
main()
{
char name[30],add[30];
FILE *fp;
fp=fopen("student.txt","w");
printf("Enter name and address : ");
scanf("%s%s",name,add);
fprintf(fp,"%s\t %s",name,add);
fclose(fp);
fp=fopen("student.txt","r");
fscanf(fp,"%s\t %s",name,add);
printf("\n -----Ouput data -----\n ");
printf("%s \t %s",name,add);
fclose(fp);
}
● Write C program to store book’s name, edition and price in a data file “book.txt” using yes/no
options. It means the computer stores data until you say ‘n’. Then print them on screen.
#include<stdio.h>
main()
{
char b_name[30];
int b_edition;
float b_price;
char ch='y';
FILE *fp;
fp=fopen("book.txt","w");
while(ch!='n')
{
printf("Enter book name : " );
scanf("%s",b_name);
printf("Enter book edition : " );
scanf("%d",&b_edition);
printf("Enter book price : " );
scanf("%f",&b_price);
fprintf(fp,"%s\t %d\t %f\n",b_name,b_edition,b_price);
printf("Add more records (y/n) : ");
scanf(" %c",&ch);
}
fclose(fp);
fp=fopen("book.txt","r");
while(fscanf(fp,"%s%d%f",b_name,&b_edition,&b_price)!=EOF)
{
printf(" %s\t %d\t %f\n ",b_name,b_edition,b_price);
}
fclose(fp);
}
● Write C program to store employee’s name, position and salary in a data file “employee.txt”
using fwrite(). The computer stores data until you say ‘n’. Then read the contents of that file
and print on screen using fread().
#include<stdio.h>
struct employee{
char e_name[30];
char post[10];
float salary;
};
main(){
struct employee emp;
char ch;
FILE *fp;
fp=fopen("employee.txt","w");
while(ch!='n'){
printf(" Enter employee name:\n ");
scanf("%s",emp.e_name);
printf(" Enter employee post:\n ");
scanf("%s",emp.post);
printf(" Enter employee salary:\n ");
scanf("%f",&emp.salary);
fwrite(&emp,sizeof(emp),1,fp);
printf("\n Add more record (y/n): ");
scanf(" %c",&ch);
}
fclose(fp);
fp=fopen("employee.txt","r");
printf("\n Data from the file: \n");
printf("\n Name\t Post \t Salary");
while(fread(&emp,sizeof(emp),1,fp))
{
printf("\n %s\t %s\t %f",emp.e_name,emp.post,emp.salary);
}
printf("\n");
fclose(fp);
}
● Write C program to show the concept of rename() and remove() functions.
#include<stdio.h>
main()
{
FILE *fp;
char name[30];
fp=fopen("oldfile.txt","w");
printf("\n Enter your name : ");
scanf("%s",name);
fprintf(fp,"%s",name);
fclose(fp);
rename("oldfile.txt","newfile.txt");
remove("newfile.txt");
}
● Write C program to show concept of ftell(), fseek() and rewind() functions.
#include<stdio.h>
main ()
{
char name [30];
int age, length;
FILE *fp;
fp = fopen ("trinity.txt","w");
printf("Enter your name: \n");
scanf("%s",name);
printf("Enter your age: \n");
scanf("%d",&age);
fprintf (fp, "%s %d", name,age);
length = ftell(fp);
rewind (fp);
fscanf (fp, "%s", name);
fscanf (fp, "%d", &age);
fclose (fp);
printf ("Name= %s \n Age= %d \n",name,age);
printf ("Total number of characters in file is %d \n", length);
// Program of fseek ()
#include<stdio.h>
main ()
{
FILE *fp;
fp = fopen("test.txt", "r");