Thread: Any help with this Program ..

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    9

    Unhappy Any help with this Program ..

    Hi there ..

    I wanna write a program that reads two numbers ( First & Last )
    & Print the numbers between them & the sum ...

    This is the code ..

    Code:
    #include <iostream>
    #include <conio>
    
    int main ()
    {
       int First , Last , sum = 0;
       cout<< "Enter the First number : ";
       cin>> First;
       cout<< "Enter the Last number : ";
       cin>> Last;
    
       cout<< "\nThe numbers between " << First << " and " << Last << " are :";
       cout<< "\n----------------------------------" << endl;
    
       while ( First <= Last )
       {
          cout<< First << endl;
          First++;
          sum+=First ;
       }
       while ( First >= Last )
       {
          cout<< Last << endl;
          Last++ ;
          sum+=Last;
       }
       cout<< "----------------------------------" << endl;
       cout<< "The sum = " << sum ;
       getch ();
       return 0 ;
    }

    There is no proplems when ( First >= Last ) ..
    But there is when ( First <= Last ) ..

    Also when I enter (-First & -Last ) or (+First & -Last) or ( -First & +Last)
    .. The sum is wrong ..

    & How can I solve the problem when ( First = Last)..

    Any Help Greatly Appreciated..

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Use an if statement perhaps?
    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.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    9
    ok I'll try ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM