0
How to print replication of number in C ?
When I am giving the number 1 it should get replicated as per the user requirement.. When I am entering 4 the number 1 should get replicated 4 times like Enter the number of times : 4 1111
1 ответ
0
Use a for loop:
#include <stdio.h>
int main() {
int length;
scanf("%d", &length);
for (int i=0; i<length; i++)
printf("%d", 1);
return 0;
}
Or if you actually want to store the replicated number as an integer:
#include <stdio.h>
int main() {
int replicated_num, length;
scanf("%d", &length);
for (int i=0; i<length; i++)
replicated_num += pow(10, i);
printf("%d", (int)replicated_num);
return 0;
}
You could also do:
replicated_num += {num} * pow(10, i);
and change num to the number you want to repeat