Week 7
Week 7
Ex 1:
#include <iostream>
using namespace std;
int main(){
int number, multiplication;
cout << "Enter number you want
multiplication: "; cin >> number;
int i = 1;
while (i <= 10){
multiplication = number * i;
cout << number << " x " << i << " =
"<< multiplication << endl;
i += 1;
}
return 0;
}
Ex 2:
#include <iostream>
using namespace std;
int main() {
int number = 12345, sum = 0, digit;
while (number > 0) {
digit = number % 10;
sum += digit;
number /= 10;
}
cout << "Sum of digits: " << sum <<
endl;
return 0;
}
Ex 3:
#include <iostream>
using namespace std;
int main(){
int row;
cout << "Enter the numbar of rows: "; cin
>> row;
int i = 1;
while (i <= row){
int j = 1;
while (j <= i){
cout << " * ";
j++;
}
cout << endl;
i++;
}
return 0;
}
Ex 4:
#include <iostream>
using namespace std;
int main(){
int i = 0, baseValue, powerValue,
result = 1;
cout << "Enter value of base: "; cin >>
baseValue;
cout << "Enter value of power: "; cin
>> powerValue;
while (i < powerValue){
result *= baseValue;
i++;
}
cout << "Result of " << baseValue << "
^ " << powerValue << " = " <<result;
return 0;
}
Ex 5:
#include <iostream>
using namespace std;
int main(){
int num, sum = 0;
cout << "Enter number: "; cin >> num;
do{
cout << "(The input ends if you
enter 0):";
cin >> num;
sum += num;
}while (num != 0 );
cout << "The sum is: " << sum;
return 0;
}