Click here to Skip to main content
15,888,303 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include <stdio.h>
int main()
{

    int marks[5];
    printf("Enter the  value of marks1:\n");
    scanf("%d\n", &marks[0]);
    printf("Enter the  value of marks2:\n");
    scanf("%d\n", &marks[1]);
  printf("Enter the  value of  marks3:\n");
    scanf("%d\n", &marks[2]);
    printf("Enter the  value of marks4:\n ");
    scanf("%d\n", &marks[3]);
    printf("Enter the  value of marks5:\n");
    scanf("%d\n", &marks[4]);
    printf("The value of marks is %d,%d,%d,%d and %d", marks[0], marks[1], 
    marks[2], marks[3],marks[4]);
    return 0;
}


What I have tried:

Enter the  value of marks1:// I am getting to ask value of mark one twice 

25
25
Enter the  value of marks2:
26
Enter the  value of  marks3:
256
Enter the  value of marks4:
 54
Enter the  value of marks5:
54
The value of marks is 25,25,26,256 and 54
Posted
Updated 1-Jun-22 0:31am

1 solution

Remove the trailing newline from your scanf format strings thus:
C++
scanf("%d", &marks[1]);


You could also simplify your code to:
C++
int marks[5];
for (int i = 0; i < 5; ++i)
{
    printf("Enter the value for marks%d: ", i + 1);
    scanf("%d", &marks[i]);
}
printf("The value of marks is %d, %d, %d, %d and %d\n", marks[0], marks[1],
marks[2], marks[3],marks[4]);
 
Share this answer
 
v2

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

  Print Answers RSS


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