Home
  • More

    More My Portfolio

Loops Related Problems

"The computing scientist’s main challenge is not to get confused by the complexities of his own making.— E. W. Dijkstra"

Problem #1
  • Problem Statement
  • Solution
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, …….

Sample input     Sample output
2                1, 2
5                1, 2, 3, 4, 5
11               1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
	#include
	int main()
	{
		int i,n,j=1;
		scanf("%d",&n);
		for(i=1;i<=n;i++)
		{
			if(j==1){
				printf("%d",i);
				j++;
			}
			else
				printf(",%d",i);
		}
		return 0;
	}
	
Problem #2
  • Problem Statement
  • Solution
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 …….

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

#include

int main()
{
    int i,n,j=1;
    scanf("%d",&n);
    for(i=1;i<=n;i+=2){
        if(j==1){
            printf("%d",i);
            j++;
        }
        else
            printf(",%d",i);
    }
    return 0;
}
	
Problem #3
  • Problem Statement
  • Solution
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, …….

Sample input             Sample output
1                        1
2                        1, 0
3                        1, 0, 1
4                        1, 0, 1, 0
7                        1, 0, 1, 0, 1, 0, 1
13                       1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1

#include

int main()
{
    int i,n,j=1;
    scanf("%d",&n);
    for(i=1;i<=n;i++){
        if(j==1){
            printf("%d",i%2);
            j++;
        }
        else
            printf(",%d",i%2);
    }
    return 0;
}
Problem #4
  • Problem Statement
  • Solution
Write a program (WAP) that will take N numbers as inputs and compute their average. 
(Restriction: Without using any array)

Sample input		Sample output
3
10 20 30.5          AVG of 3 inputs:  20.166667
2
22.4 11.1           AVG of 2 inputs:  16.750000

>

#include<stdio.h>

int main()
{
    int i,n;
    float a,sum=0;
    scanf("%d",&n);
    for(i=1;i<=n;i++){
        scanf("%f",&a);
        sum+=a;
    }
    printf("AVG of %d inputs: %f",n,sum/n);
    return 0;
}
Problem #5
  • Problem Statement
  • Solution
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(X,Y)		Sample output
10 5                    100, 81, 64, 49, 36, Reached!
5 10					25, 36, 49, 64, 81, Reached!
10 10                   Reached!


#include<stdio.h>

int main()
{
    int i,x,y;
    scanf("%d %d",&x,&y);
    if(x>y){
        int j=1;
        for(i=x;i>=y;i--){
            if(i==y){
                printf(" Reached!");
            }
            else
            if(j==1){
                printf("%d",i*i);
                j++;
            }
            else
                printf(",%d",i*i);
        }
    }
    else if(x<y){
        int j=1;
        for(i=x;i<=y;i++){
            if(i==y){
                printf(" Reached!");
            }
            else
            if(j==1){
                printf("%d",i*i);
                j++;
            }
            else
                printf(",%d",i*i);
        }
    }
    else
        printf("Reached!");
    return 0;
}
Problem #6
  • Problem Statement
  • Solution
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.

(Hint: Use break/continue)

Sample input(X,N,n1, n2,..,nN)			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, 4 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!


#include<stdio.h>

int main()
{
    int p1,p2,i,n;
	scanf("%d",&p1);
	scanf("%d",&n);
	int check=n;
	for(i=1;i<=n;i++){
		scanf("%d",&p2);
		check--;
		if(p2==p1){
			printf("Right,player-2 wins!\n");
			break;
		}
		else{
			printf("Wrong,%d chance(s) left!\n",check);
			if(check==0){
			    printf("Player-1 Wins!");
				break;
			}
			continue;
		}
	}
}
Problem #7
  • Problem Statement
  • Solution
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                   Input 1: X
1                   Input 2: 1
a                   Input 3: a
A

#include<stdio.h>

int main()
{
    char ch;
    int i=0;
    while(scanf(" %c",&ch)==1){
        i++;
        if(ch=='A')break;
        printf("Input %d: %c\n",i,ch);
    }
}
Problem #8
  • Problem Statement
  • Solution
Write a program (WAP) that will reverse the digits of an input integer.

Sample input       Sample output
13579              97531
4321               1234


#include<stdio.h>

int main()
{
    int n,rev=0;
    scanf("%d",&n);
    while(n!=0){
        rev=(rev*10)+(n%10);
        n=n/10;
    }
    printf("%d",rev);
    return 0;
}
Problem #9
  • Problem Statement
  • Solution
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


#include<stdio.h>

int main()
{
    int i,n,sum=0;
    scanf("%d",&n);
    for(i=1;i<=n;i++){
        if(i%2!=0){
            sum+=i;
        }
        else
            sum-=i;
    }
    printf("Result: %d",sum);
    return 0;
}
© , Designed & Coded by Robiuddin Robi. Using Now UI Kit Pro.

Have you tried once by yourself first?