Welcome to Easy Notes Nepal

We are here to serve you!!

HSEB C PROGRAM SOLUTION

 

HSEB C PROGRAM 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 Factoria 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 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 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;

 return s;

}

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 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();

}

 

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]

# winclude<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;

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=getche();

  }

  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 III

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 Enterd 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]

# winclude<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]

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=getche();

  }

  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();

  }

 

 


Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Top Post Ad

Below Post Ad

Followers