# Thread: Sum of k-digits in ^n

1. ## 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");
}

2. 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. 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");
}

4. Any solution guys?

5. 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?

6. 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. What is your code for the simplified problem?

9. 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

10. :
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. 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.)

12. Ok got it,but in my main programm what is wrong???and i get an error(I suck) -_-

13. 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.

14. Originally Posted by MoZak
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. 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