Click here to Skip to main content
15,918,404 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Write a Program to enter name and roll number and marks of two subject and then find the total and percentage then print the name , roll number, total and percent.

What I have tried:

C
/* Program Of School */
#include <stdio.h>
void main ()
{
 char Name[50];
 Int Roll, Mark1, Mark2;
 float per;
Printf("enter the name and roll number\n");
scanf("%c %d" , &name, &Roll);
printf("enter the marks of two subject");
scanf("%d %d" , &mark1, &mark2);
sum=mark1+mark2:
per=(mark1+mark2)/2.0;
printf("the name is %c and roll mo is %d, &name, &roll number);
printf("total marks are %d and percentage are %f, &sum, &per);
getch();
}
Posted
Updated 9-Mar-18 4:43am
v2
Comments
Patrice T 9-Mar-18 5:18am    
And you have a question?

Four problems:

  1. With scanf() you have to pass pointers as arguments to get the values stored. But with printf() you have to pass integral values like int and float) by value.
  2. The "%c" format sequence is for a single character. Use "%s" for strings.
  3. With strings (char arrays) the object is already a pointer. So you have to pass the plain name or the address of the first element (Name or &Name[0]).
  4. Your percentage calculation is wrong. The general formula is 100 * part / total.
 
Share this answer
 
Comments
Maciej Los 9-Mar-18 5:55am    
5ed!
You prograsm is full of bugs. You have to be careful while writing a C program:
  • C is case-sensitive, if you name a variable Mark1 then every its occurrence must be written exactly Mark1 ('mark1' is not an option). Likewise there is a printf function, not a 'Printf'.
  • scanf takes the address of the arguments whereas printf takes the value of the arguments.
  • The statement are terminated by semicolon (;) not colon (':').
  • The format specifier of strings is %s not '%c'.
  • You missed the trailing double quote of the format specifier in the last two printf calls

For sure I missed to report something...
Please note you are computing the average value of the two marks, not a percent at all.

Try
C
#include <stdio.h>
int main ()
{
  char name[50];
  int roll, mark1, mark2;
  int sum;
  double avg;
  printf("enter the name and roll number\n");
  scanf("%s %d" , name, &roll);
  printf("enter the marks of two subject\n");
  scanf("%d %d" , &mark1, &mark2);
  sum = mark1 + mark2;
  avg = (mark1 + mark2)/2.0;
  printf("the name is %s and roll mo is %d\n", name, roll );
  printf("total marks are %d and the average is %g\n", sum, avg);
  getchar();
  return 0;
}
 
Share this answer
 
Comments
Maciej Los 9-Mar-18 11:12am    
5ed!
Won't work, unless your subjects only ever have single character names.
For scanf, "%c" means "read a single character" so if you type "mike" it will all fail.
Instead, use "%s" which reads a string, and prompt the user for separate lines worth of data: first prompt for the name, read the string, then prompt for the roll number and read that.
I'd also suggest that you do the same "prompt for one item, then read it" for the marks - it makes it easier for the user to follow.
 
Share this answer
 
v2
For solving your home work you should learn coding, so read the documentation about scanf and printf. You made some horrible error which some other answers already are pointing at.

If you hadnt installed Visual Studio now and learn how to use the debugger.

Tip: copy and debug some working sample code to understand C.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900