Problem 49

author
By Supreme2023-03-16
Problem Statement :
Write a program to read structure elements from keyboard
Code:

#include <stdio.h>

struct student {
char name[20];
int age;
float marks;
char dept[20];
};

int main() {

//49. Write a program to read structure elements from keyboard.
struct student s;

printf("\tStudent information Form\n");
printf("==========================================\n");

printf("\nEnter Name : ");
gets(s.name);

printf("\nEnter Department : ");
gets(s.dept);

printf("\nEnter Age : ");
scanf("%d", &s.age);

printf("\nEnter Marks : ");
scanf("%f", &s.marks);



printf("\n\n------------------------------------------");
printf("\n\tStudent Information\n");
printf("------------------------------------------\n");
printf("Name : %s\n", s.name);
printf("\nDepartment : %s\n", s.dept);
printf("\nAge : %d\n", s.age);
printf("\nMarks : %.2f\n", s.marks);
printf("------------------------------------------\n");
return 0;
}

Description :
  • This program declares a structure called "student" that has four members: a character array called "name" with a length of 20, an integer called "age", a floating-point number called "marks", and another character array called "dept" with a length of 20.
  • The program then creates a variable called "s" of type "student" and prompts the user to enter their name, department, age, and marks. The input for name and department is taken using the "gets" function, which is generally not recommended due to the possibility of buffer overflow. The input for age and marks is taken using the "scanf" function.
  • Finally, the program prints out the entered student information in a formatted manner. The program ends by returning 0.
Output Image:
Output-image

© Copyright 2022. All right reserved, GTU Coder.