PartA. WriteaprogramWhileLoopsthatuseswhileloopstoperformthefollowingsteps: Prom
ID: 3681458 • Letter: P
Question
PartA. WriteaprogramWhileLoopsthatuseswhileloopstoperformthefollowingsteps:
Prompt the user to input two positive integers: firstNum and secondNum (firstNum must be smaller than secondNum).
Output all the even numbers between firstNum and secondNum inclusive.
Output the sum of all the even numbers between firstNum and secondNum inclusive.
Output all the odd numbers between firstNum and secondNum inclusive.
Output the sum of all the odd numbers between firstNum and secondNum inclusive.
Part B. Write a program Stars that uses two nested for loops to output the pattern of stars shown below (note that there are no spaces in between stars).
Explanation / Answer
PART A
#include<iostream>
using namespace std;
int main()
{
int firstNum,secondNum,sum=0,temp;//Declaring variable for first number ,second number and sum
cout<<"Enter the First Number ";
cin>>firstNum;//Taking First number as input
cout<<" Enter the Second Number ";
cin>>secondNum;//Taking Second number as input
temp=firstNum;
if(temp%2) //Checking If first number is odd then add 1
{
temp++;
}
while(temp<=secondNum)
{
cout<<temp<<" "; //Printing all the even number
sum+=temp;
temp+=2;
}
cout<<" Sum of Even Numbers= "<<sum<<endl; //Printing Sum of even Number
if(firstNum%2==0) //Checking if the first number is even then make it odd by adding 1
{
firstNum++;
}
sum=0;
while(firstNum<=secondNum)
{
cout<<firstNum<<" "; //Printing odd numbers between first and Second Number
sum+=firstNum;
firstNum+=2;
}
cout<<" Sum of Odd Numbers= "<<sum<<endl; // Printing the sum of odd Numbers
return 0;
}
PART B
#include<iostream>
using namespace std;
int main()
{
int val,temp;
cout<<"Enter an integer between 3 and 10 ";
cin>>val;
temp=val;
while(temp>0)
{
val=temp;
while(val>0)
{
cout<<"*";
val--;
}
cout<<endl;
temp--;
}
return 0;
}