Practice Program







  • Program of factorial of a numbers.
  • Solution: through for loop
    void main()
    {
        int i,n,fact=1;
    
        printf("Enter the number:");
        scanf("%d",&n);
    
      for(i=1; i<=n; i++)
      {
          fact = fact * i;
      }
      printf("\nFactorial of %d is %d",n,fact);
    }
    

    Output1
    Enter the number:5

    Factorial of 5 is 120.
    Output2,
    Enter the number:10

    Factorial of 10 is 3628800.
    Output3
    Enter the number:15

    Factorial of 15 is 2004310016.





    Solution : through while loop
    void main()
    {
        int i,n,fact=1;
    
        printf("Enter the number:");
        scanf("%d",&n);
    
      i=1;
      while( i<=n)
      {
          fact = fact * i;
          i++;
      }
      printf("\nFactorial of %d is %d",n,fact);
    }
    






    Solution: from do-while loop
    void main()
    {
        int i,n,fact=1;
    
        printf("Enter the number:");
        scanf("%d",&n);
    
      i=1;
      do
      {
          fact = fact * i;
          i++;
      }while( i<=n);
      printf("\nFactorial of %d is %d",n,fact);
    }
    





    defining a function 'fact' which will return factorial of a number.

    int fact(int n)
    {
        int i,fact=1;
    
        for(i=1; i<=n; i++)
        {
            fact = fact *i;
        }
        return fact;
    
    }
    

    Using above define function 'fact':
    void main()
    {
        int n,result;
    
        printf("Enter the number:");
        scanf("%d",&n);
    
       result = fact(n);
    
       printf("Factorial of %d is %d",n,result);
    }
    

    Output1
    Enter the number:5

    Factorial of 5 is 120.

    Using above define function 'fact':
    void main()
    {
        int n,result;
    
        printf("Enter the number:");
        scanf("%d",&n);
    
       result = fact(n);
    
       printf("Factorial of %d is %d",n,result);
    }
    

    Output2
    Enter the number:10

    Factorial of 10 is 3628800.

    Using above define function 'fact':
    void main()
    {
        int n,result;
    
        printf("Enter the number:");
        scanf("%d",&n);
    
       result = fact(n);
    
       printf("Factorial of %d is %d",n,result);
    }
    

    Output3
    Enter the number:15

    Factorial of 15 is 2004310016.