Sunday, March 17, 2013

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;
}

 

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




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

12 comments:

  1. You have amazing blog

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

    regards,

    ReplyDelete
    Replies
    1. Post the questions ...I will try to help you

      Delete
  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. // lexicographical_compare example
    #include // std::cout, std::boolalpha
    #include // std::lexicographical_compare
    #include // std::tolower

    // a case-insensitive comparison function:
    bool mycomp (char c1, char c2)
    { return std::tolower(c1)<std::tolower(c2); }

    int main () {
    char foo[]="Apple";
    char bar[]="apartment";

    std::cout << std::boolalpha;

    std::cout << "Comparing foo and bar lexicographically (foo<bar):\n";

    std::cout << "Using default comparison (operator<): ";
    std::cout << std::lexicographical_compare(foo,foo+5,bar,bar+9);
    std::cout << '\n';

    std::cout << "Using mycomp as comparison object: ";
    std::cout << std::lexicographical_compare(foo,foo+5,bar,bar+9,mycomp);
    std::cout << '\n';

    return 0;
    }

    ReplyDelete
  9. Problem 4: Subset Sums
    Here is a recursive solution - ill post the code, then explain what is going on. Also, this is code I wrote for brute forcing a knapsack cipher, so there is a little more going on then is necessary to understand (the whole set bit business.)

    int set_bit(int solution, int bit)
    {
    int a = (1 << bit);
    return (solution+a);
    }
    //the int returned by subset sum has the bit values set to the solution
    // if -1, there was no solution
    int subset_sum(int s, int a_index, int solution)
    {
    //to do - make bits work right
    //we need a successful add to make bits on the left side
    if (s <0)
    return -1;
    if (s == 0)
    return solution;
    if (a_index == 16)
    return -1;
    int included = subset_sum( s - hard[a_index], a_index+1, set_bit(solution, a_index) );
    int not_included = subset_sum( s - hard[a_index], a_index+1, solution );
    if (included >= 0)
    return included;
    return not_included;
    }
    **explanation time:
    The subset_sum function takes a target sum and the current place in our array, a_index. the solution to the subset sum problem is:
    1) include the current item in our solution - the solution is the sum minus the current item, plus the solution to the rest of the array.
    2) do not include the current item in our solution - the solution is the solution to the sum and rest of the array

    base cases: we included an item that made the sum negative, return -1
    we ran out of array and did not meet the sum, return -1
    we have met the sum, return solution.

    in my code, solution is a 16 bit integer, where each one represents include the item. ie 7 = 00000111, the last 3 items made the solution.

    ReplyDelete
  10. /*
    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
  11. 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