Loop Exercise 2
On this page, you will get some loop exercise and their solution. Try it by yourself first then if you can not do then see the solution.
Exercise 11:
Write a program (WAP) that will calculate the result for the first Nth terms of the following series. [In that series sum, dot sign (.) means multiplication]
12.2 + 22.3 + 32.4 + 42.5 + …….
Input | Output |
---|---|
2 | Result: 14 |
3 | Result: 50 |
4 | Result: 130 |
7 | Result: 924 |
Solution:
#include<stdio.h>
int main() {
int n;
int sum = 0;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
int term = (i * i);
sum += (i+1) * term;
}
printf("Result: %d\n", sum);
return 0;
}
Output:
7
Result: 924
Exercise 12:
Write a program (WAP) that will print print Fibonacci series upto
Nth terms.
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,……N.
Sample Input | Sample Output |
---|---|
1 | 1 |
2 | 1, 1 |
4 | 1, 1, 2, 3 |
7 | 1, 1, 2, 3, 5, 8, 13 |
Solution:
#include<stdio.h>
int main() {
int n, i;
int first = 1, second = 1, next;
scanf("%d", &n);
if(n==1){
printf("1");
}else{
printf("%d, %d", first, second);
for (i = 3; i <= n; i++) {
next = first + second;
printf(", %d", next);
first = second;
second = next;
}
}
return 0;
}
Output:
7
1, 1, 2, 3, 5, 8, 13
Exercise 13:
Write a program (WAP) that will print the factorial (N!) of a given number N. Please see the sample input output.
Sample Input | Sample Output |
---|---|
1 | 1! = 1 = 1 |
2 | 2! = 2 X 1 = 2 |
3 | 3! = 3 X 2 X 1 = 6 |
4 | 4! = 4 X 3 X 2 X 1 = 24 |
Solution:
#include<stdio.h>
int main() {
int n, i;
unsigned long long fact = 1;
scanf("%d", &n);
if (n < 0){
printf("Error!");
}
else {
for (i = 1; i <= n; ++i) {
fact *= i;
}
printf("%d! = %d ", n,n);
for (i = n-1; i >= 0; i--) {
if(i==0){
break;
}
printf("x %d ", i);
}
printf("= %d",fact);
}
return 0;
}
Output:
4
4! = 4 X 3 X 2 X 1 = 24
Exercise 14:
Write a program (WAP) that will find nCr where n >= r; n and r are integers.
Sample Input | Sample Output |
---|---|
5 2 | 10 |
10 3 | 120 |
7 7 | 1 |
6 1 | 6 |
Solution:
#include<stdio.h>
int fact(int z){
int f = 1, i;
if (z == 0){
return(f);
}
else{
for (i = 1; i <= z; i++){
f = f * i;
}
}
return(f);
}
int main(){
int n, r, result;
scanf("%d%d", &n, &r);
result = fact(n) / (fact(r) * fact(n - r));
printf("%d", result);
}
Output:
7 7
1
Exercise 15:
Write a program (WAP) that will find xy (x to the power y) where x, y are positive integers.
Input (x,y) | Output |
---|---|
5 2 | 25 |
2 0 | 1 |
6 1 | 6 |
0 5 | 0 |
Solution:
#include<stdio.h>
int main() {
int x, y;
long long result = 1;
scanf("%d%d", &x, &y);
for(int i = 0; i < y; i++) {
result *= x;
}
printf("%lld\n", result);
return 0;
}
Output:
5 2
25
Exercise 16:
WAP that will find the GCD (greatest common divisor) and LCM (least common multiple) of two positive integers.
Sample Input | Sample Output |
---|---|
5 7 |
GCD: 1 LCM: 35 |
12 12 |
GCD: 12 LCM: 12 |
12 32 |
GCD: 4 LCM: 96 |
Solution:
#include<stdio.h>
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
int main() {
int x, y;
int gcd_val, lcm_val;
scanf("%d %d", &x, &y);
gcd_val = gcd(x, y);
lcm_val = (x * y) / gcd_val;
printf("GCD: %d\n", gcd_val);
printf("LCM: %d\n", lcm_val);
return 0;
}
Output:
5 7
GCD: 1
LCM: 35
Exercise 17:
WAP that will determine whether a number is prime or not.
Sample Input | Sample Output |
---|---|
1 | Not prime |
2 | Prime |
11 | Prime |
39 | Not prime |
101 | Prime |
Solution:
#include<stdio.h>
int main() {
int n, i, flag = 0;
scanf("%d", &n);
for (i = 2; i <= n/2; ++i) {
if (n % i == 0) {
flag = 1;
break;
}
}
if (n == 1) {
printf("Not prime");
}
else {
if (flag == 0){
printf("Prime\n");
}
else{
printf("Not prime\n");
}
}
return 0;
}
Output:
2
Prime
Exercise 18:
WAP that will determine whether an integer is palindrome number or not.
Sample Input | Sample Output |
---|---|
9 | Yes |
91 | No |
222 | Yes |
12321 | Yes |
110 | No |
Solution:
#include<stdio.h>
int main() {
int n, reversed = 0, remainder, original;
scanf("%d", &n);
original = n;
while (n != 0) {
remainder = n % 10;
reversed = reversed * 10 + remainder;
n /= 10;
}
if(original == reversed){
printf("Yes\n");
}
else{
printf("No\n");
}
return 0;
}
Output:
222
Yes
Exercise 19:
WAP that will calculate following mathematical function for the input of x. Use only the series to solve the problem.
Sinx = x- (x3)/3! + (x5)/5! - (x7)/7! + ......- ∞.
Sample Input | Sample Output |
---|---|
1 | 0.841 |
2 | 0.909 |
3 | 0.141 |
Solution:
#include<stdio.h>
#include<math.h>
int main() {
double x, term, sum = 0;
int sign = 1, fact = 1;
scanf("%lf", &x);
for (int i = 1; i <= 15; i += 2) {
term = sign * pow(x, i) / fact;
sum += term;
sign *= -1;
fact *= (i+1) * (i+2);
}
printf("%.3lf\n", sum);
return 0;
}
Output:
1
0.841
Exercise 20:
Write a program that takes an integer number n as input and find out the sum of the following series up to n terms.
1 + 12 + 123 + 1234 + …….
Sample Input | Sample Output |
---|---|
1 | 1 |
2 | 13 |
3 | 136 |
4 | 1370 |
Solution:
#include<stdio.h>
int main() {
int n;
scanf("%d", &n);
int sum = 0, term = 0;
for (int i = 1; i <= n; i++) {
term = term * 10 + i;
sum += term;
}
printf("%d",sum);
return 0;
}
Output:
3
136