Assignments solution

advertisement
Assignment 1:
● Write a complete C++ program that asks the user to enter two numbers and prints
the sum, difference, product, quotient and remainder of the two numbers.
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int num1,num2;
cout<<"Enter two numbers"<<endl;
cin>>num1>>num2;
cout<<"The sum is "<<num1+num2<<endl;
cout<<"The difference is "<<num1-num2<<endl;
cout<<"The product is "<<num1*num2<<endl;
cout<<"The quotient is "<<num1/num2<<endl;
cout<<"The modulus is "<<num1%num2<<endl;
return 0;
}
Assignment 2:
● Write a complete C++ program that asks the user to enter two numbers and enter
an operator (+ , - ,* , / ) then calculate the result depending on the entered operator.
(You should use switch).
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int num1,num2;
char op;
cout<<"Enter two numbers: "<<endl;
cin>>num1>>num2;
cout<<"Enter an operator: "<<endl;
cin>>op;
switch(op){
case '+':
cout<<"The result is "<<num1+num2<<endl;
break;
case '-':
cout<<"The result is "<<num1-num2<<endl;
break;
case '*':
cout<<"The result is "<<num1*num2<<endl;
break;
case '/':
cout<<"The result is "<<num1/num2<<endl;
break;
}
return 0;
}
Assignment 3:
1. Write a C++ program using if else condition statement to find out whether the number is
even or odd.
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int num;
cout<<"Enter the number: "<<endl;
cin>>num;
if(num%2==0)
cout<<"It is even "<<endl;
else
cout<<"It is odd "<<endl;
return 0;
}
2. Write C++ program that asks the user how many numbers will be entered and after the user
enter those numbers, calculate and print the number of even and odd numbers.
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int control,number,evenCount=0,oddCount=0;
cout<<"How many numbers? "<<endl;
cin>>control;
for(int i=0; i<control;i++)
{
cout<<"Enter a number "<<endl;
cin>>number;
if(number%2==0)
evenCount++;
else
oddCount++;
}
cout<<"Number of odd numbers is: "<<oddCount<<endl;
cout<<"Number of even numbers is: "<<evenCount<<endl;
return 0;
}
Download