c++ s Ahmed Ali int main(int argc, char *argv[]) int age; age=10; cout<<" You are "<<age<<" years old.\n"; cout<<" You are too young to play the game.\n"; system("pause"); //2 int main(int argc, char *argv[]) cout<<"*****\n"; cout<<"*****\n"; cout<<"*****\n"; cout<<"*****\n"; cout<<"*****\n"; system("pause"); //3 Exercise 3: Write a C++ program to declare two integer, one float variables and assign 10, 15, and 12.6 to them respectively. It then prints these values on the screen. : int main(int argc, char *argv[]) int x; int y; float z; x=10; y=15; z=12.6; cout<<"x="<<x<<"\t"<<"y="<<y<<"\t"<<"z="<<z; cout<<"\n"; system("pause"); //4 Exercise 4: Write a C++ program to prompt the user to input her/his name and print this name on the screen, as shown below. The text from keyboard can be read by using cin>> and to display the text on the screen you can use cout<<. Hello Sok!.
c++ s Ahmed Ali : int main(int argc, char *argv[]) char name[20]; cout<<"enter your name:"; cin>>name; cout<<"hello "<<name<<"! \n"; system("pause"); //5 Exercise 5: Write a C++ program to prompt the user to input 3 integer values and print these values in forward and reversed order, as shown below. Please enter your 3 numbers: 12 45 78 Your numbers forward: 12 45 78 Your numbers reversed: 78 45 12 : int main(int argc, char *argv[]) int val1; int val2; int val3; cout<<"please enter your 3 numbers:"; cin>>val1>>val2>>val3; cout<<"\nyour numbers forward:\n"; cout<<val1<<"\n"<<val2<<"\n"<<val3<<"\n"; cout<<"your numbers reversed:\n"; cout<<val3<<"\n"<<val2<<"\n"<<val1<<"\n"; system("pause"); //sheet 2
c++ s Ahmed Ali EXERCISE 1 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". int a; cout << "Type an integer : "; cin >> a; if ( (a >= 56) && (a <= 78) ) cout << "YOU WIN" << endl; else cout << "YOU LOSE" << endl; EXERCISE 2 Write a program that asks the user to type all the integers between 8 and 23 (both included) using a for loop. A solution that uses input from users could be int a = 8; int 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 << "?"; int typedint = 0; cout << "Type all numbers between 8 and 23: " << endl; for(int i = 8; i <= 23; i++)
c++ s Ahmed Ali cin >> typedint; if (typedint!= i) cout << "You Missed the sequence the next number was " << i << endl; EXERCISE 3 Same exercise but you must use a while. A solution that uses input from the user could be 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 << "?"; EXERCISE 4 Write a program that asks the user to type 10 integers and writes the sum of these integers. /* A program to sum 10 integers using a loop*/ //Define variables int i = 1; int n = 0; int temp; //Counter //Sum //Input store cout << "This program sums ten user entered integers\n\n"; for (i = 1 ; i <= 10 ; i++) cout << "Type integer " << i << ": "; cin >> temp;
c++ s Ahmed Ali n = n + temp; cout << "\nthe sum of the integers is: " << n << endl; 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; EXERCISE 5 Write a program that asks the user to type 10 integers and writes the smallest value. int value = 0, min = 0; cout<<"enter 10 values and I will tell you which one is the smallest"<<endl; cout<<"enter number: "<<endl; cin >> value; min = value; for(int i =0; i < 9; i++) cout<<"enter number: "<<endl; cin>>value; if(value < min) min = value; cout<<"smallest value is: "<<min<<endl;
c++ s Ahmed Ali EXERCISE 6 Write a program that asks the user to type an integer N and computes the sum of the cubes from 53 to N3. SOLUTION: //Calling the appropriate libraries //Declare variables. Iniatial value for p is 125 (53) int i, n, p=125; //Ask the user to enter a number then save to variable n cout<<"enter a number: \n"; cin>>n; //This will compute the sum of the cubes if the value of variable n is greater than 5 for(i=n;i>5;i--)p+=i*i*i; //This will compute the sum of the cubes if the value of variable n is less than 5 for(i=n;i<5;i++)p+=i*i*i; //Displays the sum of the cube cout<<"the sum of cube of 5 up to cube of "<<n<<" is "<<p; system("pause"); EXERCISE 7 Write a program that asks the user to type an integer N and compute u(n) defined with : u(0)=3 u(n+1)=3*u(n)+4 : int n; int func = 0;
c++ s Ahmed Ali cout<<"enter a value: "<<endl; cin>>n; for(int i = 0; i < n; i++) if(i == 0) func += 3; func = 3*func +4; cout<<"the result of the computed function is: "<< func <<endl; EXERCISE 8 Write a program that asks the user to type an integer N and compute u(n) defined with : u(0)=1 u(1)=1 u(n+1)=u(n)+u(n-1) #include <cmath> int u(int n) if (n<=1) return n; else return u(n-1)+u(n-2); int a; cout<<"enter an integer: "; cin>>a; cout<<"u("<<a<<")="<<u(a); EXERCISE 9 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. 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;
c++ s Ahmed Ali while(!ok); N=N+17; cout<<"the final value is : "<<N<<endl; EXERCISE 10 Write a program that is able to compute some operations on an integer. The program writes the value of the integer and writes the following menu : 1. Add 1 2. Multiply by 2 3. Subtract 4 4. Quit The programs ask the user to type a value between 1 and 4. If the user types a value from 1 to 3 the operation is computed, the integer is written and the menu is displayed again. If the user types 4, the program quits. int n, op; bool quit = false; while(!quit && (cin >> n) ) //get input if 'quit' is false cout << "What do you want to do with " << n << '?' << endl << "1. Add 1" << endl << "2. Multiply by 2" << endl << "3. Subtract 4" << endl << "4. Quit" << endl; cin >> op; switch(op) case 1: cout << n + 1 << endl; break; case 2: cout << n * 2 << endl; break; case 3: cout << n - 4 << endl; break; case 4: quit = true; break; default: cout << "Unknown operation!" << endl; break;
c++ s Ahmed Ali EXERCISE 11 Write a program that asks the user to type a positive integer. When the user types a negative value the program writes ERROR and asks for 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 positive integers. If the number of typed values is zero the program writes 'NO AVERAGE'. int x, sum = 0,max = 0; double average; do cout<<"type an integer:"; cin>>x; if (x > 0 ) sum += x; max++; else if(x<0) cout << "ERROR "; while(x!=0); if(max == 0) cout << "NO AVERAGE"<<endl; else average = sum / max; cout<<"the average is : "<<average<<endl; system("pause"); EXERCISE 12 Write a program that asks the user to type an integer N and compute u(n) defined with :
c++ s Ahmed Ali u(0)=3 u(1)=2 u(n)=n*u(n-1)+(n+1)*u(n-2)+n int f(int x) if(x==0 x==1) if(x == 0) return 3; if(x == 1) return 2; else return x*f(x-1)+(x+1)*f(x-2)+x; int n; cout << "Enter an integer: " << endl; cin >> n; cout << endl << "Result: " << f(n); EXERCISE 13 Write a program that asks the user to type 10 integers and write the number of occurrence of the biggest value. void main() int number, biggest(0), occur(0); for (int i = 1; i < 11 ; i++) cout << "Input " << i << ": "; cin >>number; if (number > biggest) biggest = number; occur = 0; if (biggest == number) occur++; cout << "Biggest number inputted was: " << biggest << " \noccurance: " << occur << endl; system("pause"); EXERCISE 14 Write a program that asks the user to type the value of N and computes N!.
c++ s Ahmed Ali int num; cout << "Enter a number: "; cin >> num; long fact = 1; for (int i = 1; i <= num; i++) fact *= i; cout <<"!"<< num<< "= "<< fact; EXERCISE 15 Write a program that asks the user to type an integer N and that indicates if N is a number or not. void main() int num, i; bool prime = true; cout << "Enter a number: "; cin >> num; for (i = num-1; i > 1 && prime == true; i--) if (num%i == 0) prime = false; if (prime == true) cout << "Prime number"; else cout << "Not prime number"; system("pause"); EXERCISE 16 Write a program that asks the user to type an integer N and that writes the number of prime numbers lesser or equal to N. bool is_prime(int n)
c++ s Ahmed Ali if (n<2) return false; for (int i = 2; i < n; i++) if ((n % i) == 0) return false; return true; int temp = 0; cout << "Enter an integer: "; cin >> temp; for (int i = 2; i <= temp; i++) if (is_prime(i)) cout << i << endl; EXERCISE 17 Write a program that asks the user to type the value of an integer N and compute the N-st prime number. bool is_prime(int n) if (n<2) return false; for (int i = 2; i < n; i++) if ((n % i) == 0) return false;
c++ s Ahmed Ali return true; int i=0; int num = 0; int myval = 0; cout << "Enter an integer: "; cin>>num; if (num==0) cout<<"wrong degree"; int nst=0; while (nst<num) if (is_prime(i)) nst++; myval=i; i++; cout<<"the "<<num<<"-st prime number is "<<myval<<endl; EXERCISE 18 Write a program that asks the user to type the value of N and writes this picture : N=1 * N=2 ** * N=3 *** ** * int i,j,n; cout<<"type the value of N : "; cin>>n; for(i=1;i<=n;i++) for(j=1;j<=n+1-i;j++) cout<<"*"; cout<<endl;
c++ s Ahmed Ali EXERCISE 19 Write a program that asks the user to type the value of N and display an output like the following: N=1 * N=2 ** * N=3 *** ** * and so on... int i,j,n; cout<<"type the value of N : ";cin>>n; for(i=1; i<=n; i++) for (j=1; j<i; j++) cout<<" "; for (j=1; j<=n+1-i; j++) cout<<"*"; cout << endl; EXERCISE 20 u(n) is defined with: u(0) = a (a is an integer) if u(n) is even then u(n+1)=u(n)/2, else u(n+1)=3*u(n)+1 Conjecture: For all value of a, there exists a value N such that u(n)=1. a)write a program that asks the user to type the value of an integer a and writes all the values of u(n) from n=1 to n=n.
c++ s Ahmed Ali int a,n,u; cout<<"type the value of a : ";cin>>a; n=0; u=a; while(u!=1) if(u%2==0)u=u/2; else u=3*u+1; n++; cout<<"u("<<n<<")="<<u<<endl; b) Write a program that asks the user to type a value M and computes the value of a from 2 and M that maximizes the value of N. This value is called A. The program must write the value of A and N. int a,n,u,m,amax,nmax; cout<<"type the value of M : ";cin>>m; amax=2; nmax=2; for(a=3;a<=m;a++) n=0; u=a; while(u!=1) if(u%2==0)u=u/2; else u=3*u+1; n++; if(n>nmax)amax=a;nmax=n; cout<<"the value of A is :"<<amax<<endl; cout<<"the value of N is :"<<nmax<<endl; EXERCISE 21 Request the user to type numbers, each time printing its triple, until the user enters -999. for (int input; input!= -999;) cout << "Enter a number (-999 to quit): ";
c++ s Ahmed Ali cin >> input; if (input!= -999) cout << "Tripled: " << input * 3 << endl; return(0); EXERCISE 22 Request the user to type positive numbers until either a zero or a negative is typed, and then show the user how many positives were typed in. int i,k=0,nr; for(i=1; i<=10000; i++) cin>>nr; if(nr>0) k=k+1; else break; cout<<k; EXERCISE 23 Request the user to type positive numbers, or to stop by typing a number smaller than 1. Print the average. int n, sum = 0, nums = 0; do cout << "Enter a number (less than 1 = quit): "; cin >> n; if( n > 0 ) sum += n; ++nums; while( n > 0 ); if( nums > 0 )
c++ s Ahmed Ali else cout << "The average is: " << sum / nums; cout << "No average"; cout << endl; EXERCISE 24 Request the user to type numbers, or type 0 to stop. Show how many numbers were between 100 and 200 (both included). int input, numberbetween(0); const short int minnum(100), maxnum(200); do cout << "Enter a number (0 to quit): "; cin >> input; if (input >= minnum && input <= maxnum) numberbetween++; while (input!= 0); cout << "Numbers between " << minnum << " and " << maxnum << ": " << numberbetween; return(0); EXERCISE 25 The country A has 50M inhabitants, and its population grows 3% per year. The country B, 70M and grows 2% per year. Tell in how many years A will surpass B. class City public: int inhabitants,gpy; //growth per year City(int a,int b) inhabitants=a; gpy=b; ; int main () City A(50,3),B(70,2); int i=0; begin: A.inhabitants+=A.gpy*A.inhabitants/100; B.inhabitants+=B.gpy*B.inhabitants/100; i++; if (A.inhabitants>B.inhabitants)
c++ s Ahmed Ali std::cout<<"in "<<i<<"year(s), A will surpass B\n"; else goto begin; EXERCISE 26 Write a program that asks the user to input 5 sequences of characters. Then it will ask the user for a character to search for and will output the maximum number of times that it occurred between the 5 sequences. Example: Sequence 1: aabc Sequence 2: aaaa If the user chooses to search for character 'a', the program will output "Character a occurred a maximum of 4 times" because it occurred 4 times in the second sequence, and only twice in the first sequence. #include <string> #include <vector> int a = 1; string stemp; vector<string> shold; cout << "Enter any sequence of characters. Enter up to 5 sequences.\n\n"; do cout << "Enter sequence " << a << ": "; cin >> stemp; shold.push_back(stemp); a++; while(a<6); char cseek; cout << "Enter a letter to look for and count its max occurrence: "; cin >> cseek; vector<int> vicount; for(int i=0; i<shold.size(); i++) //Counting the occurrences of cseek int icount = 0; for(int k=0; k<shold[i].size(); k++) if(shold[i][k] == cseek) icount++; vicount.push_back(icount); //The maximum occurrence of cseek for each sequence will be stored in vicount, each value referred to as a local maximum int imax = vicount[0]; for(int i=0; i<vicount.size(); i++) if(vicount[i] > imax) imax = vicount[i]; //Finding the max of the local maximums
c++ s Ahmed Ali cout << "The character " << cseek << " occurred a maximum of " << imax << " times.\n\n"; EXERCISE 27 Write a program that asks the user to type a random number.write in output if this number is a prime number or not. Write,by which numbers can your number be divided. int x; int count = -1; cout << "Write a number:\n"; cin >> x; getchar(); for(int i=1;i<x+1;i++) if(x%i == 0) count+=1; cout << "Number " << x << " can be divided by " << i << endl; if(count == 1) cout << "This number is prime number." << endl; else cout << "This number is not a prime number." << endl; getchar();