C language में एक ऐसा program लिखिए जो user से चार subjects के marks input ले:
- Science
- Math
- English
- Computer
और फिर:
- Total marks
- Percentage
- Grade (A, B, C, D या Fail)
calculate करके display करे।
Program Code
#include<stdio.h>
int main()
{
float science, math, english, computer;
float total, percentage;
printf("Enter marks of Science : ");
scanf("%f", &science);
printf("Enter marks of Math : ");
scanf("%f", &math);
printf("Enter marks of English : ");
scanf("%f", &english);
printf("Enter marks of Computer : ");
scanf("%f", &computer);
total = science + math + english + computer;
percentage = (total / 400) * 100;
printf("\nTotal Marks : %.2f", total);
printf("\nPercentage : %.2f", percentage);
if (percentage >= 90)
printf("\nGrade A");
else if (percentage >= 75)
printf("\nGrade B");
else if (percentage >= 60)
printf("\nGrade C");
else if (percentage >= 40)
printf("\nGrade D");
else
printf("\nTry Next year");
return 0;
}
Program Explanation
यह program real-life marksheet logic पर based है, इसलिए ये program beginners के लिये useful होगा।
1️. Variables का use
float science, math, english, computer;
यह variables चार subjects के marks store करने के लिए हैं।
float इसलिए use किया गया है ताकि decimal marks भी handle हो सकें (जैसे 78.5)।
float total, percentage;
- total → चारों subjects का Marks store करने के लिये
- percentage → Total Percentage Store करने के लिये
2️. Marks Input
scanf(“%f”, &science);
User से one-by-one हर subject के marks Input करने के लिये।
3️. Total और Percentage Calculation
total = science + math + english + computer;
percentage = (total / 400) * 100;
- Maximum marks = 400 (4 subjects × 100)
- Percentage निकालने का formula:
(Obtained Marks / Total Marks) × 100
4️. Output Formatting
printf(“\nTotal Marks : %.2f”, total);
%.2f का use करके output को 2 decimal places तक limit किया गया है, जो professional दिखता है।
5️. Grade Logic (Important Part)
if (percentage >= 90)
यहाँ if–else ladder का use हुआ है:
| Percentage | Grade |
| 90+ | A |
| 75–89 | B |
| 60–74 | C |
| 40–59 | D |
| Below 40 | Fail |
Program top से bottom check करता है और first true condition पर रुक जाता है।
