| |

VerySource

 Forgot password?
 Register
Search
View: 670|Reply: 7

How to print 1 + 11 + 111 + 1111 + …… + 111111111 =?

[Copy link]

1

Threads

4

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-5 08:40:01
| Show all posts |Read mode
I have a question to ask:
Ask to print:
1 + 11 + 111 + 1111 + ... + 111111111 =?
2 + 22 + 222 + 2222 + ... + 222222222 =?
3 + 33 + 333 + 3333 + ... + 333333333 =?
...
...
9 + 99 + 999 + 9999 + ... + 999999999 =?

The code I wrote is as follows:
main ()
{
int i, j;
long f1, f2, sum = 0;
for (i = 1; i <= 9; i ++) {
f1 = i;
printf ("sum =");
for (j = 1; j <= 9; j ++) {
f2 = f1 * 10 + i;
sum + = f1;
if (j == 9) printf ("% d =", f1);
else printf ("% d +", f1);
f1 = f2;
}
printf ("% d\n", sum);
}
}

I wonder what went wrong? Please help me!
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-5 12:33:01
| Show all posts
The data is too big to overflow
Reply

Use magic Report

2

Threads

9

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-1-5 14:54:01
| Show all posts
Note the statement inside the first loop: sum = 0;
Otherwise it will add up
Reply

Use magic Report

2

Threads

9

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-1-5 15:06:01
| Show all posts
#include <stdio.h>
int main ()
{
int i, j;
long f1, f2, sum = 0;
for (i = 1; i <= 9; i ++)
{
f1 = i;
sum = 0; // plus
printf ("sum =");
for (j = 1; j <= 9; j ++)
{
f2 = f1 * 10 + i;
sum + = f1;
if (j == 9) printf ("% d =", f1);
else printf ("% d +", f1);
f1 = f2;
}
printf ("% d\n", sum);
}
return 0;
}
Reply

Use magic Report

1

Threads

4

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-1-6 09:03:01
| Show all posts
Still not working, it seems like data overflow, how to do it?
Reply

Use magic Report

1

Threads

4

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-1-6 09:33:01
| Show all posts
What does the last return 0; mean? Thank you!
Reply

Use magic Report

0

Threads

5

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-6 10:03:01
| Show all posts
Returns a number to the main function,
Reply

Use magic Report

0

Threads

5

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-6 10:09:01
| Show all posts
#include <stdio.h>
int main ()
{
int i, j;
long int f1, f2, sum = 0;
for (i = 1; i <= 9; i ++)
{
f1 = i;
sum = 0;
printf ("sum =");
for (j = 1; j <= 9; j ++)
{
f2 = f1 * 10 + i;
sum + = f1;
if (j == 9) printf ("% ld =", f1);
else printf ("% ld +", f1);
f1 = f2;
}
printf ("% ld\n", sum);
}
return 0;
}
Improved, that is, all the numbers are output using ld long shaping
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list