Syed Sheraz Ali Shah
01-161142-106
Assignment 3
Q1) Write a program of if else?
Ans:
#include
#include
using namespace std;
int main()
{
int num; cout << "Please enter integer:- "; cin >> num;
if ( num > 0) { cout << "You have entered a +ve integer:- " << num << endl; } else if (num<< "You have entered a -ve integer:- " << num << endl; } else { cout<<
#include
using namespace std;
int main()
{
int num, i = 1, factorial = 1;
cout << "You are in factorial program of C++. Please enter a +ve integer:- "; cin >> num; while ( i <<< num <<< factorial; getch(); return 0;
}
Q3) Write a program of do-while program C++?
Ans:
#include
#include
using namespace std;
int main()
{
float num, sum = 0.0; do { cout<>num; sum += num; } while(num != 0.0);
cout<<
#include
using namespace std;
int main()
{
char o; float num1, num2;
cout << "Select the operator you want to work (+ for addition, - for subtraction , * for multiplication , / for division):- "; cin >> o;
cout << "Enter any two numbers:- "; cin >> num1 >> num2; switch (o) { case '+': cout << num1 << " + " << num2 << " = " << num1+num2; break; case '-': cout << num1 << " - " << num2 << " = " << num1-num2; break; case '*': cout << num1 << " * " << num2 << " = " << num1*num2; break; case '/': cout << num1 << " / " << num2 << " = " << num1/num2; break; default: cout << "Invalid selection! Please correct operator thankyou"; break; } getch(); return 0;
}
Q5) Write a program of For in C++?
Ans:
#include
#include
using namespace std;
int main()
{
int n, factorial = 1;
cout << "Please enter +ve integer: "; cin >> n;
for (int i = 1; i << "Factorial of