WEshruth
WEshruth

Reputation: 777

How can i print numbers on single line

Silly doubt, how can i print numbers in sequence in C programming according to its variable value. my Code

#include <stdio.h>
void main()
{  
   int i,j,result;
   for (i=1;i<=4;i++)
   {
     for (j=i;j<=i;j++)
     {
       printf("%d\n%d",i,j+1);
     }
 }
}

getting output as

1
22
33
44

Expecting answer is:

1
22
333
4444

Upvotes: 0

Views: 2139

Answers (3)

Waqar
Waqar

Reputation: 9331

#include <stdio.h>
void main()
{  
   int i,j,result;
   for (i=1;i<=4;i++)
   {
     for (j=i;j<=i;j++)
     {
       printf("%d",i);
     }
     printf("\n");
   }
}

Upvotes: 0

Netherwire
Netherwire

Reputation: 2787

It could help you. Note that in C array indexing starts at 0, not 1.

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

Upvotes: 1

MOHAMED
MOHAMED

Reputation: 43528

void main()
{  
   int i,j,result;
   for (i=1;i<=4;i++)
   {
     for (j=1;j<=i;j++)
     {
       printf("%d",i);
     }
     printf("\n");
   }
}

Upvotes: 4

Related Questions