This program will print a vertical histogram. This program will ask 10 values from user and will print a vertical histogram according to the values entered.
class VerticalHistogram
{
public static void main(String args[])
{
int max=0;
Console con=System.console();
System.out.println("Enter 10 values: ");
int val[]=new int[10];
for(int i=0;i<10;i++)
{
System.out.print("Value "+(i+1)+": ");
val[i]=Integer.parseInt(con.readLine());
System.out.print("\n");
if(val[i]>max)
{
max=val[i];
}
}
for(int i=max;i>=0;i--)
{
for(int j=0;j<10;j++)
{
if((i-val[j])>=0)
{
System.out.print(" ");
System.out.print(" ");
}
else
{
System.out.print("*");
System.out.print("*");
}
System.out.print(" ");
}
System.out.println("");
}
System.out.print("Numbers Entered\n");
System.out.print(" ");
for(int j=0;j<10;j++)
{
System.out.print(" "+val[j]);
}
}
}
Source code:
import java.io.*;class VerticalHistogram
{
public static void main(String args[])
{
int max=0;
Console con=System.console();
System.out.println("Enter 10 values: ");
int val[]=new int[10];
for(int i=0;i<10;i++)
{
System.out.print("Value "+(i+1)+": ");
val[i]=Integer.parseInt(con.readLine());
System.out.print("\n");
if(val[i]>max)
{
max=val[i];
}
}
for(int i=max;i>=0;i--)
{
for(int j=0;j<10;j++)
{
if((i-val[j])>=0)
{
System.out.print(" ");
System.out.print(" ");
}
else
{
System.out.print("*");
System.out.print("*");
}
System.out.print(" ");
}
System.out.println("");
}
System.out.print("Numbers Entered\n");
System.out.print(" ");
for(int j=0;j<10;j++)
{
System.out.print(" "+val[j]);
}
}
}
No comments:
Post a Comment