C++ program to find Armstrong number

C++ Program to Check given number is Armstrong number or not


#include<iostream>
#include<iomanip>
int main(){
    int num,r,sum,temp;
    for(num=1;num<=500;num++){
         temp=num;
         sum = 0;
         while(temp!=0){
             r=temp%10;
             temp=temp/10;
             sum=sum+(r*r*r);
         }
         if(sum==num)
             cout << num << setw(2);
    }
    return 0;
}

 

---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------




---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------

Comments

  1. You have amazing blog

    I have four questions for c++
    can you help me solve all the four
    please,

    regards,

    ReplyDelete
  2. There are five questions you can try four of them because am working in the last one>

    I really appreciate your help.

    I am using visual studio , c++ language.




    Problem 1: Navi Language
    Dr. Smith, a linguistics professor, has recently published a book written in the Navi language. The language uses a subset of the English characters. However, the ordering of these characters is quite different from that of the English alphabet. For example, the letter A may come after H, and X may come before C, etc. Similar to other books, this book needs an index table in the back of the book, and the index must be ordered by the lexical order of the Navi language. You will be given the order of letters in the Navi language and a list of key words that have appeared in the book. You are to write a program that will generate the index table for the key words based on the given letter order.
    Input
    The first input line contains letters of the Navi language, from lowest order to the highest order. Only uppercase letters will be given and there is a blank space between two letters. The following input lines contain a list of words in lowercase letters, one word per line and the list is ended with ‘@’. You may assume that each word can be at most 15 characters long, and there are at most 50 words.
    Output
    Your program should print the words in lexical order. Same as any index table, the first letter of each word should be capitalized.
    Sample Input
    C M H B F
    hccbmf
    mhc
    hccfc
    cfh
    hbcf
    mbc
    @
    Sample Output
    Cfh
    Mhc
    Mbc
    Hccbmf
    Hccfc
    Hbcf



    ReplyDelete
  3. Problem 2: Armstrong and Perfect Numbers
    More than 2500 years ago, mathematicians got interested in numbers.
    Armstrong Numbers: The number 153 has the odd property that 13+53 + 33 = 1 + 125 + 27 = 153. Namely, 153 is equal to the sum of the cubes of its own digits.
    Perfect Numbers: A number is said to be perfect if it is the sum of its own divisors (excluding itself). For example, 6 is perfect since 1, 2, and 3 divide evenly into 6 and 1+ 2 + 3 = 6.
    Input
    Up to 50 numbers, stored in an array.
    Output
    Print the perfect numbers
    Print the Armstrong numbers.
    Hint: use functions !
    Sample Input
    153 6 26
    Sample Output
    Perfect Numbers are: 6
    Armstrong numbers are: 153




    ReplyDelete
  4. Problem 3: Apples Eater
    Mr. Apple loves to eat apples, but he eats apples in a rather strange way. Mr. Apple arranges his apples in a circle, numbers them, and then he eats every other next apple. The last remaining apple he keeps to make apple pie. The following diagram shows how Mr. Apple picks the last apple, assuming there are 5 apples:
    Input
    Each input line contains one integer n, where n is the number of apples (1 ≤ n ≤ 99999).
    Output
    Print the number of apples followed by one space followed by the number of the last apple left
    Sample Input
    5
    Sample output
    5 3



    ReplyDelete
  5. Problem 4: Subset Sums
    Given a set of positive integers S, find all subsets of S, such that the sum of elements of each subset equals to a given integer n.
    Input
    Every line contains a distinct positive integer, the set S terminates when 0 is found, followed by the value sum.
    Output
    For each data set, output the subsets whose sum is equal to sum, elements separated by a space, each subset is separated by an empty line.
    Sample Input
    4
    2
    7
    5
    9
    0
    11
    Sample Output
    2 4 5
    2 9
    4 7

    ReplyDelete


  6. Problem 5: Climbing Worm
    An inch tall worm is at the bottom of a well n inches deep. It has enough energy to climb u inches every minute, but then has to rest a minute before climbing again. During the rest, it slips down d inches. The process of climbing and resting then repeats. How long before the worm climbs out of the well? We'll always count a portion of a minute as a whole minute and if the worm just reaches the top of the well at the end of its climbing, we'll assume the worm makes it out.
    You are required to implement two functions:
    1) Function to check that the inputs of the user obey to the assumption that d < u and n < 100
    2) A function that returns the number of minutes needed for the worm to climb the well.

    Input The user enters 3 positive integers n, u and d. These give the values mentioned in the paragraph above. Furthermore, you may assume d < u and n < 100.
    Output an integer indicating the number of minutes it takes for the worm to climb out of the well.
    Sample Input
    10 2 1
    Sample Output
    17


    Thank you SO MUCH for your time

    ReplyDelete
  7. Am really wating for your help , please

    ReplyDelete
  8. /*
    1049 Climbing Worm
    Time Limit : 1000 ms Memory Limit : 32768 K Output Limit : 5120 K

    GUN C++
    */
    #include
    using namespace std;


    int main()
    {
    int n,d,u,ians;
    while(cin>>n>>u>>d && (n!=0))
    {
    ians=0;
    while(n-u>0)
    {
    n=n-u+d;
    ians+=2;
    }
    ians++;
    cout<<ians<<endl;
    }
    return 0;
    }

    ReplyDelete
  9. Hello Dear,

    I did not have time to thank you enough for your help.

    Thank you ,

    Thank you so much.

    and added one more blog to my favourit list. ^^

    Your blog is FASCINATING

    ReplyDelete

Post a Comment