Thread: how to enter multiple numbers using a for & while loop

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    85

    how to enter multiple numbers using a for & while loop

    hi
    i am currently working on loops and i would like to know how to input multiple numbers into a for loop and a while loop and get the sum of them numbers for the output

    i just cant seem to get my head round this problem
    help much appreciated
    thanks

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The sum of some numbers doesn't require a loop, it just requires arithmetic, eg:
    Code:
    int sum = 2+3+4;
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    yes i know this but i want it to keep a running total and learn more about loops
    so as soon as the user enters a number it will qrun through the loops and add the inputted numbers until it has reached 10 numbers

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Something like:
    Code:
    #include <stdio.h>
    
    int main(int argc, const char *argv[]) {
    	int i, x, total = 0;
    
    	for (i = 0; i < 10; i++) {
    		printf("Enter a number: ");
    		scanf("%d",&x);
    		total += x;
    	}
    	printf("The total is %d\n",total);
    
    	return 0;
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    thanks im going to have to check this out im going to try the while loop also ill let you know if i get stuck
    thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  2. Newbie Help: Currency Converter
    By Ashfury in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 01:21 PM
  3. for loop to find prime numbers
    By 1rwhites in forum C Programming
    Replies: 11
    Last Post: 10-21-2005, 10:37 AM
  4. Loop until enter correct value
    By jchanwh in forum C Programming
    Replies: 2
    Last Post: 11-27-2001, 01:23 AM
  5. Totaling up numbers in a for loop?
    By musayume in forum C Programming
    Replies: 3
    Last Post: 11-21-2001, 04:23 PM