X If X If R: Oop Assignment No: 1
X If X If R: Oop Assignment No: 1
10 R 60
if ( x 12) if ( x 12)
2# Write a program that asks the user to type an integer and writes "YOU WIN" if the value is between 56 and 78 (both included). In the other case it writes "YOU LOSE". 3# Write a program that asks the user to type all the integers between 8 and 23 (both included) using a FOR loop. 4# Write a program that asks the user to type all the integers between 8 and 23 (both included) using a WHILE loop. 5# Write a program that asks the user to type 10 integers and writes the sum of these integers. 6# Write a program that asks the user to type an integer between 0 and 20 (both included) and writes N + 17. If someone types a wrong value, the program writes ERROR and he must type another value. 7# Write programs that ask the user to type strictly positive integer. When the user types a negative value the program write ERROR and ask for typing another value. When the user types 0, that means that the last value has been typed and the program must write the average of the strictly positive integers. If the number of typed values is zero the program writes 'NO AVERAGE'.
if ( x > 12 ) R = 10 ; else R = 60 ;
Solution #2
#include<iostream> using namespace std; int main() { int a; cout << "Type an integer : "; cin >> a; if ( (a >= 56) && (a <= 78) ) cout << "YOU WIN" << endl; else cout << "YOU LOSE" << endl; return 0; }
Solution # 3 #include <iostream> using namespace std; int main() { int a = 8 , number; cout << "Enter all the numbers from 8 - 23. \n"; for( ; a < 24 ; ) { cin >> number; if(number == a) { a++; cout << "Next number:\n"; } else cout << "?"; } return 0; }
#include <iostream> using namespace std; int main() { int typedInt = 0; cout << "Type all numbers between 8 and 23: " << endl; for(int i = 8; i <= 23; i++) { cin >> typedInt; if (typedInt != i) cout << "You Missed the sequence the next number was "<< i << endl; } return 0; } Solution # 4 Same exercise but you must use a while. #include <iostream> using namespace std; int main() { int a = 8, number; cout << "Enter numbers from 8 to 23\n"; while(a < 24) { cin >> number; if(a == number) { cout << "Next number: "; a++; } else cout << "?"; } return 0; }
/* A program to sum 10 integers using a loop */ #include <iostream> using namespace std; int main() { //Define variables int i = 1; //Counter int n = 0; //Sum int temp; //Input store cout << "This program sums ten user entered integers\n\n"; for (i = 1 ; i <= 10 ; i++) { cout << "Type integer " << i << ": "; cin >> temp; n = n + temp; } cout << "\n The sum of the integers is: " << n << endl; return 0; } #include<iostream> using namespace std; int main() { int i, s=0, x; for(i=0;i<10;i++) { cout<<"Type an integer: "; cin>>x; s=s+x; } cout<<"The sum is : "<< s << endl; return 0; }
#include<iostream> using namespace std; int main() { int N; bool ok; do { cout<<"Type the value of N between 0 et 20 :"; cin>>N; ok = N<=20 && N>=0; if(!ok) cout<< "ERROR"<<endl; }while(!ok); N=N+17; cout<<"The final value is : "<<N<<endl; return 0; }
Solution # 7
#include<iostream> using namespace std; int main() { int x, s=0,nb=0; double average; do{ cout<<"Type an integer:"; cin>>x; if(x>0){ s=s+x; nb++; } else if(x<0) cout<<"ERROR "; }while(x!=0); if(nb==0) cout<<"NO AVERAGE"<<endl; else { average=(double)s/nb; cout<<"The average is : "<<average<<endl; } return 0; }