C++ tutorial, C++ programs, C++ Exercises, C++ Example Programs

Sunday, December 15, 2013

Program Sum of All Even Odd Numbers Between Two Numbers


Program sum of all, even, odd numbers between two numbers
/*
Write a C++ Program to input two numbers n1 and n2:

a) display all numbers and their sum between n1 and n2 inclusive
b) display all Even numbers and their sum between n1 and n2 inclusive
c) display all Odd numbers and their sum between n1 and n2 inclusive

For Free C++ Programming Techniques (Example Programs), visit
Www.EasyCppProgramming.Blogspot.Com

and For Good Notes
Visit   Www.ForFreeEducation.Blogspot.Com
*/
#include<iostream.h>

#include<conio.h>

void main()
{

  clrscr();

  int number1, number2, counter;
  long sumall = 0, sumeven=0, sumodd=0;

  cout<<"\n Enter the first number = ";

  cin>>number1;

  cout<<"\n Enter the second number = ";

  cin>>number2;


  cout<<"\n\n ----------- Results ------------";

  cout<<"\n\n All Numbers between \n Number1 and Number2 by For Loop: ";

  for (counter = number1; counter <= number2; counter++)
           {
           cout<<"\n \t"<<counter;
           sumall = sumall + counter;
           }
  cout<<"\n\n The Sum of All Numbers between \n Number1 and Number2 by For Loop ="<<sumall;


  cout<<"\n\n All Even Numbers between \n Number1 and Number2 by For Loop: ";

  if ( number1 % 2 == 0)
      counter = number1;
  else
  counter = number1 + 1;

  for (; counter <= number2; counter+=2)
           {
           cout<<"\n \t"<<counter;
           sumeven = sumeven + counter;
           }
  cout<<"\n\n The Sum of All Even Numbers between \n Number1 and Number2 by For Loop ="<<sumeven;


  cout<<"\n\n All Odd Numbers between \n Number1 and Number2 by For Loop: ";

  if ( number1 % 2 == 1)
      counter= number1;
  else
      counter = number1 + 1;

  for (; counter <= number2; counter+=2)
           {
           cout<<"\n \t"<<counter;
           sumodd = sumodd + counter;
           }
  cout<<"\n\n The Sum of All Odd Numbers between \n Number1 and Number2 by For Loop ="<<sumodd;






  getch();

}
Share:

0 comments:

Post a Comment

We Love To Hear From You!

EasyCPPprogramming.blogspotcom

Blog Archive

Labels