Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
[3.75 marks]
4)int i=5;
do {int j=0;
do {j++;
(i%2) ? cout<<'<':cout<<'>';
} while(j!=i);
cout<<endl;
i--; [3.75 marks]
} while(i>0);
Q2) a) Rewrite the following program without using the command for( ).
for(int i=1;i<100;i++){ int i=1;
while ( i<100)
x=i*i;
{ x=i*i;
cout<<i<<^2<<x<<endl;} cout<<i<<"^2"<<x<<endl;
i++;} [3.75 marks]
1 of 7 Pages
b) Rewrite the following program without using the commands if-else.
int h;
int h; cin>>h;
cin>>h; (h%2)?cout<<"even"<<endl:cout<<"odd"<<endl;
[3.75 marks]
if(h%2) cout<<even<<endl;
else cout<<odd<<endl;
c) Rewrite the following program without using the command isdigit( ).
char str[80]; char str[80];
cin.getline(str,80); cin.getline(str,80);
for(int i=0;i<strlen(str);i++){
for(int i=0;i<strlen(str);i++){
if( '0'<=str[i]<='9') str[i]++;}
if(isdigit(str[i])) str[i]++;}
cout<<str;
cout<<str; [3.75 marks]
2 of 7 Pages
Q3) a) for the following program:
int num, i;
bool P = true;
cout << "Enter a positive integer: ";
cin >> num;
for(i = 2; i <= num / 2; ++i){
if(num % i == 0){
P = false;
break;
}
}
if (P) cout << "This is a **** number";
else cout << "This is not a **** number";
what does the screen display if the user inputs:
3 of 7 Pages
_________________________________________________________________________
_______________________________[2 marks]
c) for the following program:
1) abcabcabcabc ______3______________________________________
2) ahmedmohamed_____1_____________________________________
3) noyes_____________0_____________________________________
[1 mark each]
In your words, what does this program do?
it counts the number of duplication of string A in string B element
[2 marks]
Q4) a) write a program that takes a string then finds the number of number digits, letters and spaces in that
string. as shown in the example.
Example:
4 of 7 Pages
#include <iostream>
#include <ctype.h>
void main()
{ char A [80];
cout<<"please type a sentence\n";
cin.getline(A,80);
int r=0, x=0, y=0,i=0;
while (1) {
if(isdigit(A[i]))
r++;
else if (isalpha(A[i]))
x++;
else if (isspace(A[i]))
y++;
i++;
if (A[i]=='\0')
break;
}
cout<<"the number of digits is : "<< r;
cout<<"\nthe number of letters is : "<< x;
cout<<"\nthe number of spaces is : "<< y;
cin.get();
cin.get();
}
[7.5 marks]
b) write a program that takes an integer then finds the length and the reverse of that integer. as shown in the
example.
Example:
5 of 7 Pages
#include <iostream>
void main()
{ int r=0, x=0,z;
cout<<"the number is";
cin>>z;
r=z;
while (r!=0) {r/=10;
x++;
}
cout<<"the lenght is : "<< x;
cout<<"\nthe reverse is : " ;
while (z!=0){cout<<(z%10);
z/=10;
}
cin.get();
}
Other easy solution ,but will not accepted cause it gives a different
output format.
#include <iostream>
void main()
{ int r=0, x=0;
cout<<"the number is";
cin>>r;
cout<<"\nthe reverse is : " ;
while (1) {cout<<(r%10);
r/=10;
x++;
if(r==0)
break;
}
cout<<"\nthe lenght is : "<< x;
6 of 7 Pages
cin.get();
}
_________________________________________________________________________
___________________________[7.5 marks]
7 of 7 Pages