Play with C Programming loop with 20 examples code

1 comment

Play with C Programming loop with 20 examples code

Play with C Programming loop with 20 examples code

C Program using loop - 1:

Write a program in C to display the first 10 natural numbers.

Description:
Program will print 10 natural numbers like 1 2 3 4 5 6 7 8 9 10.

Program in C language using for loop

#include <stdio.h>
void main()
{     
    int i;
    printf("The first 10 natural number is: ");
    for (i=1; i<=10; i++)
    {      
  printf("%d ",i);
    }
    printf("\n");
}


Output of the C program:

The first 10 natural number is: 1 2 3 4 5 6 7 8 9 10



Program in C language using while loop

#include <stdio.h>
void main()
{
    int i=1;
    printf("The first 10 natural number is: ");

    while(i <= 10)
    {
        printf("%d ",i);
        i++;
    }
    printf("\n");
}


Output of the C program:

The first 10 natural number is: 1 2 3 4 5 6 7 8 9 10


Program in C language using do while loop

#include <stdio.h>
void main()
{
    int a = 1;
    printf("10 Natural Number is: ")
    do
    {
        printf("%d ", a);
        a++;
    }
    while( a <= 10 );

    printf("\n");
}


Output of the C program:

The first 10 natural number is: 1 2 3 4 5 6 7 8 9 10




C Program using loop - 2:

Write a program in C to display the n terms of natural numbers and their summation.

Description:
Program will first print 10 natural numbers like 1 2 3 4 5 6 7 8 9 10 and then print their summation = 55 ?.

Program in C language using for loop

#include <stdio.h>
void main()
{
    int i, n;
    int sum = 0;
    printf("Enter How many numbers need to sum : ");
    scanf("%d", &n);

    printf("The first %d natural numbers are : ", n);
    for (i=1; i<=n; i++)
    {
        printf("%d ",i);
        sum += i;
    }
    printf("\nThe summation is : %d\n", sum);
}


Output of the C program:

Enter How many numbers need to sum : 10
The first 10 natural numbers are : 1 2 3 4 5 6 7 8 9 10
The summation is : 55



Program in C language using while loop

#include <stdio.h>
void main()
{
    int i = 1, n;
    int sum = 0;
    printf("Enter How many numbers need to sum : ");
    scanf("%d", &n);

    printf("The first %d natural numbers are : ", n);

    while(i <= n)
    {
        printf("%d ",i);
        sum += i;
        i++;
    }
    printf("\nThe summation is : %d\n", sum);
}


Output of the C program:

Enter How many numbers need to sum : 10
The first 10 natural numbers are : 1 2 3 4 5 6 7 8 9 10
The summation is : 55


Program in C language using do while loop

#include <stdio.h>
void main()
{
    int i = 1, n;
    int sum = 0;
    printf("Enter How many numbers need to sum : ");
    scanf("%d", &n);

    printf("The first %d natural numbers are : ", n);

    do
    {
        printf("%d ",i);
        sum += i;
        i++;
    }
    while( i <= n );

    printf("\nThe summation is : %d\n", sum);
}


Output of the C program:

Enter How many numbers need to sum : 10
The first 10 natural numbers are : 1 2 3 4 5 6 7 8 9 10
The summation is : 55



After the two C codes or 6 codes using different loop you are now pretty much clear what is the same syntax in different loops in for loop, in while loop and in do-while loop.

So we'll now just use any of them and you can obviously convert them to your desired loop.



C Program using loop - 3:

Write a program in C to read 10 numbers from the keyboards and make their summation and average.

Description:
Program will first take 10 numbers from the user/keyboard and then print the summation and average of that 10 numbers

Program in C language using for loop

#include <stdio.h>
void main()
{
    int i = 1, n;
    int sum = 0;
    printf("Enter How many numbers need to sum : ");
    scanf("%d", &n);

    printf("The first %d natural numbers are : ", n);

    do
    {
        printf("%d ",i);
        sum += i;
        i++;
    }
    while( i <= n );

    printf("\nThe summation is : %d\n", sum);
}


Output of the C program:

Input the 10 numbers :
Number 1 :10
Number 2 :20
Number 3 :30
Number 4 :40
Number 5 :50
Number 6 :60
Number 7 :70
Number 8 :80
Number 9 :90
Number 10 :100
The sum of 10 no is : 550
The Average is : 55.000000





C Program using loop - 4:

Write a program in C to find the multiplication table of a given integer.

Description:
Program will take an integer and make a multiplication table of that integer.

Program in C language using for loop

#include <stdio.h>
void main()
{
    int i,n;
    printf("Enter number of multiplication table : ");
    scanf("%d", &n);
    printf("\n");

    for(i=1; i<=10; i++)
    {
        printf("%d X %d = %d \n", n, i, n*i);
    }
}


Output of the C program:

Enter number of multiplication table : 15

15 X 1 = 15
15 X 2 = 30
15 X 3 = 45
15 X 4 = 60
15 X 5 = 75
15 X 6 = 90
15 X 7 = 105
15 X 8 = 120
15 X 9 = 135
15 X 10 = 150




C Program using loop - 5:

Write a program in C of right angle triangle of star / asterisk like that.

Description:
Program will take an integer and make a triangle of star or asterisk of that integer.

Program in C language using for loop

#include <stdio.h>
void main()
{
    int i, j, rows;
    printf("Enter the number of rows : ");
    scanf("%d", &rows);

    for(i=1; i<=rows; i++)
    {
        for(j=1; j<=i; j++)
        {
            printf("*");
        }
        printf("\n");
    }
}


Output of the C program:

Enter the number of rows : 10
*
**
***
****
*****
******
*******
********
*********
**********


[Continuing...]


Tags: 

C programming loop, c programming loop example, loops in c programming, advance level loop example in c programming, Loops, for loop in c, while loop in c, do-while loop in c, c program to print the summation of 10 numbers, c program to print triangle of star, c program to print the summation and average

1 comment :

  1. Computer Science GK - Computer science is a scientific and practical approach to computation and its application. It contains two core parts hardware and software. Hardware is the physical machine where we run computer applications. Hardware has evolved and has made it possible for computing devices to become faster, smaller and more efficient. The software can be very complex. It combines algorithms, data structure, the input, output, and the intermediate storage of data. Computer science also deals with a graphical user interface which makes software programs more user-friendly. Today computers are an integral part of our lives. For more visit: Computermobile.info

    ReplyDelete