C Programming Language Solution
1. For any integer input through the
keyboard, write a C program to find out whether it is odd or even. [HSEB
2062,2066,2068]
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
printf("\nEnter any number: ");
scanf("%d",&n);
if(n%2==0)
printf("%d is Even",n);
else
printf("%d is Odd",n);
getch();
}
2. Write a C program to input cost price
(CP) and selling price (SP) and determines whether there is gain or loss. [HSEB
2066]
#include<stdio.h>
#include<conio.h>
void main()
{
float cp,sp;
clrscr();
printf("\nEnter Cost Price and Selling Price:
");
scanf("%f%f",&cp,&sp);
if(sp>cp)
printf("Rs. %.2f is
Profit",sp-cp);
else
printf("Rs. %.2f is Loss",cp-sp);
getch();
}
3. Write a C program that reads three
numbers and displays the largest among them. [HSEB 2065]
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("\nEnter any three numbers: ");
scanf("%d%d%d",&a,&b,&c);
if(a>b && a>c)
printf("%d is Greater",a);
else if(b>a && b>c)
printf("%d is Greater",b);
else
printf("%d is Greater",c);
getch();
}
4. Write a C program that checks whether
the numbered entered by the user is exactly divisible by 5 but not by
11.[HSEB 2065]
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("\nEnter any number: ");
scanf("%d",&n);
if(n%5==0 && n%11!=0)
printf("%d is exactly Divisible by 5 but
not by 11",n);
else
printf("condition dissatisfied");
getch();
}
5. Write a C program to find the
commission amount on the basis of sales amount as per following conditions:
Sales amount
(Rs)
Commission
0-1000
5%
1001-2000
10%
>2000
12%
[HSEB 2066]
#include<stdio.h>
#include<conio.h>
void main()
{
float s,ca;
clrscr();
printf("\nEnter sales amount: ");
scanf("%f",&s);
if(s>=0 && s<=1000)
ca=0.05*c;
else if(s>1000 && s<=2000)
ca=0.1*c;
else
ca=0.12*c;
printf("Your Commission is Rs.
%.2f",ca);
getch();
}
6. Write a program to display name of the
day on the basis of entered number 1 to 7. For example, 1 for Sunday. [HSEB
2066]
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
printf("\nEnter number between 1 to 7 ");
scanf("%d",&n);
switch(n)
{
case 1:
printf("\nSUNDAY");
break;
case 2:
printf("\nMONDAY");
break;
case 3:
printf("\nTUESDAY");
break;
case 4:
printf("\nWEDNESDAY");
break;
case 5:
printf("\nTHURSDAY");
break;
case 6:
printf("\nFRIDAY");
break;
case 7:
printf("\nSATURDAY");
break;
default:
printf("\n Invalid
Choice");
}
getch();
}
7. Write a C program to display the sum
of ‘n’ terms of even numbers. [HSEB 2063]
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,s=0,a=2;
clrscr();
printf("\nEnter how many numbers? ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
s+=a;
a=a+2;
}
printf("\n Sum of %d terms of even numbers is
%d",n,s);
getch();
}
8. Write a C program to input a number
and display its multiplication table. [HSEB 2958, 2061]
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
clrscr();
printf("\n Enter number:");
scanf("%d",&n);
for(i=1;i<=10;i++)
printf("\n%d X %d = %d",n,i,n*i);
getch();
}
9. Write a C program to read a positive
number integer less than 20 and display its multiplication table. [HSEB 2062]
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
clrscr();
printf("\n Enter positive number less than
20:");
scanf("%d",&n);
if(n>0 && n<20)
{
for(i=1;i<=10;i++)
printf("\n%d X %d = %d",n,i,n*i);
}
else
printf("\n Invalid number");
getch();
}
10. Write a C program to print 10 terms of any series using
FOR loop. [HSEB 2064]
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,a=5;
clrscr();
printf("\n Enter how many numbers?");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("%d\t",a);
a=a+5;
}
getch();
}
11. Write a C program to print 10 terms of the following series
using FOR loop, 1, 5, 9, 13 [HSEB 2063]
#include<stdio.h>
#include<conio.h>
void main()
{
int i,a=1;
clrscr();
for(i=0;i<10;i++)
{
printf("%d\t",a);
a=a+4;
}
getch();
}
12. Write a C program to read a four digit number and display it
in reverse order. [HSEB 2055]
#include<stdio.h>
#include<conio.h>
void main()
{
int c=0,n,r,s=0;
clrscr();
printf("\n Enter any 4 digit number: ");
scanf("%d",&n);
while(n!=0)
{
r=n%10;
s=s*10+r;
n=n/10;
c=c+1;
}
if(c<=4)
printf("\n Reversed number is %d",s);
else
printf("\n It is not a 4 digit number");
getch();
}
13. Write a C program to find the factorial of a given positive
number. [HSEB 2066]
#include<stdio.h>
#include<conio.h>
void main()
{
int n,f=1,i;
clrscr();
printf("\n Enter positive number: ");
scanf("%d",&n);
if(n<0)
printf("\n You have entered negative
number");
else if(n==0)
printf("\n Factorial of %d is 1",n);
else
{
for(i=1;i<=n;i++)
f=f*i;
printf("\n Factorial of %d is
%d",n,f);
}
getch();
}
14. Write a C program to
print 10 positive integers and their factorials. [HSEB 2062]
#include<stdio.h>
#include<conio.h>
void main()
{
int n,f=1,i;
clrscr();
printf("\n Enter positive number: ");
scanf("%d",&n);
if(n<0)
printf("\n You have entered negative
number");
else if(n==0)
printf("\n Factorial of %d is 1",n);
else
{
for(i=1;i<=n;i++)
{
printf("%d\t",i);
f=f*i;
}
printf("\n Factoria of %d is
%d",n,f);
}
getch();
}
15. Write a program to input an integer number and check whether
it is prime or not. [HSEB 2066]
#include<stdio.h>
#include<conio.h>
void main()
{
int n,c=0,i;
clrscr();
printf("\nEnter any integer number: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
c=c+1;
}
if(c==2)
printf("%d is prime number",n);
else
printf("%d is not prime number",n);
getch();
}
16. Write a C program to input ‘n’ numbers and find out the
largest and smallest number. [HSEB 2062]
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,num[100],g,s;
clrscr();
printf("\nEnter how many numbers?");
scanf("%d",&n);
printf("\nEnter %d numbers",n);
for(i=0;i<n;i++)
scanf("%d",&num[i]);
g=num[0];
s=num[0];
for(i=1;i<n;i++)
{
if (num[i]>g)
g=num[i];
if(num[i]<s)
s=num[i];
}
printf("\nThe greatest number is %d",g);
printf("\nThe smallest number is %d",s);
getch();
}
17. Write a program to ask any n numbers from the user. Sort them
in ascending order and display. [HSEB 2065, 2067]
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,num[10],temp;
clrscr();
printf("Enter how many numbers?");
scanf("%d",&n);
printf("\nEnter %d numbers",n);
for(i=0;i<n;i++)
scanf("%d",&num[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(num[i]>num[j])
{
temp=num[i];
num[i]=num[j];
num[j]=temp;
}
}
}
printf("\n The sorted numbers in ascending order are\n");
for(i=0;i<n;i++)
printf("%d\t",num[i]);
getch();
}
18. Write a program to store ten different constant variables in
an array and print out the greatest number. [HSEB 2064]
#include<stdio.h>
#include<conio.h>
void main()
{
int i,num[10],g;
clrscr();
printf("\nEnter 10 numbers");
for(i=0;i<10;i++)
scanf("%d",&num[i]);
g=num[0];
for(i=1;i<10;i++)
{
if (num[i]>g)
g=num[i];
}
printf("\nThe greatest number is %d",g);
getch();
}
19. Write a program to sort integer variables in descending
order. [HSEB 2063]
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,num[10],temp;
clrscr();
printf("Enter how many numbers?");
scanf("%d",&n);
printf("\nEnter %d numbers",n);
for(i=0;i<n;i++)
scanf("%d",&num[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(num[i]<num[j])
{
temp=num[i];
num[i]=num[j];
num[j]=temp;
}
}
}
printf("\n The sorted numbers in ascending order
are\n");
for(i=0;i<n;i++)
printf("%d\t",num[i]);
getch();
}
20. Write a C program to read salaries of 200 employees and count
the number of employees getting salary between 5000 to 10000. [HSEB 2062]
#include<stdio.h>
#include<conio.h>
void main()
{
int i,c=0;
float s[200];
clrscr();
printf("\nEnter salaries for 200
employees");
for(i=0;i<200;i++)
scanf("%f",&s[i]);
for(i=0;i<200;i++)
{
if (s[i]>5000 &&
s[i]<10000)
c=c+1;
}
printf("Total number of employees getting
salary between 5000 and 10000 are %d",c);
getch();
}
21. Write a program using C language to read the age of 100
persons and count the number of persons in the age group between 50 and 60. Use
FOR and CONTINUE statement. [HSEB 2061]
#include<stdio.h>
#include<conio.h>
void main()
{
int i,c=0;
float a[100];
clrscr();
printf("\nEnter age of 100 persons");
for(i=0;i<100;i++)
scanf("%f",&a[i]);
for(i=0;i<100;i++)
{
if (a[i]>50 && a[i]<60)
c=c+1;
else
continue;
}
printf("Total number of persons aged between 50
and 60 are %d",c);
getch();
}
22. Write a C program to read age of 40 students and count the
number of students of the age between 15 and 22. [HSEB 2063]
#include<stdio.h>
#include<conio.h>
void main()
{
int i,c=0;
float a[40];
clrscr();
printf("\nEnter age of 40 students");
for(i=0;i<40;i++)
scanf("%f",&a[i]);
for(i=0;i<40;i++)
{
if (a[i]>15 && a[i]<22)
c=c+1;
}
printf("Total number of students aged between
15 and 21 are %d",c);
getch();
}
23. Write a program in C to store mark obtained by ‘n’ students
and count the number of students who obtained mark greater than 70. Also count
the number of students who are failed. (<35) [HSEB 2066]
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,c=0,cf=0;
float m[100];
clrscr();
printf("\nEnter how many students?");
scanf("%d",&n);
printf("\n Enter marks for %d students: ",n);
for(i=0;i<n;i++)
scanf("%f",&m[i]);
for(i=0;i<n;i++)
if(m[i]>70)
c=c+1;
else if(m[i]<35)
cf=cf+1;
printf("\n Total no. of students scoring more than 70 are
%d ",c);
printf("\nTotal no. of students who are fail are %d
",cf);
getch();
}
24. Write a program to read elements of the two matrices of order
3 x 3 and perform the matrix addition. [HSEB 2065]
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3], b[3][3],s[3][3],i,j;
clrscr();
printf("\n Enter elements for matrix A\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\nEnter the number
[%d] [%d] ",i,j);
scanf("%d",&a[i][j]);
}
}
printf("\n Enter the elements for matrix B\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\nEnter the number
[%d] [%d] ",i,j);
scanf("%d",&b[i][j]);
}
}
printf("\n The sum of two matrix is\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
s[i][j]=a[i][j]+b[i][j];
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",s[i][j]);
}
printf("\n");
}
getch();
}
25. Write a program to count the number of vowels and consonants
in a given text. [HSEB 2064, 2066]
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[20];
int nv=0,nc=0,i;
printf("\nEnter any string");
gets(str);
strupr(str);
for(i=0;str[i]!='\0';i++)
{
if(str[i]=='A' || str[i]=='E' || str[i]=='I' || str[i]=='O' ||
str[i]=='U')
nv++;
else if(str[i]>='A' && str[i]<='Z')
nc++;
}
printf("\n No. of Vowels = %d ",nv);
printf("\n No. of Consonants = %d ",nc);
getch();
}
26. Write a program to read a line of text and convert it into
uppercase. [HSEB 2068]
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char string[100];
printf("\nEnter any line of text in lowercase\n");
gets(string);
strupr(string);
printf("\n Entered text converted into uppercase\n");
puts(string);
getch();
}
OR
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char string[100];
int i;
printf("\nEnter any line of text in lowercase\n");
gets(string);
for(i=0;string[i]!=0;i++)
{
if(string[i]>='a' && string[i]<='z')
string[i]=string[i]-32;
}
printf("\n Enterd text converted into uppercase\n");
puts(string);
getch();
}
27. Write a program to input n names and sort them in
alphabetical order. [HSEB 2062, 2068]
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[50][20],temp[20];
int i,n,j;
printf("\nEnter how many names: ");
scanf("%d",&n);
printf("Enter %d names\n",n);
for(i=0;i<n;i++)
scanf("%s",name[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(name[i],name[j])>0)
{
strcpy(temp,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],temp);
}
}
}
printf("\nThe sorted names are\n");
for(i=0;i<n;i++)
printf("\n%s",name[i]);
getch();
}
28. Write a C Program to enter name of students and age of ten
different students in array and arrange them in descending order according to
the age and print them. [HSEB 2057]
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct student
{
char name [25];
int age;
std[10];
void main()
{
char temp[25];
int i,j,tm;
printf("Enter 10 names and age of students\n");
for(i=0;i<10;i++)
{
scanf("%s",std[i].name);
scanf("%d",&std[i].age);
}
for(i=0;i<10;i++)
{
for(j=i+1;j<10;j++)
{
if(std[i].age<std[j].age)
{
tm=std[i].age;
std[i].age=std[j].age;
std[j].age=tm;
strcpy(temp,std[i].name);
strcpy(std[i].name,std[j].name);
strcpy(std[j].name,temp);
}
}
}
printf("\nThe sorted names and age in descending order
according to age are\n");
for(i=0;i<10;i++)
printf("\n%s\t%d",std[i].name,std[i].age);
getch();
}
29. Write a program to store name and mark of 20 students. Sort
the data according to mark in descending order and display them. [HSEB 2066]
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct student
{
char name [25];
int mark;
}std[10];
void main()
{
char temp[25];
int i,j,tm;
printf("Enter names and marks for 20 students\n");
for(i=0;i<20;i++)
{
scanf("%s",std[i].name);
scanf("%d",&std[i].mark);
}
for(i=0;i<20;i++)
{
for(j=i+1;j<20;j++)
{
if(std[i].mark<std[j].mark)
{
tm=std[i].mark;
std[i].mark=std[j].mark;
std[j].mark=tm;
strcpy(temp,std[i].name);
strcpy(std[i].name,std[j].name);
strcpy(std[j].name,temp);
}
}
}
printf("\nThe sorted names and marks in descending order
according to marks are\n");
for(i=0;i<20;i++)
printf("\n%s\t%d",std[i].name,std[i].mark);
getch();
}
30. Write a C program to store Kathmandu valley’s 7 days maximum
and minimum temperature (in centigrade) and calculate average, maximum, minimum
temperature using function and print 7 days temperature, minimum, maximum and
average temperature using any high level programming language. [HSEB 2060]
#include<stdio.h>
#include<conio.h>
float maxt(float []);
float min(float []);
float avg(float []);
float m[7],mi[7],a[7];
void main()
{
float maxtemp, mintemp, avgtemp;
int i;
clrscr();
maxtemp=maxt(m);
mintemp=min(mi);
avgtemp=avg(a);
printf("\n\tMax Temp\t Min Temp \t Average Temp\n");
for(i=1;i<=7;i++)
{
printf("\nDay %d\t%f\t%f\t%f\n",i,m[i],mi[i],a[i]);
}
printf("\n Maximum Temperature is %f",maxtemp);
printf("\n MInimum Temperature is %f",mintemp);
printf("\n Average Temperature is %f",avgtemp);
getch();
}
float maxt(float m[])
{
int i;
float tm;
for(i=1;i<=7;i++)
{
printf("\nEnter maximum temperature for day %d ", i);
scanf("%f",&m[i]);
}
tm=m[1];
for(i=2;i<=7;i++)
{
if (m[i]>tm)
tm=m[i];
}
return tm;
}
float min(float mi[])
{
int i;
float tmi;
for(i=1;i<=7;i++)
{
printf("\nEnter minimum temperature for day %d ", i);
scanf("%f",&mi[i]);
}
tmi=mi[1];
for(i=2;i<=7;i++)
{
if (mi[i]<tmi)
tmi=mi[i];
}
return tmi;
}
float avg(float a[])
{
int i;
float ta,s;
for(i=1;i<=7;i++)
{
a[i]=(m[i]+mi[i])/2;
s=s+a[i];
}
ta=s/7;
return ta;
}
31. Write a C program to input a message from keyboard and
display the menu
a. Print the message length in
terms of characters.
b. print the message in reverse order
c. print the message in capital
letters
d. copy the message from one location of
screen to another location. [HSEB 2060]
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char msg[100],msg1[100];
int i,ch,len,j;
clrscr();
printf("\n Enter a message:");
gets(msg);
printf("\n 1. Print the message length in terms of
characters");
printf("\n 2. Print the message in reverse order");
printf("\n 3. Print the message in capital letters");
printf("\n 4. Copy the message from one location to
another");
printf("\n Enter your choice (1-4)");
scanf("%d",&ch);
switch(ch)
{
case 1:
len=0;
while(msg[len]!='\0')
len++;
printf("\n The string %s has %d characters\n",msg,len);
break;
case 2:
len=strlen(msg);
j=0;
for(i=len-1;i>=0;i--)
msg1[j++]=msg[i];
msg[j]='\0';
strcpy(msg,msg1);
printf("\n The reversed string is %s",msg);
break;
case 3:
for(i=0;msg[i]!='\0';i++)
{
if(msg[i]>='a' && msg[i]<='z')
msg[i]=msg[i]-32;
}
printf("\n The message in uppercase %s",msg);
break;
case 4:
for(i=0;msg[i]!='\0';i++)
msg1[i]=msg[i];
msg1[i]='\0';
printf("The copied string is %s ",msg1);
break;
default:
printf("\n Invalid choice");
}
getch();
}
32. Write a program to find the sum of n integer numbers using
function. {HSEB 2066]
#include<stdio.h>
#include<conio.h>
int sum(int);
void main()
{
int n,a;
clrscr();
printf("\nEnter how many numbers: ");
scanf("%d",&n);
a=sum(n);
printf("\n Sum of %d numbers= %d",n,a);
getch();
}
int sum(int n)
{
int i,s=0;
for(i=1;i<=n;i++)
s=s+i;
returns;
}
33. Write a program to calculate the factorial of a given number
using function. [HSEB 2063]
#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
{
int n,a;
clrscr();
printf("\nEnter any number: ");
scanf("%d",&n);
a=fact(n);
printf("\n factorial= %d",a);
getch();
}
int fact(int n)
{
int i,f=1;
for(i=1;i<=n;i++)
f=f*i;
return f;
}
34. Write a program to calculate the factorial of a given number
using recursive function. [HSEB 2064, 2068]
#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
int n,a;
clrscr();
printf("\nEnter any number: ");
scanf("%d",&n);
a=fact(n);
printf("\n factorial= %d",a);
getch();
}
int fact(int n)
if(n<=1)
return 1;
else
return(n*fact(n-1));
}
35. Write a program using user defined function to calculate y
raise to power x.[HSEB 2067]
#include<stdio.h>
#include<conio.h>
int power(int,int);
void main()
{
int y,x,p;
printf("\n Enter values for y and x: ");
scanf("%d%d",&y,&x);
p=power(y,x);
printf("\n y raise to power x= %d",p);
getch();
}
int power(int y, int x)
{
int pw=1,i;
for(i=1;i<=x;i++)
pw=pw*y;
return pw;
}
36. Write a program that reads different names and addresses into
the computer and rearrange them into alphabetical order using the structure
variables. [HSEB 2061, 2064]
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct student
{
char name[30];
char add [30];
}std[100];
void main()
{
char tname[30],tadd[30];
int i,j,n;
printf("\n Enter how many students: ");
scanf("%d",&n);
printf("Enter names and addresses for %d students: ",n);
for(i=0;i<n;i++)
scanf("%s%s",std[i].name, std[i].add);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if (strcmp(std[i].name,std[j].name)>0)
{
strcpy(tname,std[i].name);
strcpy(std[i].name,std[j].name);
strcpy(std[j].name,tname);
strcpy(tadd,std[i].add);
strcpy(std[i].add,std[j].add);
strcpy(std[j].add,tadd);
}
}
}
printf("\n Sorted names in alphabetical order according to names
are:\n");
for(i=0;i<n;i++)
printf("\n %s\t %s",std[i].name,std[i].add);
getch();
}
37. Write a program to show data writing and reading operation
to/from a data file. [HSEB 2066]
#include<stdio.h>
#include<conio.h>
struct
{
int roll;
char name[25];
float mark;
}std;
void main()
{
int n,i;
FILE *fp;
fp=fopen("d:\\cprg\student.txt","wb");
clrscr();
printf("\n Enter how many records: ");
scanf("%d",&n);
printf("enter student number name and marks for %d
students",n);
for(i=0;i<n;i++)
{
scanf("%d%s%f",&std.roll,std.name,&std.mark);
fwrite(&std,sizeof(std),1,fp);
}
fclose(fp);
fp=fopen("d:\\cprg\student.txt","r");
printf("\nRoll\tName\tMarks Obtained\n");
while(fread(&std,sizeof(std),1,fp))
printf("%d\t%s\t%f\n",std.roll,std.name,std.mark);
fclose(fp);
getch();
}
38. Write a program to enter name, roll-number and marks of 10
students and store them in a file. [HSEB 2065]
# include<stdio.h>
#include<conio.h>
struct
{
int roll;
char name[25];
float mark;
}std;
void main()
{
int i;
FILE *fp;
fp=fopen("d:\\cprg\student.txt","wb");
clrscr();
printf("enter student roll number name and marks for 10
students");
for(i=0;i<10;i++)
{
scanf("%d%s%f",&std.roll,std.name,&std.mark);
fwrite(&std,sizeof(std),1,fp);
}
fclose(fp);
getch();
}
39. Write a program to store std-no, name and mark of ‘n’
students in a data file. Display the records in appropriate format reading from
the file. [HSEB 2066]
#include<stdio.h>
#include<conio.h>
struct
{
int roll;
char name[25];
float mark;
}std;
void main()
{
int n,i;
FILE *fp;
fp=fopen("d:\\cprg\student.txt","w");
clrscr();
printf("\n Enter how many records: ");
scanf("%d",&n);
printf("enter student number name and marks for %d
students",n);
for(i=0;i<n;i++)
{
scanf("%d%s%f",&std.roll,std.name,&std.mark);
fprintf(fp,"%d\t%s\t%f\n",std.roll,std.name,std.mark);
}
fclose(fp);
fp=fopen("d:\\cprg\student.txt","r");
printf("\nRoll\tName\tMarks Obtained\n");
while(fscanf(fp,"%d%s%f",&std.roll,std.name,&std.mark)!=EOF)
printf("%d\t%s\t%f\n",std.roll,std.name,std.mark);
fclose(fp);
getch();
}
40. Write a program using C language that reads successive
records from the new data file and display each record on the screen in an
appropriate format. [HSEB 2061, 2062]
#include<stdio.h>
#include<conio.h>
struct
{
int roll;
char name[25];
float mark;
}std;
void main()
{
int n,i;
FILE *fp;
fp=fopen("d:\\cprg\student.txt","wb");
clrscr();
printf("\n Enter how many records: ");
scanf("%d",&n);
printf("enter student number name and marks for %d
students",n);
for(i=0;i<n;i++)
{
scanf("%d%s%f",&std.roll,std.name,&std.mark);
fwrite(&std,sizeof(std),1,fp);
}
fclose(fp);
fp=fopen("d:\\cprg\student.txt","r");
printf("\nRoll\tName\tMarks Obtained\n");
while(fread(&std,sizeof(std),1,fp))
printf("%d\t%s\t%f\n",std.roll,std.name,std.mark);
fclose(fp);
getch();
}
41. Write a program to rename and delete a data file using rename
and remove command. [HSEB 2064, 2067]
#include<stdio.h>
#include<conio.h>
void main()
{
char filename[20];
char oldfilename[20],newfilename[20];
printf("\n Enter the file name to be removed: ");
gets(filename);
if(remove(filename)==0)
printf("File %s is removed",filename);
else
printf("File %s cannot be removed",filename);
printf("\n Enter old file name: ");
gets(oldfilename);
printf("\n Enter new file name: ");
gets(newfilename);
if(rename(oldfilename,newfilename)==0)
printf("\n File %s is renamed to
%s",oldfilename,newfilename);
else
printf("\n file %s cannot be renamed",oldfilename);
getch();
}
42. Write a program to open a new file and read roll-no,
name, address and phone number of students until the user says “no”, after
reading the data, write it to the file then display the content of the file.
[HSEB 2068]
#include<stdio.h>
#include<conio.h>
struct
{
int roll;
char name[25];
char add[30];
long phone;
}std;
oid main()
{
char ch='y';
FILE *fp;
fp=fopen("d:\\cprg\student.txt","w");
clrscr();
while(ch=='y' || ch=='Y')
{
printf("\n Enter roll number: ");
scanf("%d",&std.roll);
printf("\n Enter name: ");
scanf("%s",std.name);
printf("\n Enter address: ");
scanf("%s",std.add);
printf("\n Enter phone number: ");
scanf("%ld",&std.phone);
fprintf(fp,"%d\t%s\t%s\t%ld\n",std.roll,std.name,std.add,std.phone);
printf("DO you want to continue (Y/N)? ");
ch=getch();
}
fclose(fp);
fp=fopen("d:\\cprg\student.txt","r");
printf("\nRoll\tName\tAddress\tPhone\n");
while(fscanf(fp,"%d%s%s%ld",&std.roll,std.name,std.add,&std.phone)!=EOF)
printf("%d\t%s\t%s\t%ld\n",std.roll,std.name,std.add,std.phone);
fclose(fp);
getch();
}
C Program HSEB Question Solved PART II
1.
Write a program to read a line of text and convert it into uppercase. [HSEB
2068]
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char string[100];
printf("\nEnter any line of text in lowercase\n");
gets(string);
strupr(string);
printf("\n Enterd text converted into uppercase\n");
puts(string);
getch();
}
OR
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char string[100];
int i;
printf("\nEnter any line of text in lowercase\n");
gets(string);
for(i=0;string[i]!=0;i++)
{
if(string[i]>='a' && string[i]<='z')
string[i]=string[i]-32;
}
printf("\n Entered text converted into uppercase\n");
puts(string);
getch();
}
2. Write a program to input n names
and sort them in alphabetical order. [HSEB 2062, 2068]
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[50][20],temp[20];
int i,n,j;
printf("\nEnter how many names: ");
scanf("%d",&n);
printf("Enter %d names\n",n);
for(i=0;i<n;i++)
scanf("%s",name[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(name[i],name[j])>0)
{
strcpy(temp,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],temp);
}
}
}
printf("\nThe sorted names are\n");
for(i=0;i<n;i++)
printf("\n%s",name[i]);
getch();
}
3. Write a C Program to enter name of students and age of
ten different students in array and arrange them in descending order according
to the age and print them. [HSEB 2057]
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct student
{
char name [25];
int age;
}std[10];
void main()
{
char temp[25];
int i,j,tm;
printf("Enter 10 names and age of students\n");
for(i=0;i<10;i++)
{
scanf("%s",std[i].name);
scanf("%d",&std[i].age);
}
for(i=0;i<10;i++)
{
for(j=i+1;j<10;j++)
{
if(std[i].age<std[j].age)
{
tm=std[i].age;
std[i].age=std[j].age;
std[j].age=tm;
strcpy(temp,std[i].name);
strcpy(std[i].name,std[j].name);
strcpy(std[j].name,temp);
}
}
}
printf("\nThe sorted names and age in descending order
according to age are\n");
for(i=0;i<10;i++)
printf("\n%s\t%d",std[i].name,std[i].age);
getch();
}
4. Write a program to store name and mark of 20 students. Sort the
data according to mark in descending order and display them. [HSEB 2066]
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct student
{
char name [25];
int mark;
}std[10];
void main()
{
char temp[25];
int i,j,tm;
printf("Enter names and marks for 20 students\n");
for(i=0;i<20;i++)
{
scanf("%s",std[i].name);
scanf("%d",&std[i].mark);
}
for(i=0;i<20;i++)
{
for(j=i+1;j<20;j++)
{
if(std[i].mark<std[j].mark)
{
tm=std[i].mark;
std[i].mark=std[j].mark;
std[j].mark=tm;
strcpy(temp,std[i].name);
strcpy(std[i].name,std[j].name);
strcpy(std[j].name,temp);
}
}
}
printf("\nThe sorted names and marks in descending order
according to marks are\n");
for(i=0;i<20;i++)
printf("\n%s\t%d",std[i].name,std[i].mark);
getch();
}
5. Write a program to find the sum
of n integer numbers using function. {HSEB 2066]
#include<stdio.h>
#include<conio.h>
int sum(int);
void main()
{
int n,a;
clrscr();
printf("\nEnter how many numbers: ");
scanf("%d",&n);
a=sum(n);
printf("\n Sum of %d numbers= %d",n,a);
getch();
}
int sum(int n)
{
int i,s=0;
for(i=1;i<=n;i++)
s=s+i;
return s;
}
6. Write a program to calculate the
factorial of a given number using function. [HSEB 2063]
#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
{
int n,a;
clrscr();
printf("\nEnter any number: ");
scanf("%d",&n);
a=fact(n);
printf("\n factorial= %d",a);
getch();
}
int fact(int n)
{
int i,f=1;
for(i=1;i<=n;i++)
f=f*i;
return f;
}
7. Write a program to calculate the
factorial of a given number using recursive function. [HSEB 2064, 2068]
#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
{
int n,a;
clrscr();
printf("\nEnter any number: ");
scanf("%d",&n);
a=fact(n);
printf("\n factorial= %d",a);
getch();
}
int fact(int n)
{
if(n<=1)
return 1;
else
return(n*fact(n-1));
}
8. Write a program using user
defined function to calculate y raise to power x.[HSEB 2067]
#include<stdio.h>
#include<conio.h>
int power(int,int);
void main()
{
int y,x,p;
printf("\n Enter values for y and x: ");
scanf("%d%d",&y,&x);
p=power(y,x);
printf("\n y raise to power x= %d",p);
getch();
}
int power(int y, int x)
{
int pw=1,i;
for(i=1;i<=x;i++)
pw=pw*y;
return pw;
}
9. Write a program that reads
different names and addresses into the computer and rearrange them into
alphabetical order using the structure variables. [HSEB 2061, 2064]
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct student
{
char name[30];
char add [30];
}std[100];
void main()
{
char tname[30],tadd[30];
int i,j,n;
printf("\n Enter how many students: ");
scanf("%d",&n);
printf("Enter names and addresses for %d srudents: ",n);
for(i=0;i<n;i++)
scanf("%s%s",std[i].name, std[i].add);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if (strcmp(std[i].name,std[j].name)>0)
{
strcpy(tname,std[i].name);
strcpy(std[i].name,std[j].name);
strcpy(std[j].name,tname);
strcpy(tadd,std[i].add);
strcpy(std[i].add,std[j].add);
strcpy(std[j].add,tadd);
}
}
}
printf("\n Sorted names in alphabetical order according to names
are:\n");
for(i=0;i<n;i++)
printf("\n %s\t %s",std[i].name,std[i].add);
getch();
}
10. Write a program to show data writing and reading
operation to/from a data file. [HSEB 2066]
#include<stdio.h>
#include<conio.h>
struct
{
int roll;
char name[25];
float mark;
}std;
void main()
{
int n,i;
FILE *fp;
fp=fopen("d:\\cprg\student.txt","wb");
clrscr();
printf("\n Enter how many records: ");
scanf("%d",&n);
printf("enter student number name and marks for %d
students",n);
for(i=0;i<n;i++)
{
scanf("%d%s%f",&std.roll,std.name,&std.mark);
fwrite(&std,sizeof(std),1,fp);
}
fclose(fp);
fp=fopen("d:\\cprg\student.txt","r");
printf("\nRoll\tName\tMarks Obtained\n");
while(fread(&std,sizeof(std),1,fp))
printf("%d\t%s\t%f\n",std.roll,std.name,std.mark);
fclose(fp);
getch();
}
11. Write a program to enter name, roll-number and marks of
10 students and store them in a file. [HSEB 2065]
# include<stdio.h>
#include<conio.h>
struct
{
int roll;
char name[25];
float mark;
}std;
void main()
{
int i;
FILE *fp;
fp=fopen("d:\\cprg\student.txt","wb");
clrscr();
printf("enter student roll number name and marks for 10 students");
for(i=0;i<10;i++)
{
scanf("%d%s%f",&std.roll,std.name,&std.mark);
fwrite(&std,sizeof(std),1,fp);
}
fclose(fp);
getch();
}
12. Write a program to store std-no, name and mark of ‘n’
students in a data file. Display the records in appropriate format reading from
the file. [HSEB 2066]
#include<stdio.h>
#include<conio.h>
struct
{
int roll;
char name[25];
float mark;
}std;
void main()
{
int n,i;
FILE *fp;
fp=fopen("d:\\cprg\student.txt","w");
clrscr();
printf("\n Enter how many records: ");
scanf("%d",&n);
printf("enter student number name and marks for %d
students",n);
for(i=0;i<n;i++)
{
scanf("%d%s%f",&std.roll,std.name,&std.mark);
fprintf(fp,"%d\t%s\t%f\n",std.roll,std.name,std.mark);
}
fclose(fp);
fp=fopen("d:\\cprg\student.txt","r");
printf("\nRoll\tName\tMarks Obtained\n");
while(fscanf(fp,"%d%s%f",&std.roll,std.name,&std.mark)!=EOF)
printf("%d\t%s\t%f\n",std.roll,std.name,std.mark);
fclose(fp);
getch();
}
13. Write a program using C language that reads successive
records from the new data file and display each record on the screen in an
appropriate format. [HSEB 2061, 2062]
#include<stdio.h>
#include<conio.h>
struct
{
int roll;
char name[25];
float mark;
}std;
void main()
{
int n,i;
FILE *fp;
fp=fopen("d:\\cprg\student.txt","wb");
clrscr();
printf("\n Enter how many records: ");
scanf("%d",&n);
printf("enter student number name and marks for %d
students",n);
for(i=0;i<n;i++)
{
scanf("%d%s%f",&std.roll,std.name,&std.mark);
fwrite(&std,sizeof(std),1,fp);
}
fclose(fp);
fp=fopen("d:\\cprg\student.txt","r");
printf("\nRoll\tName\tMarks Obtained\n");
while(fread(&std,sizeof(std),1,fp))
printf("%d\t%s\t%f\n",std.roll,std.name,std.mark);
fclose(fp);
getch();
}
14. Write a program to rename and delete a data file using
rename and remove command. [HSEB 2064, 2067]
#include<stdio.h>
#include<conio.h>
void main()
{
char filename[20];
char oldfilename[20],newfilename[20];
printf("\n Enter the file name to be removed: ");
gets(filename);
if(remove(filename)==0)
printf("File %s is removed",filename);
else
printf("File %s cannot be removed",filename);
printf("\n Enter old file name: ");
gets(oldfilename);
printf("\n Enter new file name: ");
gets(newfilename);
if(rename(oldfilename,newfilename)==0)
printf("\n File %s is renamed to
%s",oldfilename,newfilename);
else
printf("\n file %s cannot be renamed",oldfilename);
getch();
}
15. Write a
program to open a new file and read roll-no, name, address and phone number of
students until the user says “no”, after reading the data, write it to the file
then display the content of the file. [HSEB 2068]
#include<stdio.h>
#include<conio.h>
struct
{
int roll;
char name[25];
char add[30];
long phone;
}std;
void main()
{
char ch='y';
FILE *fp;
fp=fopen("d:\\cprg\student.txt","w");
clrscr();
while(ch=='y' || ch=='Y')
{
printf("\n Enter roll number: ");
scanf("%d",&std.roll);
printf("\n Enter name: ");
scanf("%s",std.name);
printf("\n Enter address: ");
scanf("%s",std.add);
printf("\n Enter phone number: ");
scanf("%ld",&std.phone);
fprintf(fp,"%d\t%s\t%s\t%ld\n",std.roll,std.name,std.add,std.phone);
printf("DO you want to continue (Y/N)? ");
ch=getch();
}
fclose(fp);
fp=fopen("d:\\cprg\student.txt","r");
printf("\nRoll\tName\tAddress\tPhone\n");
while(fscanf(fp,"%d%s%s%ld",&std.roll,std.name,std.add,&std.phone)!=EOF)
printf("%d\t%s\t%s\t%ld\n",std.roll,std.name,std.add,std.phone);
fclose(fp);
getch();
}
Important Programs
1.
Write a program for the Palindrome.
# include<stdio.h>
# include<conio.h>
Void main()
{
int n,reverseinteger=0,remainder=0;
printf("Enter an integer=");
scanf(%d",&n);
original integer=n;
while (n!=0)
remainder =n%10;
reverseinteger=reverseinteger n/10;
originalinteger==reverseinteger;
printf("%d is a palindrome");
else
printf(%d is not a palindrome");
}
2.
Write a program to print the prime numbers
till 100.
#include <stdio.h>
#include<conio.h>
void main()
{
int n,i,j;
for(n=1;n<=100,n++)
{
i=0;
for(j=1;j<=n;j++)
{
if(n%j==0)
i++
}
if(i==2)
printf("\n%d",n);
}
}
3.
Write a program to display in the following
format : -
1
12
123
1234
12345
#include<stdio.h>
#include<conio.h>
void main()
{
int I,j,;
{
for(i=1;i<=5;i++)
for(j=1;j<=i;j++)
{
printf("%d",j);
printf("\n");
}}}
4.
Write a program to find sum, difference and
product of 2 numbers using switch case statement.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,ch;
printf("enter two numbers:-");
scandf("%d%d",&a&b);
printf("\n1.\tSum.");
printf("\n2.\tDifference");
printf("\n3.\tProduct");
printf("\n4.\tEnter your choice (1,2,3)");
scanf("%d",&ch);
switch(ch);
{
case 1:
c=a+b;
printf("The sum of two numbers is %d",c);
break;
case 1:
c=a-b;
printf("The difference of two numbers is
:-%d",c);
break:
case 3:
c=a*b;
printf("The product of two numbers
is:-%d",c);
break:
default:
printf("Wrong choice ! Enter 1,2,3");
}
5.
Write a program to multiply two matrix. (2071-8)
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3],i,j,k;
int sum=0;
printf("enter the 1st matrix:-");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("enter the second matrix:-");
for(i=o;i<3;i++)
for(j=0;j<3;j++)
scanf(%d",&b[i][j]); //multiplication
for(i=0;i<3;i++)
for(j=0;j<3;j++)
for(k=0;k<3;k++)
c[i][j]=a[i][k]*b[k][j];
printf("multiplication of two matrix is : -
\n\t%d",c[i][j]);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
}
6.
Write a program to add two numbers using
function.(2073-5)
#include<stdio.h>
#include<conio.h>
void main()
{
int num1,num2,num;
printf("enter any two numbers:-\n");
scanf("%d%d",&num1,num2);
num=sum(num1,num2);
printf("\naddition of two numbers is
:-%d",num);
}
{
int sum(int num1,num2);
int num3;
num3=num1+num2;
return(num3);
}
7.
Write a program to sort twenty integer
number's in ascending order.(2073-8)
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a[100],n,temp;
priitf("enter the no. of elements:-");
scanf("%d",&n);
printf("enter the numbers : - " );
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(a[i]>a[j])
temp=a[i];
a[i]=a[j];
a[j]=temp;
printf("the no's in ascending order are :
-\n%d",a[i]);
for(i=o;i<n;i++);
}
8.
Write a program to display first ten even
no's. (2073-5)
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
printf("enter numbers between 1-10\n");
for(n=1;n<=10;n++)
scanf("%d",&n);
if(n%2==0)
printf("%d",n);
}
or
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
printf("even numbers between 1-10 are\n");
n=2
while(n<=10)
print("%d",n);
n=n+2;
}
9.
Differentiate between structure and union
and write a program to input the employee name and post of five employees and
display the records in proper format using structure. (2073-3+7)
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main
char name[10],post[10];
int i,e,n;
printf("enter the no. of employees");
scanf("%d",&n);
printf("enter the name of employees:-");
for(i=0;i<=n;i++)
scanf("%s",&e[i].name);
printf("enter the post of the employee");
for(i=0;i<n;i++)
scanf("%s"&e.post[10]);
printf("the name of the employee is %s and the
employee post is %s",e[i].name,e[i].post);
for(i=0;i<n;i++)
}
10.
Write a C program to find the sum of n
integer using functions. (2073-6)
#include<stdio.h>
#include<conio.h>
int addnumbers(int n);
void main()
{ int num;
printf("enter any positive number:-");
scanf("%d",&num);
printf("sum=%d",addnumbers(num));
{
int
addnumbers(int n)
if(n!=0)
return n +
addnumbers(n-1);
else
return n;
}
}
11.
Write a program to demonstrate the value of
a variable and address of variable using pointer in C. (2073-5)
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y;
int ptr;x=10;
ptr=&x ;
y=&ptr;
printf("value of x is %d\n\n",x);
printf("%d is stored at address
%u\n",x,&x);
printf("%d is stored at address
%u\n",&x,,&x);
printf("%d is stored at address
%u\n",y,&*ptr);
printf("%d is stored at address
%u\n",ptr,&ptr);
printf("%d is stored at
address%u\n",y,&y);
ptr=25;
printf("\n now x=%d\n",x);
}
12.
Write a C program to read five positive
number using array and find out the smallest among them. (2073-10)
# include<stdio.h>
#include<conio.h>
void main()
{
int array[5];
int i,j,num,temp;
printf("enter the number:-");
scanf("%d",&num);
if(num>0)
printf("enter the elements one by one\n");
for(i=0;i<array[i];i++)
printf("input array is \n",array[i]);
for(i=0;i<array[i];i++)
for(j=0;j<array[i-1];j++)
if(array[j]>array[j+1])
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
printf("sorted array is:-\n");
for(i=0;i<array[i];i++)
printf("%d \n",array[i]);
else;
printf("enter a positive number");
}