0% found this document useful (0 votes)
18 views

Task 3

The document discusses a C++ program with 15 options for tasks involving basic math and logic problems. The program uses control structures like if/else statements and switch cases to evaluate user input and return corresponding output.

Uploaded by

fadymamdouh2515
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Task 3

The document discusses a C++ program with 15 options for tasks involving basic math and logic problems. The program uses control structures like if/else statements and switch cases to evaluate user input and return corresponding output.

Uploaded by

fadymamdouh2515
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

TASK_3

NAME: Yasser Farghal Abd-eltawwab


#include <iostream>
//the next library its for can use sqrt() function
#include <math.h>
using namespace std;

//task3
int pick, sr;

//next line its for function used in q12


bool perfect_s(int num){
//here compiler make follow lines to choose value(T or F) for this bool
//sqrt its function to give (return)value of square root of number
sr=sqrt(num);
//this if to check the value of square root of the number mul it self = your number
if (sr*sr==num) {
return true;
}//end of if
else{
return false;
}//end of else
}

int main() {
cout<<"please pick up number of task you want"<<endl;
cout<<endl;
cout<<"1.Check character vowel or consonant"<<endl;
cout<<"2.find maximum between two numbers"<<endl;
cout<<"3.Print day of week name"<<endl;
cout<<"4.enter two numbers& choose among deferent operations"<<endl;
cout<<"5.Print the following serial: {1, 3, 5, 7, 9,11, ... 99}"<<endl;
cout<<"6.Print the following serial: {1, 4, 9, 16, 25,36, ... 121}."<<endl;
cout<<"7.Print numbers divisible by 5, for the integers from 1 to 99"<<endl;
cout<<"8.enter (5) integers & get 1) The sum of the integers. 2) The average of the
integers"<<endl;
cout<<"9.Read a positive integer N, then calculate and print (N!)"<<endl;
cout<<"10.Read a positive integer N and determine whether N is a prime."<<endl;
cout<<"11.Print numbers divisible by k, for the integers from m to n."<<endl;
cout<<"12.Read a positive integer N and determine whether N is a perfect square"<<endl;
cout<<"13.inputs number then sum the number into its individual digits and prints their sum
"<<endl;
cout<<"14.Read an integer from 1 to 999 and print it in words"<<endl;
cout<<"15.A simple payroll program"<<endl;
cin>>pick;
if (pick==1) {
//1.Check character vowel or consonant using SWITCH case.
char leter;
cout<<"please enter a character to check its vowel or not"<<endl;
cin>>leter;
switch (leter) {
//lowercase_cases_check
case 'a':
case 'e':
case 'u':
case 'i':
case 'o':
//upercase_cases_check
case 'A':
case 'E':
case 'U':
case 'I':
case 'O':
cout<<"its vowel character"<<endl;
break;
default:
cout<<"it consonant character"<<endl;
break;
}
/*
another solution:
char leter;
cout<<"please enter a character to check its vowel or not"<<endl;
cin>>leter;
switch (leter) {
case 'A':
cout<<"its vowel character"<<endl;
break;
case 'a':
cout<<"its vowel character"<<endl;
break;
case 'E':
cout<<"its vowel character"<<endl;
break;
case 'e':
cout<<"its vowel character"<<endl;
break;
case 'U':
cout<<"its vowel character"<<endl;
break;
case 'u':
cout<<"its vowel character"<<endl;
break;
case 'I':
cout<<"its vowel character"<<endl;
break;
case 'i':
cout<<"its vowel character"<<endl;
break;
case 'O':
cout<<"its vowel character"<<endl;
break;
case 'o':
cout<<"its vowel character"<<endl;
break;
default:
cout<<"it consonant character"<<endl;
break;
}
*/
}//end of if
else if (pick==2){
/*Using SWITCH find maximum between two numbers (Note:use only two
variables e.g. number1, number2)*/
int num1,num2;
cout<<"please enter your 2 numbers to check the greater number "<<endl;
cout<<"please enter your first number "<<endl;
cin>>num1;
cout<<"please enter your secound number "<<endl;
cin>>num2;
//this switch has true or false possibility 1=true & 0=false because its has condaition
switch (num1>num2) {
case 1:
cout<<"the number "<<num1<<" its greater"<<endl;
break;
case 0:
cout<<"the number "<<num2<<" its greater number"<<endl;
break;
}
}//end of else if1
else if (pick==3){
/*Using SWITCH find maximum between two numbers (Note:use only two
variables e.g. number1, number2)*/
int day;
cout<<"please choose No.day of 7days start from saturday "<<endl;
cin>>day;
switch (day) {
case 1:
cout<<"the day is saturday"<<endl;
break;
case 2:
cout<<"the day is sunday"<<endl;
break;
case 3:
cout<<"the day is monday"<<endl;
break;
case 4:
cout<<"the day is Tuesday"<<endl;
break;
case 5:
cout<<"the day is Wednesday"<<endl;
break;
case 6:
cout<<"the day is hursday"<<endl;
break;
case 7:
cout<<"the day is Friday"<<endl;
break;
}
}//end of else if2
else if (pick==4){
/*Using SWITCH that tell user to enter two numbers then ask user to choose among
deferent operations*/
int num1,num2,sum;
char symbol;
cout<<"please enter your first number"<<endl;
cin>>num1;
cout<<"please enter your secound number"<<endl;
cin>>num2;
cout<<"please enter your operation symbol"<<endl;
cin>>symbol;
switch (symbol) {
case '+':
sum=num1+num2;
cout<<"the result its "<<sum<<endl;
break;
case '-':
sum=num1-num2;
cout<<"the result its "<<sum<<endl;
break;
case '*':
sum=num1*num2;
cout<<"the result its "<<sum<<endl;
break;
case '/':
sum=num1/num2;
cout<<"the result its "<<sum<<endl;
break;
case '%':
sum=num1%num2;
cout<<"the result its "<<sum<<endl;
break;
}
}//end of else if3
else if (pick==5){
/*Print the following serial: {1, 3, 5, 7, 9,11, ... 99}*/
int counter=1;
cout<<"{ ";
while(counter<=99){
cout<<counter<<". ";
counter+=2;
}
cout<<"}";
}//end of else if4
else if(pick==6){
/*Print the following serial: {1, 4, 9, 16, 25,36, ... 121}. */
int counter=1 , sum=0;
cout<<"{ ";
while(sum<121){
sum=counter*counter;
cout<<sum<<". ";
counter++;
}
cout<<"}";
}//end of else if5
else if(pick==7){
/*Print numbers divisible by 5, for the integers from 1 to 99. */
int num=1;
while(num<=99){
if (num%5==0) {
cout<<num<<" ";
}
num++;
}
cout<<endl;
}//end of else if6
else if (pick==8){
/*Ask the user for enter 5 integers. The program should then print:
a) The sum of the integers.
b) The average of the integers*/
int counter=1 , num;
float avg=0, sum=0;
cout<<"please enter your 5 numbers"<<endl;
for (;counter<=5; counter++) {
cin>>num;
sum+=num;
}
avg=sum/(counter-1);
cout<<"the result of sum the numbers its: "<<sum<<endl;
cout<<"the result of avrege the numbers its: "<<avg<<endl;
}//end of else if7
else if (pick==9){
/*9.Read a positive integer N, then calculate and print (N!).*/
int num, sum=1, counter=1;
cout<<"please enter your numbers and enter (=) to calc N!"<<endl;
cin>>num;
do{
sum*=counter;
counter++;
}while(counter<=num);
cout<<"the N! for your numbers is: "<<sum<<endl;
}//end of else if8
else if (pick==10){
/*10.Read a positive integer N and determine whether N is a prime*/
int counter, num;
bool is_prime = true;
cout << "Enter a positive integer: ";
cin >> num;
// 0 and 1 are not prime numbers
if (num == 0 || num == 1) {
is_prime = false;
}//end if

// loop to check if n is prime


for (counter = 2; counter <= num/2; counter++) {
if (num % counter == 0) {
is_prime = false;
break;
}//end inner if
}//end loop

if (is_prime){
cout << num << " is a prime number"<<endl;
}//end if
else{
cout << num << " is not a prime number"<<endl;
}//end else
}//end of else if9
else if (pick==11){
/*11.Print numbers divisible by k, for the integers from m to n. */
int num1, num2, Div;
cout<<"please enter your start number"<<endl;
cin>>num1;
cout<<"please enter your end number"<<endl;
cin>>num2;
cout<<"please enter the number for divisible numbers by it"<<endl;
cin>>Div;
while(num1<=num2){
if(num1%Div==0){
cout<<num1<<" its divisible by "<<Div<<endl;
}
num1++;
}
}//end of else if10
else if (pick==12){
/*12.Read a positive integer N and determine whether N is a perfect square */
int num;
bool perfect;
cout<<"please enter your positive number"<<endl;
cin>>num;
//here we equal value of bool perfect by return value from bool perfect_s
perfect=perfect_s(num);
if (perfect) {
cout<<"the number "<<num<<" its perfect square"<<endl;
}
else{
cout<<"the number "<<num<<" its not perfect square"<<endl;
}
}//end of else if11
else if (pick==13){
/*Write a program that inputs number, sum the number into its individual digits and prints
their sum*/
int num,sum=0,r_digit;
cout<<"please enter number: ";
cin>>num;
//the run condation of next loop its number be greater than 0
while(num>0){
//next line its to get the right digit of your number enterd
r_digit=num%10;
/*next line its to add the numbers will be in right digit of your number to sum to get result*/
sum=sum+r_digit;
//the next line its to update the number and delete the old right digit
num=num/10;
}//end_of_loop
cout<<"Sum is= "<<sum<<endl;
}//end of else if12
else if (pick==14){
/*Read an integer from 1 to 999 and print the integer in words.*/
int num, hundreds=0, tens=0, ones=0;
cout<<"please enter number"<<endl;
cin>>num;
if(num>=1&&num<=999){
hundreds=num/100;
tens=(num-(hundreds*100))/10;
ones=num-(hundreds*100)-(tens*10);
if (hundreds>0) { //start inner if
switch (hundreds) {//start inner switch
case 1:
cout<<"one hundreds ";
break;
case 2:
cout<<"two hundreds ";
break;
case 3:
cout<<"three hundreds ";
break;
case 4:
cout<<"four hundreds ";
break;
case 5:
cout<<"five hundreds ";
break;
case 6:
cout<<"six hundreds ";
break;
case 7:
cout<<"seven hundreds ";
break;
case 8:
cout<<"eight hundreds ";
break;
case 9:
cout<<"nine hundreds ";
break;
}//end of inner switch
if (tens&&ones == 0) {//start of inner inner if
cout<<endl;
}//end of inner inner if
}
if (tens!=1) {//start of inner if
switch (tens) {//start of inner switch
case 2:
cout<<"Twenty ";
break;
case 3:
cout<<"Thirty ";
break;
case 4:
cout<<"Forty ";
break;
case 5:
cout<<"Fifty ";
break;
case 6:
cout<<"Sixty ";
break;
case 7:
cout<<"Seventy ";
break;
case 8:
cout<<"Eighty ";
break;
case 9:
cout<<"Ninety ";
break;
}//end of inner switch
if (ones>0) {//start of inner if
switch (ones) {//start of inner switch
case 1:
cout<<"one"<<endl;
break;
case 2:
cout<<"two"<<endl;
break;
case 3:
cout<<"three"<<endl;
break;
case 4:
cout<<"four"<<endl;
break;
case 5:
cout<<"five"<<endl;
break;
case 6:
cout<<"six"<<endl;
break;
case 7:
cout<<"seven"<<endl;
break;
case 8:
cout<<"eight"<<endl;
break;
case 9:
cout<<"nine"<<endl;
break;
}//end of inner switch
}//start of inner if
else{//start of inner else
cout<<endl;
}
}
else if (ones>0) {//start of else if
switch (ones) {//start of inner switch
case 1:
cout<<"Eleven"<<endl;
break;
case 2:
cout<<"Twelve"<<endl;
break;
case 3:
cout<<"Thirten"<<endl;
break;
case 4:
cout<<"Fourteen"<<endl;
break;
case 5:
cout<<"Fifteen"<<endl;
break;
case 6:
cout<<"Sixteen"<<endl;
break;
case 7:
cout<<"Seventeen"<<endl;
break;
case 8:
cout<<"Eightteen"<<endl;
break;
case 9:
cout<<"Nineteen"<<endl;
break;
}//end of inner switch
}//end of else if
else{//start of else
cout<<"ten"<<endl;
}//end of else
}//end of outter if
}//end of else if13
else if (pick==15){
/*A simple payroll program, must read the name of an employee. If the name is not ‘END’,
it then must read the number of hours worked, and the rate of pay per hour. Then, it must
calculate the total pay, and print the name & total pay. This is repeated until the name read is
‘END’*/
string name;
int H_work, R_pay, sum=0;
cout<<"enter name or end to exist: "<<endl;
cin>>name;
while (name!="end") {
cout<<"please enter your hour worked No.: "<<endl;
cin>>H_work;
cout<<"please enter your rate pay per hour No.: "<<endl;
cin>>R_pay;
if (H_work&&R_pay != 0) {
sum=H_work*R_pay;
cout<<name<<"its total pay equal: "<<sum<<endl;
}
cout<<"enter name or end to exist: "<<endl;
cin>>name;
}
}//end of else if14
}
Q2_trace :
Its give error because height it’s not decalred
Q3_q3:
1-to exit from the loop or program
2-it can be worked but if the user insert end in
the first the do while loop will execute the code
at lest one time because the do while this its
mechanism

You might also like