Note: Try to solve these Exercises on your own before checking the solutions.
Filter:
Sort:
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
Solution
#include <stdio.h>
intmain(){intn,i;printf("Enter a positive integer: ");scanf("%d",&n);printf("Divisors of %d in decreasing order: ",n);for(i=n;i>=1;i--){if(n%i==0){printf("%d ",i);}}printf("\n");return0;}
#include <iostream>
usingnamespacestd;intmain(){intn;cout<<"Enter a positive integer: ";cin>>n;cout<<"Divisors of "<<n<<" in decreasing order: ";for(inti=n;i>=1;i--){if(n%i==0){cout<<i<<" ";}}cout<<"\n";return0;}
Exercise 2: Power Calculation
Write a CC++ program that reads in two positive integers base and exp and prints the result of:
\[result = base^{exp}\]
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
Solution
#include <stdio.h>
intmain(){intbase,exp;longlongresult=1;printf("Enter base and exponent: ");scanf("%d %d",&base,&exp);for(inti=0;i<exp;i++){result*=base;}printf("%d^%d = %lld\n",base,exp,result);return0;}
#include <iostream>
usingnamespacestd;intmain(){intbase,exp;longlongresult=1;cout<<"Enter base and exponent: ";cin>>base>>exp;for(inti=0;i<exp;i++){result*=base;}cout<<base<<"^"<<exp<<" = "<<result<<"\n";return0;}
Exercise 3: Factorial
Write a CC++ program that reads in a positive integer n and prints the result of n! (Factorial), where:
\[n! = 1 \times 2 \times \dots \times n\]
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
Solution
#include <stdio.h>
intmain(){intn;intfact=1;printf("Enter a positive integer: ");scanf("%d",&n);for(inti=1;i<=n;i++){fact*=i;}printf("%d! = %d\n",n,fact);return0;}
#include <iostream>
usingnamespacestd;intmain(){intn;intfact=1;cout<<"Enter a positive integer: ";cin>>n;for(inti=1;i<=n;i++){fact*=i;}cout<<n<<"! = "<<fact<<"\n";return0;}
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.
Solution
#include <stdio.h>
intmain(){intn,i,isPrime=1;printf("Enter an integer: ");scanf("%d",&n);if(n<=1){isPrime=0;}else{for(i=2;i<=n/2;i++){if(n%i==0){isPrime=0;break;}}}if(isPrime){printf("%d is a Prime number.\n",n);}else{printf("%d is not a Prime number.\n",n);}return0;}
#include <iostream>
usingnamespacestd;intmain(){intn,i,isPrime=1;cout<<"Enter an integer: ";cin>>n;if(n<=1){isPrime=0;}else{for(i=2;i<=n/2;i++){if(n%i==0){isPrime=0;break;}}}if(isPrime){cout<<n<<" is a Prime number.\n";}else{cout<<n<<" is not a Prime number.\n";}return0;}
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.
Solution
#include <stdio.h>
intmain(){intn,reversed=0,remainder;printf("Enter a number: ");scanf("%d",&n);while(n!=0){remainder=n%10;reversed=reversed*10+remainder;n/=10;}printf("Reversed number: %d\n",reversed);return0;}
#include <iostream>
usingnamespacestd;intmain(){intn,reversed=0,remainder;cout<<"Enter a number: ";cin>>n;while(n!=0){remainder=n%10;reversed=reversed*10+remainder;n/=10;}cout<<"Reversed number: "<<reversed<<"\n";return0;}
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\))
Solution
#include <stdio.h>
intmain(){intn,sum=0,remainder;printf("Enter an integer: ");scanf("%d",&n);if(n<0)n=-n;while(n>0){remainder=n%10;sum+=remainder;n/=10;}printf("Sum of digits: %d\n",sum);return0;}
#include <iostream>
usingnamespacestd;intmain(){intn,sum=0,remainder;cout<<"Enter an integer: ";cin>>n;if(n<0)n=-n;while(n>0){remainder=n%10;sum+=remainder;n/=10;}cout<<"Sum of digits: "<<sum<<"\n";return0;}
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, and do-while loops.
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
Solution
#include <stdio.h>
voidprintRange(intm,intn){if(m>n){printf("Error: m is greater than n.\n");return;}printf("Range: ");for(inti=m;i<=n;i++){printf("%d ",i);}printf("\n");}intmain(){intm,n;printf("Enter m and n: ");scanf("%d %d",&m,&n);printRange(m,n);return0;}
#include <iostream>
usingnamespacestd;voidprintRange(intm,intn){if(m>n){cout<<"Error: m is greater than n.\n";return;}cout<<"Range: ";for(inti=m;i<=n;i++){cout<<i<<" ";}cout<<"\n";}intmain(){intm,n;cout<<"Enter m and n: ";cin>>m>>n;printRange(m,n);return0;}
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 in N.
DigitsAvg(int N): Returns the average of the digits of N.
IsPrimeEfficient(int N): Returns 1 if N is prime, and 0 otherwise.
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
Solution
#include <stdio.h>
doublePower(floatN,intM){doubleresult=1.0;for(inti=0;i<M;i++){result*=N;}returnresult;}intDigitsNum(intN){intcount=0;if(N==0)return1;while(N!=0){N/=10;count++;}returncount;}doubleDigitsAvg(intN){intsum=0,count=0,digit;inttempN=N;if(N==0)return0.0;while(tempN!=0){digit=tempN%10;sum+=digit;tempN/=10;count++;}return(double)sum/count;}intIsPrimeEfficient(intN){if(N<=1)return0;for(inti=2;i*i<=N;i++){if(N%i==0)return0;}return1;}intmain(){floatfNum;intintNum,powerExp;printf("Enter a float base and integer exponent for Power(): ");scanf("%f %d",&fNum,&powerExp);printf("Result: %.2f\n\n",Power(fNum,powerExp));printf("Enter a positive integer for Digit operations and Prime check: ");scanf("%d",&intNum);printf("Number of digits: %d\n",DigitsNum(intNum));printf("Average of digits: %.2f\n",DigitsAvg(intNum));if(IsPrimeEfficient(intNum)){printf("%d is Prime\n",intNum);}else{printf("%d is Not Prime\n",intNum);}return0;}
#include <iostream>
#include <iomanip>
usingnamespacestd;doublePower(floatN,intM){doubleresult=1.0;for(inti=0;i<M;i++){result*=N;}returnresult;}intDigitsNum(intN){intcount=0;if(N==0)return1;while(N!=0){N/=10;count++;}returncount;}doubleDigitsAvg(intN){intsum=0,count=0,digit;inttempN=N;if(N==0)return0.0;while(tempN!=0){digit=tempN%10;sum+=digit;tempN/=10;count++;}return(double)sum/count;}intIsPrimeEfficient(intN){if(N<=1)return0;for(inti=2;i*i<=N;i++){if(N%i==0)return0;}return1;}intmain(){floatfNum;intintNum,powerExp;cout<<"Enter a float base and integer exponent for Power(): ";cin>>fNum>>powerExp;cout<<"Result: "<<fixed<<setprecision(2)<<Power(fNum,powerExp)<<"\n\n";cout<<"Enter a positive integer for Digit operations and Prime check: ";cin>>intNum;cout<<"Number of digits: "<<DigitsNum(intNum)<<"\n";cout<<"Average of digits: "<<fixed<<setprecision(2)<<DigitsAvg(intNum)<<"\n";if(IsPrimeEfficient(intNum)){cout<<intNum<<" is Prime\n";}else{cout<<intNum<<" is Not Prime\n";}return0;}
#include <stdio.h>
intmain(){intn;doublesum=0;printf("Enter n: ");scanf("%d",&n);for(inti=1;i<=n;i++){sum+=1.0/i;// 1.0 makes this a floating-point division}printf("Sum of the series: %.4f\n",sum);return0;}
#include <iostream>
#include <iomanip>
usingnamespacestd;intmain(){intn;doublesum=0;cout<<"Enter n: ";cin>>n;for(inti=1;i<=n;i++){sum+=1.0/i;// 1.0 makes this a floating-point division}cout<<"Sum of the series: "<<fixed<<setprecision(4)<<sum<<endl;return0;}
Exercise 15: Armstrong Number
An Armstrong number is a number that is equal to the sum of its digits, each raised to the power of the number of digits.
For example, \(153 = 1^3 + 5^3 + 3^3\) and \(9474 = 9^4 + 4^4 + 7^4 + 4^4\).
Write a program that reads a positive integer and prints whether it is an Armstrong number.
Example:
Enter a positive integer: 153
153 is an Armstrong number
Solution
#include <stdio.h>
intmain(){intn;printf("Enter a positive integer: ");scanf("%d",&n);// Count the digitsintdigits=0;for(inttemp=n;temp>0;temp/=10){digits++;}// Add every digit raised to the power of the number of digitsintsum=0;for(inttemp=n;temp>0;temp/=10){intdigit=temp%10;intpower=1;for(inti=0;i<digits;i++){power*=digit;}sum+=power;}if(sum==n){printf("%d is an Armstrong number\n",n);}else{printf("%d is not an Armstrong number\n",n);}return0;}
#include <iostream>
usingnamespacestd;intmain(){intn;cout<<"Enter a positive integer: ";cin>>n;// Count the digitsintdigits=0;for(inttemp=n;temp>0;temp/=10){digits++;}// Add every digit raised to the power of the number of digitsintsum=0;for(inttemp=n;temp>0;temp/=10){intdigit=temp%10;intpower=1;for(inti=0;i<digits;i++){power*=digit;}sum+=power;}if(sum==n){cout<<n<<" is an Armstrong number"<<endl;}else{cout<<n<<" is not an Armstrong number"<<endl;}return0;}
Exercise 16: Perfect Number
A perfect number is a positive integer that is equal to the sum of its proper divisors (all of its divisors except the number itself).
For example, \(6 = 1 + 2 + 3\) and \(28 = 1 + 2 + 4 + 7 + 14\).
Write a program that reads a positive integer and prints whether it is a perfect number.
Example:
Enter a positive integer: 28
28 is a perfect number
Solution
#include <stdio.h>
intmain(){intn;printf("Enter a positive integer: ");scanf("%d",&n);// Add up the proper divisors (every divisor except n itself)intsum=0;for(inti=1;i<=n/2;i++){if(n%i==0){sum+=i;}}if(sum==n){printf("%d is a perfect number\n",n);}else{printf("%d is not a perfect number\n",n);}return0;}
#include <iostream>
usingnamespacestd;intmain(){intn;cout<<"Enter a positive integer: ";cin>>n;// Add up the proper divisors (every divisor except n itself)intsum=0;for(inti=1;i<=n/2;i++){if(n%i==0){sum+=i;}}if(sum==n){cout<<n<<" is a perfect number"<<endl;}else{cout<<n<<" is not a perfect number"<<endl;}return0;}