In: Computer Science
Complete the following assignment in C programming language.
Below is the code for the above question in C:
#include <stdio.h>
int main()
{
char first_name[20];
char name[20];
int exam_scores[3];
int sum = 0;
int i;
printf("Enter your name : ");
scanf("%s", first_name);
for (int j = 0; first_name[j] != '\0'; j++)
{
name[j] = first_name[j];
}
for(i = 1 ; i <= 3 ; i++)
{
printf("What is the score of Exam#%d? \n",i);
scanf("%d", &exam_scores[i]);
}
for(int i = 1 ; i <= 3 ; i++)
{
sum = sum+exam_scores[i];
}
float average = sum / 3;
printf("hello %s, based on your exam score of %d, %d, and %d, your
average is %f with a letter of an
‘A.’",name,exam_scores[1],exam_scores[2],exam_scores[3],average);
return 0;
}
Screenshot :
Explanation :
In the above code first the user is prompted to enter his or her name followed by prompted to enter the marks 1 , marks 2, marks 3 . This is achieved by using the for loop for asking the user to enter the marks . Then using the for loop the marks are added and then divide by the constant 3 to calculate the average of the three marks . In the end the name and the average of the three marks are being displayed in the format mentioned in the question above .