Thread: Sum of k-digits in ^n

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    17

    Sum of k-digits in ^n

    Hello guys how do i seperate a uknown number to find his digits?
    I've done the work for a 4-digits number in ^4.
    Now i should do the same for k-digits in ^n.
    I should this k times???i tried with array but got confused...
    :
    Code:
    #include <stdio.h>
    #include <cstdlib>
    
    // k =psifia n=dunami
    main()
    {
          int i,j,ar1,ar2,ar3,ar4,Din1,Din2,Din3,Din4,x,k,n,l,p_orio,d_orio,p;
          printf("Give  k and to n\n");
          scanf("%d %d",&k,&n);
          p_orio=1;
          d_orio=1;
          for (l=1; l<k; l++){
              p_orio=10*p_orio;
              }
              for(p=1; p<=k; p++){
                       d_orio=10*p_orio-1;
                             }
          printf("print %d %d\n",p_orio,d_orio);
          for(i=p_orio; i<=d_orio; i++){
          Din1=1;
          Din2=1;
          Din3=1;
          Din4=1;
                   ar1=(i/1000);
                    x=(i%1000);
                    ar2=(x/100);
                    x=(x%100);
                    ar3=(x/10);
                    x=(x%10);
                    ar4=x;
                    for (j=1; j<=n; j++){
                       Din1=Din1*ar1;
                       Din2=Din2*ar2;
                       Din3=Din3*ar3;
                       Din4=Din4*ar4;
                        }
                        if (i==Din1+Din2+Din3+Din4){
                        printf(number raised in %d  are: %d\n",n,i);
                        }
                        }
                        system("pause");
                        }
    Help please xD

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    long long unknown;  // user input
    int digits[16];
    int idx = 0;
    
    while (unknown > 0)
      { digits[idx] = unknown %10;
          unknown /= 10;
          idx++; }
    At the end of the loop, each digit from unknown is in a separate array slot.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    17
    The problem asks to print all number that are equal to their digits ^n
    e.g 1634=1^4+6^4+3^4+4^4...So it should show all numbers from 1000 to 9999...
    I did this and the programm stops working :
    Code:
    #include <stdio.h>
    #include <cstdlib>
    
    // k =psifia n=dunami
    main()
    {
          int i,j,ar1,ar2,ar3,ar4,Din1,Din2,Din3,Din4,x,k,n,l,p_orio,d_orio,p,dig[16],Din[16],sum,id=0;
          printf("Dwse to k kai to n\n");
          scanf("%d %d",&k,&n);
          p_orio=1;
          d_orio=1;
          for (l=1; l<k; l++){
              p_orio=10*p_orio;
              }
              for(p=1; p<=k; p++){
                       d_orio=10*p_orio-1;
                             }
          printf("emfanise %d %d\n",p_orio,d_orio);
          for(i=p_orio; i<=d_orio; i++){ //in case i add k=4,n=4 p_orio=1000 d_orio=9999
                        while (i>0){
                              dig[id]=i%10;
                              i/=10;
                              id++;
                              }
         
                    for (j=1; j<=n; j++){
                    Din[j]=1;
                    }
                      for (j=1; j<=n; j++){
                    
                      Din[j]=Din[j]*dig[id];
                      sum=sum+Din[j];
                        }
                        if (i==sum){
                        printf("oi arithmoi einai pou einai upswmenoi stin %di  dynami einai oi: %d\n",n,i);
                        }
                        }
                        system("pause");
                        }
    Last edited by MoZak; 11-20-2011 at 05:40 AM.

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    17
    Any solution guys?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Simplify the problem: write a program to print the digits of a number, with each digit separated by a space. You may print the digits in reverse order. What's your solution to this?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    17
    I'll do and this buy why do i get an error??When i printf dig[id] it saws smth like that:
    24422424424
    200023
    -0
    4242422442
    23230223
    0
    0
    00
    0
    0
    and then it stops working by getting an error

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your code for the simplified problem?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jan 2011
    Posts
    17
    I am stuck with this don't want to get more confused...please help me at this.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Sorry, if you are so stuck that you cannot work on a simpler problem to get unstuck, then you must remain stuck, unless a flash of insight from your own mind saves the day. Law of the universe

    You see, if you can write a loop to separate a number into its component digits, you can then apply the "raise to power of 4 and add the results together" part. What I am trying to get you to do is to leave out the other part so that you can see clearly how to do the "separate a number into its component digits" part. If you insist on doing both together, then what I can tell you is that... you should write a loop to separate a number into its component digits, raise each component digit to the fourth power and then add it to a running total. Easy
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jan 2011
    Posts
    17
    :
    Code:
     #include<stdio.h>
     main()
    {
      int n  , i ;
      int dig[15] , top = 0;
      printf("Enter the number :");
      scanf("%d" , &n);
      while( n > 0 )
      {
            i = n % 10;
            dig[id] = i;
            n = n / 10;
    
      }
      while(dig)printf("%d " , dig[id]);
    }
    Is this right?????

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Goody. Unfortunately you have an infinite loop because dig never changes, and indeed it is an array so that is to be expected. Here's your program, adapted to work:
    Code:
    #include<stdio.h>
    
    int main(void)
    {
        int number;
        int digit;
        printf("Enter the number: ");
        scanf("%d", &number);
        while (number > 0)
        {
            digit = number % 10;
            printf("%d ", digit);
            number /= 10;
        }
        printf("\n");
        return 0;
    }
    Observe that I am not saving the digits in the array because I don't need to retrieve the digits later. Likewise, for the calculation that you are going to make, you don't need all the digits after the loop is done: you only need to result of the calculation.

    (There is also an assumption that the number is positive, or at least non-negative, but this is probably reasonable.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Jan 2011
    Posts
    17
    Ok got it,but in my main programm what is wrong???and i get an error(I suck) -_-

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Wait. Here's one idea: extend the simplified version. Instead of printing the digit with a space, perform the calculation that you want to perform.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by MoZak View Post
    Ok got it,but in my main programm what is wrong???and i get an error(I suck) -_-
    Laserlight is giving you extremely good advice here...

    Are you even trying her examples?

  15. #15
    Registered User
    Join Date
    Jan 2011
    Posts
    17
    I am trying my friend be patient xDDD.
    I understand how she is trying to help me but my mind can't reach hers xDD

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with digits
    By jrice528 in forum C++ Programming
    Replies: 4
    Last Post: 10-03-2007, 06:04 PM
  2. get all the right digits
    By mag_chan in forum C Programming
    Replies: 6
    Last Post: 11-27-2005, 06:16 AM
  3. 100,000 digits of pi
    By The Brain in forum A Brief History of Cprogramming.com
    Replies: 102
    Last Post: 11-24-2004, 12:31 AM
  4. Two digits
    By cyberCLoWn in forum C++ Programming
    Replies: 4
    Last Post: 02-23-2004, 01:53 PM
  5. Getting digits from int.
    By aker_y3k in forum C++ Programming
    Replies: 8
    Last Post: 02-21-2003, 12:45 PM

Tags for this Thread