C programing , Loop exercise part- 01

 Loop related problems 

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 InputSample Output
2
1, 2
3
1, 2, 3
5
1, 2, 3, 4, 5
 
 

 
    #include<stdio.h>
 
    int main() {
 
       int n, i;
       printf(" Inter The Nth term N = ");
       scanf_s("%d", &n);
       for (i = 1; i <= n; i++ )
       {
             printf("%d", i);
             if (i != n)
             {
                    printf(",");
             }
       }
       return 0;
}

 
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 InputSample Output
2
1, 3
5
1, 3, 5, 7, 9
11
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21
 
 

#include<stdio.h>
int main () {
 
       int n, i;
 
       printf(" Inter The Nth term N = ");
       scanf_s("%d", &n);
 
       for (i = 1; i <= n; i++ )
      
 
      {
                    printf("%d", (i*2)-1);
 
                    if (i !=n)
                    {
                           printf(",");
                    }
 
       }
 
 
       return 0;
}
 
  
 
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 InputSample Output
1
1
2
1, 0
7
1, 0, 1, 0, 1, 0, 1


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

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 InputSample Output
3
10   20   30.5
AVG of 3 inputs: 20.166667
2
11.1   22.4
AVG of 2 inputs: 16.750000

#include<stdio.h>
int main() {
 
       int i, n;
       float num, sum =0, avrg;
 
       printf(" How many numbers you wwant to avrage = ");
       scanf_s("%d", &n);
 
       for (i = 1; i <= n; i++)
       {
             printf(" Enter a number = ");
             scanf_s("%f", &num);
 
             sum = sum + num;
 
 
       }
       avrg = sum / n;
       printf("The average of numbers is = %.3f\n", avrg);
 
       return 0;
}
 
 
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 InputSample 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 x, y, i ;

      

       printf(" Enter Number X = ");

       scanf_s("%d", &x);

       printf(" Enter Number y = ");

       scanf_s("%d", &y);

 

       while (x!=y)

       {

             if (x < y)

             {

                    printf("%d",x*x );

                    printf(",");

                    x++;   

             }

 

             if (x > y)

             {

                    printf("%d",x*x );

                    printf(",");

                    x--;

             }

 

            

       }

 

       if (x = y)

       {

             printf("Reached!");

       }

 

 

       return 0;

}







নাফিজ বারাকাহ্

Content Creator, Writer, Designer. Writes technology based articles, poems & Product reviews. Create gameplay videos on youtube, designs Logo, Poster also Typography as a Hobby.

1 Comments

Thank you for sharing your valuable comment.

  1. Thanks for sharing solution of exercise 05.

    ReplyDelete
Previous Post Next Post