How to print replication of number in C ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

24th Oct 2018, 7:31 AM
Dhiviya Thirumavalavan
Dhiviya Thirumavalavan - avatar
1 Answer
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
24th Oct 2018, 7:48 AM
jtrh
jtrh - avatar