简体   繁体   中英

How to produce the following output?

Since i'm new in the programming world ,i'm facing little problem while writting program for this pattern .I tried many times but the result is not what i wanted ? The pattern is :

 1 
 23 
 456 
 78910 

What i have written is :-

#include<stdio.h> 
#include<conio.h> 
void main() 
{ 
    int num = 1 , j = 1 , x = 1 , i = 1 ; 
    while( j <= 4 ) { 
        while( i <= num ) { 
            printf( "%d", x ) ; 
            x++ ; 
            i++ ; 
        } 
        num++ ; 
        i = ( i + 1 ) - num ; 
        j++ ; 
    } 
    getch() ; 
} 
#include <stdio.h>

int main()
{
    printf("1\n23\n456\n78910\n");
    return 0;
}

produces the output you desire

You need to print a newline after the inner loop:

#include<stdio.h> 
#include<conio.h>
int main() 
{ 
    int num = 1 , j = 1 , x = 1 , i = 1 ; 
    while( j <= 4 ) { 
        while( i <= num ) { 
            printf( "%d", x ) ; 
            x++ ; 
            i++ ; 
        } 
        printf("\n");
        num++ ; 
        i = ( i + 1 ) - num ; 
        j++ ; 
    } 
    getch();
    return(0);
} 

There is another example:

int main()
{
    int i, j, num = 1, line = 4;
    for(i = 1; i <=  line ; i++)
    {
        for(j = 0; j < i; j++)
        {
            printf("%d", num);
            num++;
        }
        printf("\n");
    }
    return 0;
}

With single loop:

# include<stdio.h>
# define LIMIT 100

int main(){
int i, prev=0, next=0, diff=1;
for(i=1;i<LIMIT;i++){
    printf("%d", i);next++;
    if(diff == next-prev){
        printf("\n");
        diff++;prev = next = 0;
    }
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM