Computer for SEE and NEB

It is a complete SEE and NEB solution for computer science. It includes Computer Fundamentals, Database (SQL), Programming in C QBASIC, CSS, JavaScript, and PHP for beginners.

Breaking

Post Top Ad

Your Ad Spot

Monday, November 21, 2022

C program to read name and marks of 5 different subjects calculate total percentage and display them in tabular format using structure

C program to read name and marks of 5 different subjects calculate total percentage and display them in tabular format using structure


C program to read name and marks of 5 different subjects calculate total percentage and display them in tabular format using structure


Introduction:


C is one of the most popular programming languages used for developing a wide range of applications. It is widely used in various fields, including operating systems, embedded systems, and gaming. In this article, we will explore how to write a C program to read names and marks of five different subjects, calculate the total percentage, and display it in tabular format using structure.

Programming code:


#include<stdio.h>
#include<conio.h>
struct student{
char name[20];
int mark[5];
}list;
main()
{
int tot=0,i;
float per;
printf("Enter name:");
scanf("%s",list.name);
for(i=0;i<=4;i++)
{
printf("Enter mark:");
scanf("%d",&list.mark[i]);
tot=tot+list.mark[i];
}
per=tot/5;
printf("-----------------------\n");
printf("Name:%s\n",list.name);
printf("marks secured\n");
for(i=0;i<=4;i++)
{
printf("%d\t",list.mark[i]);
}
printf("\nTotal marks:%d\tPercentage=%f\n",tot,per);
getch();
}

Explanation of the Code:


  1. This program is designed to read the name and marks of 5 different subjects for a student, calculate the total percentage, and display the results in tabular format using structures.
  2. The program begins by including the standard input/output and console input/output header files using '#include'. It then defines a structure 'student' which contains two variables - an array of characters to store the name of the student and an array of integers to store the marks of 5 subjects.
  3. In the main function, variables 'tot' and 'i' are declared as integers and 'per' as a float. The program prompts the user to enter the name of the student using the 'scanf()' function and stores it in the 'name' variable of the structure 'list'.
  4. Then a for loop is used to prompt the user to enter marks for all the five subjects, which are then stored in the 'mark' variable of the structure 'list'. The 'tot' variable is updated in each iteration of the loop by adding the current marks to the previous sum of marks.
  5. After the loop, the program calculates the percentage of marks obtained by dividing the total marks by 5 and storing the result in the 'per' variable. The program then displays the name, marks obtained in each subject, total marks obtained, and the percentage of marks obtained in a tabular format using 'printf()'.
  6. Finally, the program waits for user input using 'getch()' before terminating.


Output:



FAQs:

Q.1 What is the purpose of using a structure in this program? 
Ans: The structure is used to store the input values in a more organized way. It allows us to group related data items under a single name.

Q.2 How is the total marks calculated in this program? 
Ans: The total marks are calculated by summing up the marks of all five subjects entered by the user.

Q.3 What is the significance of the 'printf()' function in this program?
Ans: The 'printf()' function is used to display the output on the screen in a formatted manner. It allows us to print text and values of variables in a specific order.

Q.4 Can this program be modified to calculate the average marks instead of the total percentage? 
Ans: Yes, this program can be modified to calculate the average marks by dividing the total marks by the number of subjects (i.e., 5 in this case) and displaying the result.

Conclusion:

In this article, we have learned how to write a C program to read names and marks of five different subjects, calculate the total percentage, and display it in tabular format using structure. We have discussed the program logic and the use of various C programming concepts such as structures, input/output functions, loops, and conditional statements. By following the code and explanation given in this article, you can easily create your own C program to calculate total percentage and display it in tabular format.


No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages