# Loop Exercise 1

On this page, you will see some loop exercise and their solution.

## Exercise 1:

Write a program (WAP) that will print following series upto Nth terms.
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, ……,N.

Sample Input Sample Output
2 1, 2
3 1, 2, 3
5 1, 2, 3, 4, 5

### Solution:

#include<stdio.h>
int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
if(i==n){
printf("%d",i);
}
else{
printf("%d, ",i);
}
}
return 0;
}

5
1, 2, 3, 4, 5

## Exercise 2:

Write a program (WAP) that will print following series upto Nth terms.
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 ……N.

Sample Input Sample Output
2 1, 3
5 1, 3, 5, 7, 9
11 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21

### Solution:

#include<stdio.h>
int main(){
int n, value=1;
scanf("%d",&n);
for(int i=1;i<=n;i++){
if(i==n){
printf("%d",value);
}
else{
printf("%d, ",value);
}
value+=2;
}
return 0;
}

#### Output:

11
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21

## Exercise 3:

Write a program (WAP) that will print following series upto Nth terms.
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, …… N.

Sample Input Sample Output
1 1
2 1, 0
7 1, 0, 1, 0, 1, 0, 1

### Solution:

#include<stdio.h>
int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
if(i%2!=0){
if(i==n){
printf("1");
}
else{
printf("1, ");
}
}
else{
if(i==n){
printf("0");
}
else{
printf("0, ");
}
}
}
return 0;
}

#### Output:

13
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1

## Exercise 4:

Write a program (WAP) that will take N numbers as inputs and compute their average.
[N:B: Restriction: Without using any array ]

Sample Input Sample Output
3
10   20   30.5
AVG of 3 inputs: 20.166667
2
11.1   22.4
AVG of 2 inputs: 16.750000

### Solution:

#include<stdio.h>
int main(){
int n;
scanf("%d",&n);
float total,num;
for(int i=1;i<=n;i++){
scanf("%f",&num);
total+=num;
}
printf("AVG of %d inputs: %f",n,(total/n));
return 0;
}

#### Output:

3
10 20 30.5
AVG of 3 inputs: 20.166666

## Exercise 5:

Write a program (WAP) that will take two numbers X and Y as inputs. Then it will print the square of X and increment (if XY) X by 1, until X reaches Y. If and when X is equal to Y, the program prints “Reached!”

Sample Input Sample Output
10   5 100, 81, 64, 49, 36, Reached!
5   10 25, 36, 49, 64, 81, Reached!
10   10 Reached!

### Solution:

#include<stdio.h>
int main(){
int x,y;
scanf("%d%d",&x,&y);
if(x==y){
printf("Reached!\n");
}
else if(x<y){
for(int i=x; i<=y;i++){
if(i==y){
printf("Reached!\n");
}
else{
printf("%d, ",i*i);
}
}
}
else if(x>y){
for(int i=x; i>=y;i--){
if(i==y){
printf("Reached!\n");
}
else{
printf("%d, ",i*i);
}
}
}
return 0;
}

#### Output:

10 5
100, 81, 64, 49, 36, Reached!

## Exercise 6:

Write a program (WAP) for the described scenario: Player-1 picks a number X and Player-2 has to guess that number within N tries. For each wrong guess by Player-2, the program prints “Wrong, N-1 Choice(s) Left!” If Player-2 at any time successfully guesses the number, the program prints “Right, Player-2 wins!” and terminates right away. Otherwise after the completion of N wrong tries, the program prints “Player-1 wins!” and halts.

Sample Input Sample Output
5
3
12   8   5
Wrong, 2 Choice(s) Left!
Wrong, 1 Choice(s) Left!
Right, Player-2 wins!
100
5
50   100
Wrong, 2 Choice(s) Left!
Right, Player-2 wins!
20
3
12   8   5
Wrong, 2 Choice(s) Left!
Wrong, 1 Choice(s) Left!
Wrong, 0 Choice(s) Left!
Player-1 wins!

### Solution:

#include<stdio.h>
int main(){
int guess,num,time;
scanf("%d",&num);
scanf("%d",&time);
for(int i = 0; i < time; i++) {
scanf("%d", &guess);
if(guess == num) {
printf("Right, Player-2 wins!\n");
break;
} else {
printf("Wrong, %d Choice(s) Left!\n", time-i-1);
}
}
if(guess != num) {
printf("Player-1 wins!\n");
}
return 0;
}

#### Output:

5
3
12 8 5
Wrong, 2 Choice(s) Left!
Wrong, 1 Choice(s) Left!
Right, Player-2 wins!

## Exercise 7:

Write a program (WAP) that will run and show keyboard inputs until the user types an 'A' at the keyboard.

Sample Input Sample Output
X
1
a
A
Input 1: X
Input 2: 1
Input 3: a

### Solution:

#include<stdio.h>
int main(){
char input;
int n=1;
while (1){
scanf(" %c", &input);
if(input == 'A') {
break;
}
printf("Input %d: %c\n", n,input);
n++;
}
return 0;
}

X
1
a
A
Input 1: X
Input 2: 1
Input 3: a

## Exercise 8:

Write a program (WAP) that will reverse the digits of an input integer.

Sample Input Sample Output
13579 97531
4321 1234

### Solution:

#include<stdio.h>
int main(){
int num, reversed_num = 0;
scanf("%d", &num);
while (num != 0) {
int digit = num % 10;
reversed_num = reversed_num * 10 + digit;
num /= 10;
}
printf("%d\n", reversed_num);
return 0;
}

1234
4321

## Exercise 9:

Write a program (WAP) that will find the grade of N students. For each student, it will take the marks of his/her the attendance (on 5 marks), assignment (on 10 marks), class test (on 15 marks), midterm (on 50 marks), term final (on 100 marks). Then based on the tables shown below, the program will output his grade.

 Attendance (A) 5% Assignments (HW) 10% Class Tests (CT) 15% Midterm (MT) 30% Final (TF) 40%
90-100 A 70-73 C+ Less than 55 F
86-89 A- 66-69 C
82-85 B+ 62-65 C-
78-81 B 58-61 D+
74-77 B- 55-57 D

### Solution:

#include<stdio.h>
int main() {
int n, i;
float A,hw, ct, mt, ft, total_marks, percentage;
scanf("%d", &n);
for (i = 1; i <= n; i++) {
scanf("%f%f%f%f%f", &A,&hw,&ct,&mt,&ft);
total_marks = (A+hw+ct+mt+ft);
percentage = (total_marks / 180) * 100;
if (percentage >= 90) {
printf("Student %d : A\n",i);
} else if (percentage >= 86 && percentage <= 89) {
printf("Student %d : A-\n",i);
} else if (percentage >= 82 && percentage <= 85) {
printf("Student %d : B+\n",i);
} else if (percentage >= 78 && percentage <= 81) {
printf("Student %d : B\n",i);
} else if (percentage >= 74 && percentage <= 77) {
printf("Student %d : B-\n",i);
} else if (percentage >= 70 && percentage <= 73) {
printf("Student %d : C+\n",i);
} else if (percentage >= 66 && percentage <= 69) {
printf("Student %d : C\n",i);
} else if (percentage >= 62 && percentage <= 65) {
printf("Student %d : C-\n",i);
} else if (percentage >= 58 && percentage <= 61) {
printf("Student %d : D+\n",i);
} else if (percentage >= 55 && percentage <= 57) {
printf("Student %d : D\n",i);
} else {
printf("Student %d : F\n",i);
}
}
return 0;
}

#### Output:

2
5 10 15 44.5 92.5
0 7.5 5 20 55.5
Student 1 : A
Student 2 : F

## Exercise 10:

Write a program (WAP) that will give the sum of first Nth terms for the following series.
1, -2, 3, -4, 5, -6, 7, -8, 9, -10, 11, -12, 13, -14, …….

Sample Input Sample Output
2 Result: -1
3 Result: 2
4 Result: -2

### Solution:

#include<stdio.h>
int main() {
int n, i, sum = 0;
scanf("%d", &n);
for (i = 1; i <= n; i++) {
if (i % 2 == 0) {
sum -= i;
}
else {
sum += i;
}
}
printf("Result: %d\n", sum);
return 0;
}

2
Result: -1