0% found this document useful (0 votes)
7 views6 pages

Solution TD - From Ex12 To Ex16

Uploaded by

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

Solution TD - From Ex12 To Ex16

Uploaded by

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

Solution Ex 12-Meth1 using loop while:

#include<iostream>
#include<cmath>
using namespace std;
int main(){
int n, i, s = 0;
do{
cout << "Enter an integer strictly positive: ";
cin >> n;
} while (n <= 0);

i=1;
while (i <= n){
s = s + pow(i, 2);
i = i + 1;
}
cout << "Sum= " << s << endl << endl;
system("pause");
return 0;
}

Solution Ex 12-Meth2 using loop For:

#include<iostream>
#include<cmath>
using namespace std;
int main(){
int n, i, s = 0;
do{
cout << "Enter an integer strictly positive: ";
cin >> n;
} while (n <= 0);

for(i=1; i<=n; i=i+1){


s = s + pow(i, 2);
}
cout << "Sum= " << s << endl << endl;
system("pause");
return 0;
}

1
Solution Ex 13-Meth1 using loop For:
#include<iostream>
#include<iomanip>
using namespace std;
int main(){
int n, i, Sodd = 0;
do{
cout << "Enter n (between 5 and 25): ";
cin >> n;
} while (n<5 || n>25);

cout << "Odd numbers from 1 to " << n << " are: " << endl;
for (i = 1; i <= n; i = i + 2){
cout << i <<setw(4);
Sodd = Sodd + i;
}
cout << "\nSum of odd integers from 1 to " << n << " is: " << Sodd << endl << endl;
system("pause");
return 0;
}

Solution Ex 13-Meth2 using loop While:


#include<iostream>
#include<iomanip>
using namespace std;
int main(){
int n, i, Sodd = 0;
do{
cout << "Enter n (between 5 and 25): ";
cin >> n;
} while (n<5 || n>25);
cout << "Odd numbers from 1 to " << n << " are: " << endl;

i=1
while(i <= n){
cout << i <<setw(4);
Sodd = Sodd + i;
i=i+2;
}
cout << "\nSum of odd integers from 1 to " << n << " is: " << Sodd << endl << endl;
system("pause");
return 0;
}

2
Solution Ex 13-Meth3 using %:

#include<iostream>
#include<iomanip>
using namespace std;

int main(){
int n, i, Sodd = 0;
do{
cout << "Enter n (between 5 and 25): ";
cin >> n;
} while (n<5 || n>25);

cout << "Odd numbers from 1 to " << n << " are: " << endl;
for (i = 1; i <= n; i = i + 1){
if(i%2==1){
cout << i <<setw(4);
Sodd = Sodd + i;
}
}
cout << "\nSum of odd integers from 1 to " << n << " is: " << Sodd << endl << endl;
system("pause");
return 0;
}

3
Solution Ex 14:
#include<iostream>
using namespace std;

int main(){
int i, a, b, s1 = 0, s2 = 0;
do{
cout << "Enter 2 positive integers: ";
cin >> a >> b;
} while (a <= 0 || b <= 0);

for (i = 1; i <= a; i = i + 1) {
if (a%i == 0)
s1 = s1 + i;
}
for (i = 1; i <= b; i = i + 1) {
if (b%i == 0)
s2 = s2 + i;
}
if (s1 == s2)
cout << a << " and " << b << " (friends)"<<endl<<endl;
else
cout << a << " and " << b << " (not friends)"<<endl<<endl;
system("pause");
return 0;
}

4
Solution Ex 15:

#include <iostream>
using std::cout; using std::cin;
using std::endl;
#include <iomanip>
using std::setw;

int main(){
int n, i, j, s=0;
cout<<"Enter a number: ";
cin>>n;
s=n;
for(i=1; i<=4; i=i+1){
for(j=1; j<=10; j=j+1){
cout<<setw(7)<<s;
s=s+n;
}
cout<<endl;
}
system("pause");
return 0;
}

5
Solution Ex 16:

#include<iostream>
using std::cin; using std::cout; using std::endl;

int main(){
int spaces=0, stars, i, k, j, n;
do{
cout<<"Introduce n between 3 and 10: ";
cin>>n;
}while(n<3 || n>10);
stars=n;
for(i=n; i>0; i--){
for(k=1; k<=stars; k++){
for(j=1; j<=spaces; j++)
cout<<" ";
for(j=1; j<=stars; j++)
cout<<"*";
cout<<endl;
}
spaces=spaces+i;
stars=stars-1;
}
system("pause");
return 0;
}

You might also like