Program to list the names of students who have scored more than 60% of the total marks in three subjects using structure variables. | |
#include<stdio.h> #include<conio.h> struct student { char name[30]; char usn[10]; int sub1; int sub2; int sub3; int total; float percent; }; main( ) { int i, n; struct student s[20]; //Declaring an array of type 'student' printf("\t\t enter the number of students:"); scanf("%d", &n); printf("%d", n); for(i = 0; i < n, ++i) { getchar(); printf("\n\n enter the student name:"); scanf("%d", &s[i].name ); //Read into name of s[i] gets(s[i].name); puts(s[i].name); printf("\n enter the student university seat number:"); gets(s[i].usn); puts(s[i].usn); printf("\n enter the marks of subject1:"); scanf("%d", &s[i].sub1); printf("%d", s[i].sub1); printf("\n enter the marks of subject2:"); scanf("% d", &s[i].sub2); printf(" %d", s[i].sub2); printf("\n enter the marks of subject3:"); scanf("%d",&s[i] sub3 ); printf ("% d",s[i].sub3 ); } printf(" \n\n\t\t list of students name, university seat number and percentage \n\t\t who have scored more than 60% marks\n"); printf("\n\t\t "); printf("\n\t\t name \t\t\t roll number \t percentage\n"); printf(" \t\t__ \n"); /* Accessing members of each element in the array 's' and processing them*/ for (i = 0; i < n, ++i) { s[i].total = s[i].sub1 + s[i].sub2 + s[i].sub3; s[i].percent = s[i].total/3.0; if (s[i].percent >= 60) printf("\t\t %-20s\t %s\t\t %.2f\n", s[i].name, s[i].usn, s[i].percent); } | Each element in the array now is a structure with its own name, usn, sub1, sub2, sub3, total and percent Reading value into the name member of' ith element in the array 's' All operations can be performed on the members of the structure as if they are individual variables |
Output: enter the number of students: 1 enter the student name: sai enter the student university seat number: 001 enter the marks of subject1: 50 enter the marks of subject2: 1482 enter the marks of subject3: 75 list of students name, university seat number and percentage who have scored more than 60% marks name roll number percentage sat 001 535.67 |
Program to list the names of students who have scored more than 60% of the total marks in three subjects using structure variables.
0
Post a Comment
0 Comments