Week 4 & 5 Programming Exercises
Note: Try to solve these Exercises on your own before checking the solutions.
Exercise 1: Divisors
Write a CC++ program that reads in a positive integer and prints all the divisors of the integer in decreasing order.
- Input: A single positive integer.
- Output: All divisors separated by spaces.
Example:
Enter a positive integer: 12
Divisors of 12 in decreasing order: 12 6 4 3 2 1
Exercise 2: Power Calculation
Write a CC++ program that reads in two positive integers base and exp and prints the result of:
In main(), you need to input the values for base and exp, calculate the power using a loop, and print the result.
Example:
Enter base and exponent: 2 5
2^5 = 32
Exercise 3: Factorial
Write a CC++ program that reads in a positive integer n and prints the result of n! (Factorial), where:
In main(), check if the number is negative (factorial doesn’t exist), otherwise calculate and print the factorial.
Example:
Enter a positive integer: 5
5! = 120
Exercise 4: Prime Check
Write a CC++ program that reads in an integer and checks whether the given number is Prime or not.
A Prime Number is a natural number greater than 1 that has no positive divisors other than 1 and itself.
In main(), read the number and use a flag or counter to determine if it has any divisors other than 1 and itself.
Example 1:
Enter an integer: 7
7 is a Prime number.
Example 2:
Enter an integer: 10
10 is not a Prime number.
Exercise 5: Reverse Number
Write a CC++ program that reads a positive integer number and prints the number with its digits reversed.
Example:
Enter a number: 5892
Reversed number: 2985
Note: Your program should work for any number of digits.
Exercise 6: Sum of Digits
Write a program that reads an integer number and prints the sum of its digits.
Example:
Enter an integer: 467
Sum of digits: 17
(Calculation: \(4 + 6 + 7 = 17\))
Exercise 7: Range Printer
Write a function printRange(int m, int n) that prints all numbers from m to n.
- The function should print an error message if \(m > n\).
- Challenge: Try implementing this using
for,while, anddo-whileloops.
In main(), input two integers from the keyboard and pass them to your function.
Example:
Enter m and n: 3 7
Expected output: 3 4 5 6 7
Exercise 8: Modular Functions
Write the following functions and call them from main(). Do not use the math.h library.
Power(float N, int M): Computes and returns \(N^M\).DigitsNum(int N): Returns the number of digits inN.DigitsAvg(int N): Returns the average of the digits ofN.IsPrimeEfficient(int N): Returns1ifNis prime, and0otherwise.
In main(), you need to input values, call these functions, and print the results demonstrating they work.
Example:
Enter a float base and integer exponent for Power(): 2.5 3
Result: 15.62
Enter a positive integer for Digit operations and Prime check: 12345
Number of digits: 5
Average of digits: 3.00
12345 is Not Prime
Exercise 9
What is the output of the following code?
#include<stdio.h>
int main() {
int myList[] = {0, 1, 2, 3, 4, 5};
for (int i = 4; i >= 0; i--) {
myList[i + 1] = myList[i];
}
for (int i = 0; i < 6; i++)
printf("%d ", myList[i]);
return 0;
}
#include<iostream>
using namespace std;
int main() {
int myList[] = {0, 1, 2, 3, 4, 5};
for (int i = 4; i >= 0; i--) {
myList[i + 1] = myList[i];
}
for (int i = 0; i < 6; i++)
cout << myList[i] << " ";
return 0;
}
Exercise 10
What is the value of indexOfMax after executing the following code?
#include<stdio.h>
int main() {
int myList[] = {1, 5, 5, 5, 5, 1};
int max = myList[0];
int indexOfMax = 0;
for (int i = 1; i < 6; i++) {
if (myList[i] > max) {
max = myList[i];
indexOfMax = i;
}
}
printf("%d", indexOfMax);
return 0;
}
#include<iostream>
using namespace std;
int main() {
int myList[] = {1, 5, 5, 5, 5, 1};
int max = myList[0];
int indexOfMax = 0;
for (int i = 1; i < 6; i++) {
if (myList[i] > max) {
max = myList[i];
indexOfMax = i;
}
}
cout << indexOfMax;
return 0;
}
Think: What would be the value of
indexOfMaxif the condition wasmyList[i] >= max?
Exercise 11
Which of the following code segments produces the following output: 1 4 9 16 25?
a.
int i = 0;
while (i < 5) {
i = i + 1;
printf("%d ", i * i);
}
b.
int i = 0;
while (i <= 5) {
i = i + 1;
printf("%d ", i * i);
}
c.
int i = 0;
while (i <= 5) {
printf("%d ", i * i);
i = i + 1;
}
d.
int i = 0;
while (i < 5) {
printf("%d ", i * i);
i = i + 1;
}
a.
int i = 0;
while (i < 5) {
i = i + 1;
cout << i * i << " ";
}
b.
int i = 0;
while (i <= 5) {
i = i + 1;
cout << i * i << " ";
}
c.
int i = 0;
while (i <= 5) {
cout << i * i << " ";
i = i + 1;
}
d.
int i = 0;
while (i < 5) {
cout << i * i << " ";
i = i + 1;
}
Exercise 12
Which of the following code segments will not finish (go to infinite loop)?
a.
int i = 1;
int j = 0;
while (i != 10) {
j = i * 5;
printf("%d", j);
i = i + 1;
}
b.
int i = 1;
int j = 0;
while (i != 10) {
j = i * 5;
printf("%d", j);
i = i + 2;
}
c.
int i = 1;
int j = 0;
while (i <= 10) {
j = i * 3;
printf("%d", j);
i = i + 2;
}
d.
int i = 1;
int j = 0;
while (i <= 10) {
j = i * 3;
printf("%d", j);
i = i + 5;
}
a.
int i = 1;
int j = 0;
while (i != 10) {
j = i * 5;
cout << j;
i = i + 1;
}
b.
int i = 1;
int j = 0;
while (i != 10) {
j = i * 5;
cout << j;
i = i + 2;
}
c.
int i = 1;
int j = 0;
while (i <= 10) {
j = i * 3;
cout << j;
i = i + 2;
}
d.
int i = 1;
int j = 0;
while (i <= 10) {
j = i * 3;
cout << j;
i = i + 5;
}
Exercise 13
What is the output of the following code? And what is the purpose of this code?
#include <stdio.h>
int main() {
int a = 5, b = 10;
a = a + b;
b = a - b;
a = a - b;
printf("new values: a = %d, b = %d\n", a, b);
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a = 5, b = 10;
a = a + b;
b = a - b;
a = a - b;
cout << "new values: a = " << a << ", b = " << b << "\n";
return 0;
}