Help, Again.

This is a discussion on Help, Again. within the C Programming forums, part of the General Programming Boards category; Well, i need to write a program that gets 10 numbers from a user, and then prints their sum. using ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    18

    Help, Again.

    Well, i need to write a program that gets 10 numbers from a user, and then prints their sum.

    using loops.

    I've tried but it's only takes input 1 time.

    Code:
    #include <stdio.h>
    
    int main()
    
    {
    	int num,entered_num;
    
    	for(num=0;num<=10;num++)
    		printf("Please enter a number: \n");
    	scanf("%d", &entered_num);
    
    
    	printf("The sum of the numbers you have entered is: %d\n", entered_num);
    
    	getchar()
    
    }

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    Code:
    int num;
    int total = 0;
    int counter = 1;
    
    while ( counter <= 10 )
    {
       printf("Enter a number: ");
       scanf("%d", &num);
       counter++;
       total += num;
    }
    
    printf("\nTotal is: %d", total);
    Just do the same with your for loop
    I'm just trying to be a better person - My Name Is Earl

  3. #3
    Register User andor's Avatar
    Join Date
    Aug 2006
    Location
    Novi Sad
    Posts
    42
    And where are the brackets after the loop and the sum?

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    18
    Thanks!

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    65
    You may also want to read up on scanf(), its return value and "flushing the input buffer". There's zillions of articles in the FAQ about it and it's discussed about 5 times a week on this board.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21