81.7k views
1 vote
problem 2 (38 points): write a c program to prompt the user to enter a number between 1 and 15462. the final goal is to display the individual digits of the numbers on a line with three spaces between the digits. the first line is to start with the right-most digit and print it five times; the second line is to start with the second digit from the right and print it four times, and so forth. in order to do that, you must first separate the digits. no conversion from string to integers is allowed, neither are arrays. for example, if 1234 is entered, the following should be printed: 4 4 4 4 4 3 3 3 3 2 2 2 1 1 0 then, write c code to accomplish a similar print, where the digits are displayed flush left and in order. again, the digits must be separated programmatically. no conversion from string to integers is allowed, neither are arrays. for example: 0 1 2 3 4 0 1 2 3 0 1 2 0 1 0

1 Answer

4 votes

#include <bits/stdc++.h>

std::string f;

void fi(int d) {

std::cout << "First step:\\";

if(d<5) {

for(int i=5-d; i>0; i--) {

f.insert(0,"0");

}

}

for(int n=5;n>0;n--) {

for(int m=n;m>0;m--) {

std::cout << f.at(n-1) << " ";

}

}

std::cout << std::endl;

}

void se(int d) {

std::cout << "Second step:\\";

int n = 5;

if(d<5) {

for(int i=5-d; i>0; i--) {

f.insert(0,"0");

}

}

do{

for(auto const& m: f) {

std::cout << m << " ";

}

f.pop_back();

n--;

}while(n>0);

std::cout << std::endl;

}

int main(int argc, char* argv[]) {

std::cin>>f;

fi(f.size());

se(f.size());

return 0;

}

problem 2 (38 points): write a c program to prompt the user to enter a number between-example-1
User Olli K
by
6.0k points