Results 1 to 2 of 2

Thread: calculate the sum of all the even numbers between the range the user inputs

  1. #1
    Join Date
    Sep 2010
    Posts
    11

    calculate the sum of all the even numbers between the range the user inputs

    ***************** Calculate the sum of all the even
    numbers between the range the user inputs *************

    This is what I have so far... but something is wrong and I can't figure it out... I keep getting 0 (zero) when I run the program. Please Help!



    #include<stdio.h>

    int main()
    {

    int display, min, max;
    int sum=0;
    int counter1, counter2;

    printf("Enter minimum value: ");
    scanf("%d", &min);

    printf("Enter maximum value: ");
    scanf("%d", &max);

    counter1=min%2;
    counter2=min+1;

    if (counter1>0)
    {
    for (display=counter2; display<=max; display=display+2)
    sum=sum+display;
    }

    if (counter1=0)
    {
    for (display=min; display<=max; display=display+2)
    sum=sum+display;
    }

    printf("%d", sum);


    }

  2. #2
    Join Date
    May 2006
    Posts
    21

    Re: calculate the sum of all the even numbers between the range the user inputs

    Try using the code below:

    Code:
    #include<stdio.h>
    
    int main()
    {
    
    	int display = 0, min, max;
    	int sum=0;
    	int counter1, counter2;
    
    	printf("Enter minimum value: ");
    	scanf("%d", &min);
    
    	printf("Enter maximum value: ");
    	scanf("%d", &max);
    
    	if(min%2==0)
    	{
    		min = min;
    	}
    	else
    	{
    		min = min+1;
    	}
    
    	if (min>0 && max>1)
    	{
    		for (display=min; display<=max; display=display+2)
    		{
                            sum = sum+display;
    			
    		}
    	}
    	else
    	{
    		printf("Either counter1 is negative or counter2 is negative");
    	}
    printf("%d", sum);
    return 0;
    }

Similar Threads

  1. How to calculate told of numbers combination in Microsoft Excel list
    By Ankit Zh@very in forum MS Office Support
    Replies: 3
    Last Post: 24-01-2012, 02:23 PM
  2. How delete rows between two inputs numbers on Excel?
    By Hridayeshu in forum Windows Software
    Replies: 1
    Last Post: 04-01-2012, 07:56 PM
  3. display all the odd numbers between the range the user inputs
    By MaryJ in forum Software Development
    Replies: 1
    Last Post: 22-11-2010, 05:13 PM
  4. What is the shell program to calculate addition of two numbers?
    By Shophia_D in forum Software Development
    Replies: 3
    Last Post: 09-12-2009, 02:34 PM
  5. User inputs in Batch (.bat) files
    By Gap meer in forum Software Development
    Replies: 3
    Last Post: 21-05-2009, 12:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,534,623.31023 seconds with 17 queries