mardi 27 novembre 2018

How to print 10 values per line in C where the values are in decreasing order and using only while loops and if-else-statements?

I want to write a program where the user can input a number BETWEEN 1 and 100. The program will then display that number with all numbers preceeding it until 1. Each line should have 10 numbers only. So basically it should look like:


The sample output


I used this code:


#include <stdio.h>
#include <conio.h>

void main(void) 
{

    int num, counter;
    counter=1;

    printf("Input a number between 1 and 100: ");
    scanf("%d", &num);

    printf("\n");

    if(num>1 && num<100)

        while(num>0)
        {
            printf("%d %d %d %d %d %d %d %d %d %d \n", num, num - 1, num - 2, num - 3, num - 4, num - 5, num - 6, num - 7, num - 8, num - 9);
            num -= 10;
        }

    else
        printf("\nInvalid value. Please enter a different number.");

    getch();
}


But at the end, it still keeps on showing 0 and negative numbers.

My output

How do I fix this?

Aucun commentaire:

Enregistrer un commentaire