help with a c++ program

This is a discussion on help with a c++ program within the C++ Programming forums, part of the General Programming Boards category; Hello ! I need some help with this code(my program should display the last digit of the sum 2^a + ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    26

    help with a c++ program

    Hello !

    I need some help with this code(my program should display the last digit of the sum
    2^a + 3^a +...+9^a - where a is a number entered from the keyboard):
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int a,n,i,p = 1,uc,s = 0;
    	cout << "a = ";
    	cin >> a;
    	if (a == 0) cout << "8" << endl;
    	
    	for (n = 2;n <= 9;n++) {
    	for (i = 1;i <= n;i++) {
    	p = p*n;
    	uc = p%10;
    	p = p%10;
    	s = s+uc;
    	}
    	}
    	cout << s << endl;
    	return 0;
    }
    What's wrong with my code ? The program doesn't work.
    I would be grateful if you would explain to me what I did wrong.

    :

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    For one thing, none of your calculations involve a. You rather seem to be adding 2^1 + 2^2 + 3^1 + 3^2 + ... + 9^8 + 9^9 (except that you keep accumulating things to the same p).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    26
    Quote Originally Posted by anon View Post
    For one thing, none of your calculations involve a. You rather seem to be adding 2^1 + 2^2 + 3^1 + 3^2 + ... + 9^8 + 9^9 (except that you keep accumulating things to the same p).
    My first idea was to add the last digit of every term of the sum.
    By summing the digits I thought I could find out the last digit of the number given.
    For example,if a is 2 the number 2^4 + 3^4 = 6 + 1 =7.
    I now realise that for bigger numbers the sum will exceed 10 so I need to calculate the last digit of this one too.
    What should I do?

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,507
    Run a loop, varying n from 2 to 9 and adding pow(n,a) to the result each time.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    26
    Thank you ! It works now very well

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help converting array program to link list program
    By hsmith1976 in forum C++ Programming
    Replies: 0
    Last Post: 02-14-2010, 09:50 PM
  2. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  3. Replies: 48
    Last Post: 09-26-2008, 04:45 AM
  4. Replies: 5
    Last Post: 08-17-2007, 12:43 AM
  5. Replies: 18
    Last Post: 11-13-2006, 01:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21