Program to output as below formate:
1
2 3
4 5 6
7 8 9 10

Answers were Sorted based on User's Feedback



Program to output as below formate: 1 2 3 4 5 6 7 8 9 10..

Answer / jyoti

public class format {
public static void main(String args[])
{
int k=1;
for(int i=0;i<5;i++)
{
for(int j=0;j<i;j++)
{
System.out.print(k++ + " ");
}
System.out.print("\n");
}
}
}

Is This Answer Correct ?    15 Yes 3 No

Program to output as below formate: 1 2 3 4 5 6 7 8 9 10..

Answer / koushik sarkar

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

Is This Answer Correct ?    6 Yes 1 No

Program to output as below formate: 1 2 3 4 5 6 7 8 9 10..

Answer / debashree

In the above example one never knows how many lines are required to be print. So, to keep the program generic , we can implement it the following way:

public class FormattedOutput2 {

/**
* @param args
*/
public static void main(String[] args) {
printOutput(Integer.parseInt(args[0]), 1, 0);
}

private static void printOutput(int lastNum, int i, int num) {
int count = 0;
while(num < lastNum){
StringBuffer formatString = new StringBuffer();
while(count < i && num < lastNum){
formatString.append(++num + " ");
count++;
}
System.out.println(formatString);
i++;
count = 0;
}

}

}


Output for : 10
1
2 3
4 5 6
7 8 9 10

Output for: 102
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55
56 57 58 59 60 61 62 63 64 65 66
67 68 69 70 71 72 73 74 75 76 77 78
79 80 81 82 83 84 85 86 87 88 89 90 91
92 93 94 95 96 97 98 99 100 101 102

Is This Answer Correct ?    2 Yes 2 No

Program to output as below formate: 1 2 3 4 5 6 7 8 9 10..

Answer / dhananjaya nawarathne

public class formatNumbers {
public static void main(String args[])
{
int k=1;
for(int i=1;i<5;i++)
{
for(int j=0;j<i;j++)
{
System.out.print(k++ + " ");
}
System.out.print("
");
}
}
}

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Core Java Interview Questions

Explain the importance of thread scheduler in java?

0 Answers  


What are the properties of thread?

0 Answers  


Justify your answer that you can't define a method inside another method in java, if you can then how?

0 Answers  


What are structs in java?

0 Answers  


What is the use of beaninfo?

0 Answers  






What is the difference between quicksort & mergesort? When should they be used? What is their running time?

0 Answers   Akamai Technologies,


can u give one sinario when you use Abstract Class and When you use Interface.

5 Answers   ITC Infotech,


Write an algorithm program in java for the following question.. In a VLSI design techniques,they used rectangles to design circuits. EVery rectangle is to be placed according to x,y coordinates. Check whether or not two rectangles overlap each other. Here overlapping of rectangles is acceptable, if 1) one rectangle intersect with other. 2) one rectangle fully covers other. The time of algorithm should not exceed o(n logn).

0 Answers  


what is multi-tasking

3 Answers   Anand Group, Photon Infotech, Tech Mahindra,


what is the Yield() method used in threads?

4 Answers   Accenture,


describe method overloading

0 Answers  


What is the instance of an object?

0 Answers  


Categories