15,918,404 members
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
Patrice T 9-Mar-18 5:18am
And you have a question?

## Solution 2

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`.

Maciej Los 9-Mar-18 5:55am
5ed!

## Solution 3

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;
}```

Maciej Los 9-Mar-18 11:12am
5ed!

## Solution 1

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.

v2

## Solution 4

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.

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

Top Experts
Last 24hrsThis month
 OriginalGriff 90 halovalo 30 CPallini 20 Pete O'Hanlon 20 Richard Deeming 10
 Pete O'Hanlon 720 OriginalGriff 581 Dave Kreskowiak 405 Richard MacCutchan 320 merano99 260

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