So I have a homework wherein I have to convert integer numbers to roman numerals. The code I use is this:
#include <stdio.h>
int main(){
int N, i, a, num;
(1<=N<=3000);
scanf("%d", &N);
for(i=0; i<N; i ){
scanf("%d", &num);
while (num != 0){
if (num>=1000){
printf("M\n");
num -= 1000;
}
else if (num>=900){
printf("CM\n");
num -= 900;
}
else if (num>=500){
printf("D\n");
num -= 500;
}
else if (num>=400){
printf("CD\n");
num -= 400;
}
else if (num>=100){
printf("C\n");
num -= 100;
}
else if (num>=90){
printf("XC\n");
num -= 90;
}
else if (num>=50){
printf("L\n");
num -= 50;
}
else if (num>=40){
printf("XL\n");
num -= 40;
}
else if (num>=10){
printf ("X\n");
num -= 10;
}
else if (num>=9){
printf ("IX\n");
num -= 9;
}
else if (num>=5){
printf ("V\n");
num -= 5;
}
else if (num>=4){
printf ("IV\n");
num -= 4;
}
else if (num>=1){
printf ("I\n");
num -= 1;
}
}
**printf("\n");**
}
return 0;
}
but the result of the code would be this: result
I know I can remove the \n so it fixes the spacing, but we are required for the input to be in a new line, so if I remove the \n, the number I'll input will be next to the roman numeral instead of below it. For example, like this: result without \n
What I want it to do is for the roman numerals to be in one line, but I can't for the life of me find a way to do it.
solved: added "printf(\n)" after the while loop
CodePudding user response:
You can remove the \n
in all of the printf
, and then add a single printf("\n")
after the while
loop.
CodePudding user response:
Of course if it works:
#include <stdio.h>
int main(void)
{
int N, i, num;
scanf("%d", &N);
for(i=0; i<N; i )
{
scanf("%d", &num);
while (num != 0)
{
if (num>=1000)
{
printf("M");
num -= 1000;
}
else if (num>=900)
{
printf("CM");
num -= 900;
}
else if (num>=500)
{
printf("D");
num -= 500;
}
else if (num>=400)
{
printf("CD");
num -= 400;
}
else if (num>=100)
{
printf("C");
num -= 100;
}
else if (num>=90)
{
printf("XC");
num -= 90;
}
else if (num>=50)
{
printf("L");
num -= 50;
}
else if (num>=40)
{
printf("XL");
num -= 40;
}
else if (num>=10)
{
printf ("X");
num -= 10;
}
else if (num>=9)
{
printf ("IX");
num -= 9;
}
else if (num>=5)
{
printf ("V\n");
num -= 5;
}
else if (num>=4)
{
printf ("IV");
num -= 4;
}
else if (num>=1)
{
printf ("I");
num -= 1;
}
}
printf ("\n");
}
return 0;
}