Thread: converting function data to an array

  1. #1
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88

    converting function data to an array

    How can I change this function so it stores each of the even integers into an array of integers?


    Code:
    void sumIntegers ()
    {
    	
    	int num = 0;
    	int i = 0;
    	int sum = 0;
    
    	do
    	{	
    		printf("\nI want to sum up even integers.\n\n");
    			printf("\nPlease enter a positive integer:\n\n\t\t\t");
    				scanf(" %d", &num);
    					
    
    					if(num < 0)
    						printf("\n*** Negative numbers are invalid! ***n\n\n");
    					
    	
    							for(i = 0; i <= num; i += 1)//or loop that adds all evens to sum
    								{				
    								sum += i;
    								i++;
    								}
    								
    
    		printf(" \nThe even numbers up to %d sum to %d.\n\n\n", num, sum);
    		printf("\tThank you for playing!\n\n\n\n");
    	}
    	while(num < 0);
    
    	return;
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    If number % 2 = 0, then the number is even. Give it a shot
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 02-10-2012, 05:42 AM
  2. Converting Between Data Types
    By emodius in forum C Programming
    Replies: 3
    Last Post: 04-20-2009, 12:05 PM
  3. Converting data types
    By Kool4School in forum C++ Programming
    Replies: 6
    Last Post: 01-09-2009, 05:19 PM
  4. converting data types
    By jaXx in forum C Programming
    Replies: 10
    Last Post: 01-02-2005, 02:50 PM
  5. Function to read data from file to array
    By anatazi in forum C Programming
    Replies: 3
    Last Post: 07-01-2002, 11:08 PM