Practice Program







  • Program Of Factors of a Numbers.
  • A factor of an integer n is an integer which can be multiplied by some integer to produce n.
  • For every number , 1 and itself are one of the factors of n.
  • If 10 is an integer then it's factors are = 1,2,5,10, for 20 ,factors are = 1,2,4,5,10,20
  • We will write a program to display the factors of a number.
  • Solution:through for loop
    void main()
    {
        int i,n;
    
        printf("Enter the number:");
        scanf("%d",&n);
    
    
     //As you know 1 is factor of every number ,
     
     printf("Factors of %d are:",n);
     //so we are printing it
        printf("%d\t",1);
    
    
        for(i=2;i<i++)
        {
            if(n%i == 0)//for every value of i we are checking that,
              printf("%d\t",i); //Is n is divisible by i
        }              //so,if it is divisible by i then ,i
                       //is the one of the factor of
                          //n and we are printing it.
        printf("%d",n);
        //As you know every element is divisible by itself
        //So we are printing the value of 'n' as one of the
         //factor of 'n'
    }
    

    Output1
    Enter the number:20

    Factors of 20 are:: 1       2        4        5        10     20

    Output2
    Enter the number:10

    Factors of 10 are:
    1       2        5        10

    Output3
    Enter the number:30

    Factors of 30 are:
    1      2       3        5        6        10     15    30




    Solution:from while loops
    void main()
    {
        int i,n;
    
        printf("Enter the number:");
        scanf("%d",&n);
    
    
     //As you know 1 is factor of every number ,
     //so we are printing it
        printf("%d\t",1);
    
        i=2;
        while(i<n)
        {
            if(n%i == 0)//for every value of i we are checking that,
              printf("%d\t",i); //Is n is divisible by i
         i++;              //so,if it is divisible by i then ,i
        }             //is the one of the factor of
                          //n and we are printing it.
        printf("%d",n);
        //As you know every element is divisible by itself
        //So we are printing the value of 'n' as one of the
         //factor of 'n'
    }
    


    Solution:from do-while loops
    void main()
    {
        int i,n;
    
        printf("Enter the number:");
        scanf("%d",&n);
    
    
     //As you know 1 is factor of every number ,
     //so we are printing it
        printf("%d\t",1);
    
        i=2;
        do
        {
            if(n%i == 0)//for every value of i we are checking that,
              printf("%d\t",i); //Is n is divisible by i
         i++;              //so,if it is divisible by i then ,i
        } while(i<n);             //is the one of the factor of
                          //n and we are printing it.
        printf("%d",n);
        //As you know every element is divisible by itself
        //So we are printing the value of 'n' as one of the
         //factor of 'n'
    }
    





    defining a function 'give_factor()' which will print factors of number.

    void  give_factor(n)
    { int i;
        //As you know 1 is factor of every number ,
     //so we are printing it
        printf("%d\t",1);
    
        i=2;
        while(i<n)
        {
            if(n%i == 0)//for every value of i we are checking that,
              printf("%d\t",i); //Is n is divisible by i
         i++;              //so,if it is divisible by i then ,i
        }             //is the one of the factor of
                          //n and we are printing it.
        printf("%d",n);
        //As you know every element is divisible by itself
        //So we are printing the value of 'n' as one of the
         //factor of 'n'
    }
    

    Using above define function 'give_factor()':
    void main()
    {
        int i,n;
    
        printf("Enter the number:");
        scanf("%d",&n);
       printf(" Factors of %d are:",n);
        //calling function 
        give_factor(n);  //actual parameters.
    
     
    }
    

    Output1
    Enter the number:20

    Factors of 20 are: 1       2        4        5        10     20

    Using above define function 'give_factor()':
    void main()
    {
        int i,n;
    
        printf("Enter the number:");
        scanf("%d",&n);
       printf(" Factors of %d are:",n);
        //calling function 
        give_factor(n);  //actual parameters.
    
     
    }
    

    Output2
    Enter the number:10

    Factors of 10 are:
    1       2        5        10

    Using above define function 'give_factor()':
    void main()
    {
        int i,n;
    
        printf("Enter the number:");
        scanf("%d",&n);
       
        printf(" Factors of %d are:",n);
        //calling function 
       give_factor(n);  //actual parameters.
    
     
    }
    

    Output2
    Enter the number:30

    Factors of 30 are:
    1      2       3        5        6        10     15    30