CS10L - Laboratory Exercise #6 - 2nd Term - 20122013

Problem Statement: The program will ask for a number. Then will display the odd numbers on that number, Then on the next line will show the even numbers from the number we have Input. Then Add up all even numbers... and multiply all the odd numbers.
 
CODE:

/**
    Laboratory Exercise #6
    CS10-B5
   
    @author Red Berroy
*/
#include <iostream.h>
 
int main()
 {
 int num,x,odd_sum,odd_product,even_sum,even_product;
 //Variable names
 even_sum=0;
 odd_sum=0;
 odd_product=1;
 cout<<"Input a number: ";
 cin>>num;
 //Enter a number
 
 cout<<"Odd Numbers: ";
 for (x=1;x<=num;x++)
 {
  if(x%2!=0)
  {
   cout<<x<<" ";
   odd_sum+=x;
   odd_product*=x;
  }
 }
 //Displays odd numbers
 cout<<"\nEven Numbers: ";
 for (x=1;x<=num;x++)
 {
  if(x%2==0)
  {
   cout<<x<<" ";
   even_sum+=x;
   even_product*=x;
  }
 }
 //Displays even numbers
 
 cout<<"\nSum of Even Numbers: "<<even_sum;
 cout<<"\nProduct of Odd Numbers: "<<odd_product<<endl;
 //Displays sum and product
 
 return 0;
}

0 comments:

Post a Comment