Practice Program







  • Program of calculating sum of n natural numbers
  • Solution: through for loop
    void main()
    {
        int n,i,sum = 0;;
        printf("Sum of how many numbers: ");
        scanf("%d",&n);
        
        //at every iteration we are assigning 
        //(current_value + i ) to new value of sum. 
        for(i=1;i<=n;i++)
        {
            sum = sum+i;
        }
        
        
        printf("Sum of first %d natural numbers are %d",n,sum);
       
    
    }
    

    Output1
    Sum of how many numbers:5

    Sum of first 5 natural numbers are 15.
    Output2
    Sum of how many numbers:10

    Sum of first 10 natural numbers are 55.
    Output3
    Sum of how many numbers:15

    Sum of first 15 natural numbers are 120.





    Solution : through while loop
    void main()
    {
        int n,i,sum = 0;;
        printf("Sum of how many numbers: ");
        scanf("%d",&n);
          i=1;
        //at every iteration we are assigning
        //(current_value + i ) to new value of sum.
        while(i<=n)
        {
            sum = sum+i;
            i++;
        }
    
    
        printf("Sum of first %d natural numbers are %d",n,sum);
    
    
    }
    






    Solution: from do-while loop
    void main()
    {
        int n,i,sum = 0;;
        printf("Sum of how many numbers: ");
        scanf("%d",&n);
          i=1;
        //at every iteration we are assigning
        //(current_value + i ) to new value of sum.
        do
        {
            sum = sum+i;
            i++;
        }while(i<=n)
    
    
        printf("Sum of first %d natural numbers are %d",n,sum);
    
    
    }
    





    defining a function 'sum' which will return sum of n numbers.

    int sum(int n)
    {
        int i,sum=0;
    
         //at every iteration we are assigning
        //(current_value + i ) to new value of sum.
        for(i=1;i<=n;i++)
        {
            sum = sum+i;
        }
        return sum;
    }
    

    Using above defined function 'Sum':
    void main()
    {
        int n,result;
        printf("How many  numbers of sum you want: ");
        scanf("%d",&n);
    
    
       result  = sum(5);
    
    
       printf("Sum of first %d numbers are %d",n,result);
    
    
    
    }
    

    Output1
    Sum of how many numbers:5

    Sum of first 5 natural numbers are 15.

    Using above defined function 'Sum':
    void main()
    {
        int n,result;
        printf("How many  numbers of sum you want: ");
        scanf("%d",&n);
    
    
       result  = sum(10);
    
    
       printf("Sum of first %d numbers are %d",n,result);
    
    
    
    }
    

    Output2
    Sum of how many numbers:10

    Sum of first 10 natural numbers are 55.

    Using above defined function 'Sum':
    void main()
    {
        int n,result;
        printf("How many  numbers of sum you want: ");
        scanf("%d",&n);
    
    
       result  = sum(15);
    
    
       printf("Sum of first %d numbers are %d",n,result);
    
    
    
    }
    

    Output3
    Sum of how many numbers:15

    Sum of first 15 natural numbers are 120.