Pregunta

Just started to learn the nested loop logic in C Programming, and i don't know how to achieve in C Programming, the length and row should follow the input number, and use '-' to separate data

Enter number: 2
-1-3
----

Enter number: 7
-1-3-5-7-9-1-3
--------------
-1-3-5-7-9-1-3
--------------
-1-3-5-7-9-1-3
--------------
-1-3-5-7-9-1-3

Anyone knows how to achieve it? thanks for your help.

¿Fue útil?

Solución

#include <stdio.h>

int main(){
    int i,j,n;

    printf("Enter number: ");
    scanf("%d", &n);
    for(i=0;i<n;++i){
        for(j=0;j<2*n;++j){
            if(i%2==0 && j%2==1)
                printf("%d", j % 10);
            else
                printf("-");
        }
        printf("\n");
    }
    return 0;
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top