// Programs in C++
A)
Answer:
int i = 2;
int n = 100;
int sum = 0;
while (i <= n)
{
if (i % 2 == 0)
{
sum += i;
}
i++;
}
cout << sum;
Explanation:
First of all, run a while loop until i is less than or equal to 100. Then inside the while loop, check if i variable has an even value and then add it the sum variable and keep on increasing the i variable which is acting as a counter. At last, print the sum.
B)
Answer:
int i = 2;
int n = 100;
int square;
int sum = 0;
while (i <sqrt(n))
{
square = i*i;
sum += square;
i++;
}
cout << sum;
Explanation:
Run a while loop until i is less than or equal to square root of 100. Then inside the while loop, square the i variable, and then add it the sum variable and keep on increasing the i variable which is acting as a counter. At last, print the sum.
C)
Answer:
int i = 0;
int n = 20;
int square;
int sum = 0;
while (i <=n)
{
sum += pow(2, i);
i++;
}
cout << sum;
Explanation:
Run a while loop until i is less than or equal to 20. Then inside the while loop, take the power of 2 up to the i variable, and then add it the sum variable and keep on increasing the i variable which is acting as a counter. At last, print the sum.
D)
Answer:
int a;
int b;
int sum = 0;
cin >> a;
cin >> b;
while (a <= b)
{
if (a % 2 != 0)
{
sum += a;
}
a++;
}
cout << sum;
Explanation:
Take inputs from user in variables a and b. Run a while loop until a is less than or equal to b. Then inside the while loop, check if a is not an even number and then add it the sum variable and keep on increasing the a variable which is acting as a counter. At last, print the sum.
E)
Answer:
int number;
cout << "Enter a number: ";
cin >> number;
int odd_digits_sum = 0;
while (number > 0)
{
int digit = number % 10;
odd_digits_sum += digit;
number /= 100;
}
cout << "Sum of all digits at odd digits: " << odd_digits_sum;
Explanation:
Run a while loop until the number entered by user is greater than 0. Inside the while loop, calculate the value of digit by taking a mod of the number variable with 10.
After that sum those digits and then divide the number by 100 and assign this new value to number variable. Print the sum of digits stored in the sum variable at the end.
F)
Answer:
int number;
cout << "Enter a number: ";
cin >> number;
int length = to_string(number).length();
int sum = 0;
for (int i=1; i <= length; i++)
{
//Add odd place digit starting from left.
if (i % 2 != 0)
{
//Read left most digit
int digit = floor(number / pow(10, length - i));
sum += digit;
int l = pow(10, (length - i));
number = number % l;
}
//Remove even place digit
else
{
int digit = floor(number / pow(10, length - i));
int l = pow(10, (length - i));
number = number % l;
}
}
cout << "Sum: " << sum << endl;
Explanation:
Run a for loop up to the length variable. Add odd place digit starting from left by checking if i variable does not have an even value.
If the condition is true, read the left most digit and add it the sum variable. Otherwise remove even place digit. Print the sum of digits stored in the sum variable at the end.