Practice Program







  • Program of printing first 'n' natural numbers
  • Solution:  through for loop
    void main()
    {
        int n,i;
    
        printf("How many natural number you  want to print: ");
        scanf("%d",&n);
    
    
        printf("First %d natural numbers are:\n",n);
        for(i=1; i<=n; i++)
        {
            printf("%d\t",i);
        }
    
    
    }
    

    Output1
    How many natural number you want to print: 5

    First %d natural numbers are:
    1    2    3    4    5
    Output2
    How many natural number you want to print: 10

    First %d natural numbers are:
    1    2    3    4    5    6    7    8    9    10
    Output3
    How many natural number you want to print: 15

    First %d natural numbers are:
    1    2    3    4    5    6    7    8    9    10    11    12    13    14    15





    Solution : through while loop
    void main()
    {
        int n,i = 1;
    
        printf("How many natural number you  want to print: ");
        scanf("%d",&n);
    
    
        printf("First %d natural numbers are:\n",n);
        while(i<=n)
        {
            printf("%d\t",i);
            i++;
        }
    
    
    }
    






    Solution:from do-while loop
    void main()
    {
        int n,i = 1;
    
        printf("How many natural number you  want to print: ");
        scanf("%d",&n);
    
    
        printf("First %d natural numbers are:\n",n);
        do
        {
            printf("%d\t",i);
            i++;
        }
         while(i<=n);
    
    }