C Examples

WAP in C to accept and print a student’s result using a structure inputted by user

The Program in C to accept and print a student’s result using a structure inputted by user is given below:

#include <stdio.h>

struct Student {
    char name[100];
    int roll_num;
    float marks[5];
    float total;
    float percentage;
};

void inputStudent(struct Student *s) {
    int i;
    printf("Enter the student's name: ");
    scanf("%s", s->name);
    printf("Enter the student's roll number: ");
    scanf("%d", &s->roll_num);
    printf("Enter the student's marks in 5 subjects: ");
    for (i = 0; i < 5; i++) {
        scanf("%f", &s->marks[i]);
        s->total += s->marks[i];
    }
    s->percentage = s->total / 5;
}

void printStudent(struct Student s) {
    int i;
    printf("Name: %s\n", s.name);
    printf("Roll Number: %d\n", s.roll_num);
    printf("Marks: ");
    for (i = 0; i < 5; i++) {
        printf("%.2f ", s.marks[i]);
    }
    printf("\n");
    printf("Total: %.2f\n", s.total);
    printf("Percentage: %.2f%%\n", s.percentage);
}

int main() {
    struct Student s;
    inputStudent(&s);
    printStudent(s);
    return 0;
}

Output:

Enter the student’s name: alex
Enter the student’s roll number: 17
Enter the student’s marks in 5 subjects: 90
91
96
92
90
Name: alex
Roll Number: 17
Marks: 90.00 91.00 96.00 92.00 90.00
Total: 459.00
Percentage: 91.80%

Pro-Tips💡

This program defines a struct “Student” with members: name, roll_num, marks, total and percentage.

The main function defines a struct of type “Student” and calls the function “inputStudent()” to input the student’s information and calculate the student’s total and percentage.

It then calls the function “printStudent()” to print the student’s information.

The function “inputStudent()” prompts the user to input the student’s name, roll number and marks in 5 subjects, it then calculates the total and percentage and stores them in the struct.

The function “printStudent()” prints the student’s name, roll number, marks, total and percentage.

Please note that the program uses scanf(“%s”,…) and scanf(“%d”,…) to read the input name

and roll number, and scanf(“%f”,…) to read the input marks, and it does not check for the validity of the input, you may want to add some validation on the input to make sure that the input is valid.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

C# Program to Delete Element at Desired position From Array

The Program in C# Program to Delete Element at Desired position From Array is given below: Output: Hello Codeauri Family, enter the number of elements in the array:4Enter…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X