Practice Program







  • Program of calculating sum of square 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*i) ) to new value of sum. 
        for(i=1;i<=n;i++)
        {
            sum = sum+(i*i);
        }
        
        
        printf("Sum of square of  first %d natural numbers are %d",n,sum);
       
    
    }
    

    Output1
    Sum of how many numbers:5

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

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

    Sum of Square of first 15 natural numbers are 1240.





    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);
            i++;
        }
    
    
        printf("Sum of square 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);
            i++;
        }while(i<=n)
    
    
        printf("Sum of square of first %d natural numbers are %d",n,sum);
    
    
    }
    






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

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

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

    Output1
    Sum of how many numbers:5

    Sum of square of first 5 natural numbers are 55.

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

    Output2
    Sum of how many numbers:10

    Sum of square of first 10 natural numbers are 385.

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

    Output3
    Sum of how many numbers:15

    Sum of square of first 15 natural numbers are 1240.