u

Wednesday, 6 June 2012

C++ Program will ask the user for a starting number and an ending number and compute the sum of the numbers in between

C++ Program will ask the user for a starting number and an ending number and compute the sum of the numbers in between


#include<iostream.h>
#include<conio.h>

void main()
{
   char Answer;

   do{

   int Number1, Number2;
   long Total = 0;
   clrscr();
   gotoxy(25,11);
   cout<<"Enter starting number: ";
   cin>>Number1;
   gotoxy(25,12);
   cout<<"Enter ending number: ";
   cin>>Number2;

   for(int i = Number1;i <= Number2; i++)
      {
     Total += i;
      }

   clrscr();
   gotoxy(25,11);
   cout<<"The sum of the integers from "<<Number1<<" to "
       <<Number2<<" is "<<Total<<".";
   gotoxy(25,12);
   cout<<"Repeat Y/N?: ";
   cin>>Answer;
   }while( Answer =='Y' || Answer == 'y');

   clrscr();
   gotoxy(25,11);
   cout<<"-press enter to quit-";
   getch();
}

0 comments:

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More