Need help to print even numbers

This is a discussion on Need help to print even numbers within the C Programming forums, part of the General Programming Boards category; Hi iam new to c programming i have this program that suppose to: A program that will ask for two ...

  1. #1
    Registered User ortegac's Avatar
    Join Date
    Mar 2006
    Location
    In front of the computer screen
    Posts
    15

    Need help to print even numbers

    Hi iam new to c programming i have this program that suppose to: A program that will ask for two number (minimum and maximum) and print at all the even numbers in between

    but iam stuck i want to combine the min and max so i can print all the even numbers in between.

    any help will be apprecited, thanks

    Code:
    // A program that will ask for two number (minimum and maximum) and print at all the even numbers in between
    
    #include<stdio.h>
    
    int main(void)
    {
    	int min, max, i;
    	printf("Please enter a minimum number: ");
    	scanf("%d", &min);
    
    	printf("Please enter a maximum number: ");
    	scanf("%d", &max);
    
    	i = min +1; // iam incrementing i
    
    	while ( i % 2); // to get even numbers
    	printf("%d", i); // result
    
    	
    
    return (0);
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    > while ( i % 2);
    Try an if statement

    Then put the if and the print inside a for loop
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User ortegac's Avatar
    Join Date
    Mar 2006
    Location
    In front of the computer screen
    Posts
    15

    Here is what got is not printing the even numbes in between

    Something is wrong isnot doing what i want ? someone help me.


    Code:
    // A program that will ask for two number (minimum and maximum) and print at all the even numbers in between
    #include<stdio.h>
    
    int main()
    {
    	int min, max, i;
    	printf("Please enter a minimum number: ");
    	scanf("%d", &min);
    
    	printf("Please enter a maximum number: ");
    	scanf("%d", &max);
    
    
    
    	for (i>min; i<max; i++)
    		
    	while( i % 2)
    	
        i = min +1;
    	printf("%d", i);
    
    
    
    return 0;
    }

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,689
    The first part of a for loop is initialization which happens when the for loop starts. Instead of initializing i you compared it to max. This obviously doesn't work.

    Make while an if statement as suggested before and increment i last.
    Last edited by whiteflags; 05-21-2006 at 12:03 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 07:26 PM
  2. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 08:45 PM
  3. Print numbers by inorder..
    By NightWalker in forum C Programming
    Replies: 5
    Last Post: 09-15-2003, 10:24 AM
  4. What kind of programs should I start writing?
    By Macabre in forum C++ Programming
    Replies: 23
    Last Post: 04-12-2003, 08:13 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 05:38 PM

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