How to display output after inputting some newline-separated integers without using arrays?

A task:

t

denotes the number of inputs followed by lines t

, each containing one integer n

. For each integer n

given in the input, display a string with a value n

.

Input example:

4
1
2
five
3

Output example:

1
2
five
6

The output should appear after all integer strings have been entered n

, i.e. it shouldn't display output after every line of input.

How can this be done with a loop while

without using an array to store the input numbers?

while(i<t)
{
    scanf("%d",&num);
    printf("%d",&num);
    i++;
}

      

This code works fine if the input numbers are n

separated by a space and displayed on one line. But when input numbers are provided after a new line, it displays the corresponding output after each input value.

+3


source to share


2 answers


Typically this type of input output is used in coding contention where the user is expected to match the expected output for the actual output.

Try pasting input using command line or online compilers and check it out. This is perfectly fine as the output is as expected.

How to do it if you specify general input right away.



It reads first t

and then reads num

and prints the number, but your print is actually after typing. Thus, the result is tested in a coding competition.

PS: If you want everything after input use arrays.

+1


source


If you want this without using an array, then the best way I can think of is recursion. But note that internally your values ​​will be stored in stack frames and your values ​​will be printed in reverse order (since the stack is LIFO). This is how you can do it,

void foo(i, t)
{
    if(t==i)
        return;
    int num;
    scanf("%d",&num);

    foo(++i, t);

    printf("%d\n",num);
}

      



Note, however, that the values ​​will be printed in reverse order since stack

- LIFO

+1


source







All Articles