Practice Program







  • Program Of Armstrong Numbers.
  • An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 407 is an Armstrong number since 4**3 + 0**3 + 7**3 = 407.
  • We will see a program of printing Armstrong number from 1 to 1000.
  • Solution: through for loop
    void main()
    {
    
        int i,a,b=0,n;
        printf("Armstrong numbers are:");
        for(i=1;i<=1000;i++)
        { //at every iteration we assigning
            b = 0;//b =0.
            n = i;
            while(n!=0)
            {  //when a number is modulo with 10
            // then it give the last digit of that number
                a = n%10;
                            //at every iteration we are  assigning the current_value of b
                            // and adding the value of a  to b and,
                b = b+(a*a*a); //assigning to the new value of b.
    
                n = n/10; //after setting up with new value of b then
                          //we are assigning the current value to n divide by 10 to
            }             //the the new value of n.
    
            if(i == b) //at every iteration we are checking if value at variable i
              printf("%d\t",i);//and b are same ,if there are same then
                              // number is Armstrong number and we are printing it.
        }
    }
    

    Output1
    Armstrong numbers are:

    1       153        370        371        407





    Solution: from while loops
    void main()
    {
    
        int i,a,b=0,n;
        printf("Armstrong numbers are:");
        i=1;
        while(i<=1000)
        { //at every iteration we assigning
            b = 0;//b =0.
            n = i;
            while(n!=0)
            {  //when a number is modulo with 10
            // then it gives the last digit of that number
                a = n%10;
                            //at every iteration we are  assigning the current_value of b
                            // and adding the value of a and to b and,
                b = b+(a*a*a); //assigning to the new value of b.
    
                n = n/10; //after setting up with new value of b then
                          //we are assigning the current value to n divide by 10 to
            }             //the the new value of n.
    
            if(i == b) //at every iteration we are checking if value at variable i
              printf("%d\t",i);//and b are same ,if there are same then
                              // number is Armstrong number and we are printing it.
    
        i++;
        }
    }
    


    Solution: from do-while loops
    void main()
    {
    
        int i,a,b=0,n;
        printf("Armstrong numbers are:");
        i=1;
        do
        { //at every iteration we assigning
            b = 0;//b =0.
            n = i;
            do
            {  //when a number is modulo with 10
            // then it gives the last digit of that number
                a = n%10;
                            //at every iteration we are  assigning the current_value of b
                            // and adding the value of a and to b and,
                b = b+(a*a*a); //assigning to the new value of b.
    
                n = n/10; //after setting up with new value of b then
                          //we are assigning the current value to n divide by 10 to
            }while(n!=0);             //the the new value of n.
    
            if(i == b) //at every iteration we are checking if value at variable i
              printf("%d\t",i);//and b are same ,if there are same then
                              // number is Armstrong number and we are printing it.
    
        i++;
        }while(i<=1000);
    }
    





    defining a function 'is_armstrong' which will check whether a number is Armstrong  number or not.
    int is_armstrong(int m)
    {
        int i,a,b=0,n;
        n = m;
        while(n!=0)
        {  //when we number is modulo with 10
        // then give the last digit of that number
            a = n%10;
                        //at every iteration we are  assigning the current_value of b
                        //multiply by 10 and then adding the value of a and
            b = b+(a*a*a); //assigning to the new value of b.
    
            n = n/10; //after setting up with new value of b then
                      //we are assigning the current value to n divide by 10 to
        }             //the new value of n.
    
        if(b == m)
        {
            return 1;
        }
        else
            return 0;
    }
    

    Using above define function 'is_armstrong':
    void main()
    
    {
        int n,result;
    
        printf("Enter the Number");
        scanf("%d",&n);
    
        //calling function.
        result = is_armstrong(n);//actual parameter.
    
        if(result == 1)//if 'is_armstrong()' will return 1,
        {            // then number is armstrong,otherwise not
            printf(Armstrong Number);
        }
        else
            printf("Not an Armstrong Number");
    }
    

    Output1
    Enter the Number:407

    Armstrong Number

    Using above define function 'is_armstrong':
    void main()
    
    {
        int n,result;
    
        printf("Enter the Number");
        scanf("%d",&n);
    
        //calling function.
        result = is_armstrong(n);//actual parameter.
    
        if(result == 1)//if 'is_armstrong()' will return 1,
        {            // then number is armstrong,otherwise not
            printf(Armstrong Number);
        }
        else
            printf("Not an Armstrong Number");
    }
    

    Output2
    Enter the Number:371

    Armstrong Number

    Using above define function 'is_armstrong':
    void main()
    
    {
        int n,result;
    
        printf("Enter the Number");
        scanf("%d",&n);
    
        //calling function.
        result = is_armstrong(n);//actual parameter.
    
        if(result == 1)//if 'is_armstrong()' will return 1,
        {            // then number is armstrong,otherwise not
            printf(Armstrong Number);
        }
        else
            printf("Not an Armstrong Number");
    }
    

    Output3
    Enter the Number:111

    Not an Armstrong Number