I'm fairly new to java. This is how the output to my problem should be:
Enter a number between 5 and 20:
5.....5 stars* on the first line. 4 stars on the sec. 3 on the next and so on one star on the last line.
I did everything but I can't get the stars to print that way, here is my code:
int number;
int num_stars;
Scanner num = new Scanner(System.in);
System.out.println("Enter a number between 5 and 20"); user to enter a
number = num.nextInt();
for(int i= 5; i >= number; i--)
{
// inner loop to handle number of columns
// values changing acc. to outer loop
for(int j = 20; j >= i; j--)
{
// printing stars
System.out.print("* ");
}
// ending line after each row
System.out.println();
}
thank you for your time
try this:
for (int i=number;i>0;i--){
for(int j=i;j>0;j--){
System.out.print("*");
}
System.out.println( );
}
but try this kind of exercises to solve on your own. It builds up your logic.
you can try fallowing code.. work for me..
int number;
int num_stars;
System.out.println("Enter a number between 5 and 20");
Scanner s = new Scanner(System.in);
number = Integer.valueOf(s.nextLine());
num_stars=number;
for (int i = 1; i <= number; i--) {
for (int j = 0; j < num_stars; j++) {
System.out.print("*");
}
num_stars--;
System.out.println();
}
System.out.println("over");
}
Related
I've been looking for different solutions out here but on my code they won't work. I'm getting an input from a user but then it doesn't display what's inside the if-else statement. I tried to do a different code for the if statement but it doesn't work. The int input works but it doesn't work in the string input.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int numbers [] = new int[10];
int temp;
String sc = " ";
Scanner scan = new Scanner(System.in);
// Loop through array
// Change value of each array per iteration
System.out.print("Enter 10 integers: ");
for(int i = 0; i < numbers.length; i++){
numbers[i] = scan.nextInt();
}
System.out.print("\nPlease Choose Among The Following:");
System.out.println("\n A - Display the numbers \t B - Display the values of even indexes \n C - Display the values of odd indexes \t D - Display the values in ascending order \n E - Display the values in descending order");
System.out.print("\nEnter The Letter of Your Choice: ");
sc = scan.nextLine();
if (sc.equals('A') || sc.equals('a')){
System.out.print("Display each item in array");
for(int number: numbers){
System.out.println(number);
}}
else if (sc.equals('B') || sc.equals('b')){
System.out.println("Display odd indexes");
for(int i = 0; i < numbers.length; i++){
if(i % 2 == 0){
System.out.println(numbers[i]);
}
}}
else if (sc.equals('C') || sc.equals('c')){
System.out.println("Display even indexes");
for(int i = 0; i < numbers.length; i++){
if(i % 2 == 1){
System.out.println(numbers[i]);
}
}}
else if (sc.equals('D') || sc.equals('d')){
int [] ascendingList = numbers.clone();
System.out.println("Display in ascending order using bubble sort");
for(int i = 0; i < ascendingList.length - 1 ; i++){
for(int j = 0; j < (ascendingList.length - i - 1); j++){
if(ascendingList[j] > ascendingList[j+1]){
temp = ascendingList[j];
ascendingList[j] = ascendingList[j+1];
ascendingList[j+1] = temp;
}
}
}
for(int number: ascendingList){
System.out.println(number);
}}
else if (sc.equals('E') || sc.equals('e')){
int [] descendingList = numbers.clone();
System.out.println("Display in descending order using bubble sort");
for(int i = 0; i < descendingList.length - 1 ; i++){
for(int j = 0; j < (descendingList.length - i - 1); j++){
if(descendingList[j] < descendingList[j+1]){
temp = descendingList[j];
descendingList[j] = descendingList[j+1];
descendingList[j+1] = temp;
}
}
}
for(int number: descendingList){
System.out.println(number);
}}
}
}
I have to write a program so that it prints a triangle like the following:
If the user gives Input 3, the output should be:
3
23
123
If the user gives Input 4, the output should be:
4
34
234
1234
So I wrote the following code:
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of rows");
int row=sc.nextInt();
for(int maincount=1; maincount<=row;maincount++){
for(int spcount=1;spcount<=row-maincount;spcount++){
System.out.print(" ");
}
//int numprint=row;
for(int numcount=1;numcount<=maincount;numcount++){
System.out.print(/*numprint*/numcount);
//numprint--;
}
System.out.println("");
}
But the output to the code is something like this:
If I input 3:
1
12
123
It looks very close to the output, so tried to find the problem
I used another variable "numprint" instead of "numcount". (I commented out The parts where I used numprint); But this time the output was:
3
32
321
The alternatives I came up with I couldn't find any working solution that would show an output similar to the desired one.
I think this is what you want:
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of rows");
int row=sc.nextInt();
for (int i=1; i<row+1; i++) {
for (int j=1; j<row+1; j++) {
if (j > row - i) {
System.out.print(j);
} else {
System.out.print(" ");
}
}
System.out.println();
}
The outer iteration using i assures 2 things:
To print the sequence of numbers using the inner iteration
Jump on the next line using System.out.println();
The inner iteration using j assures only printing the sequence of numbers. The key is to determine whether print the empty space " " or the number. j > row - i is the condition.
You can try this
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of rows");
int row=sc.nextInt();
for (int i = 1; i <= row; i++) {
for(int j = 0; j < row - i; j++) {
System.out.print(" ");
}
int temp = row - (i -1);
for (int j = 0; j < i; j++) {
System.out.print(temp++);
}
System.out.println("");
}
Try this:-
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of rows");
int row=sc.nextInt();
for (int maincount = 1; maincount <= row; maincount++) {
for (int spcount = 1; spcount <= row - maincount; spcount++) {
System.out.print(" ");
}
int numprint = row;
String str = "";
for (int numcount = 1; numcount <= maincount; numcount++) {
str = numprint + str;
numprint--;
}
System.out.print(str);
System.out.println("");
}
Hello All,
I was wondering if someone could help me with making a number triangle in Java that looks like the one below using nested while loops. Would someone be able to help me out?
4
56
789
1234
56789
I have a variable 'i' on the outer loop determining how many rows the triangle will be and a variable 'j' on the inner loop determine which number the triangle will begin with. the numbers have to stay between [1-9].
Can anyone help me out?
Try This, It Will Work... It accepts rows & number through user and in first for loop it runs the loop till the number of rows and second loop print the number as per the value of I in pattern and if condition to check if the number is 10 then reset the number with 1 to start the numbering again.
import java.util.Scanner;
public class Pattern {
public static void main(String[] args) {
int rows, number = 1;
Scanner sc = new Scanner(System.in);
System.out.println("Enter no of rows");
rows = sc.nextInt();
System.out.println("Enter no to start with");
number = sc.nextInt();
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(number + " ");
++number;
if (number == 10) {
number = 1;
}
}
System.out.println();
}
}
}
Try.. r is for number of rows and v is the value
Scanner sc = new Scanner(System.in);
int r = sc.nextInt();
int v = sc.nextInt();
int i = v - 1;
int j = 1;
while(j != r + 1){
int k = 0;
int ans = 0;
while( k < j){
i = i + 1;
if(i == 10){
i = 1;
}
ans = ans * 10 + i;
k = k + 1;
}
System.out.println(ans);
j = j + 1;
}
The question I am working on is:
A square with the side length n is filled with numbers 1,2,3,...n2 is a magic square if the sum of elements in each row, column, and two diagonals is the same value.
Write a program that reads in a value of 16 values from the keyboard and tests whether they form a magic square when put into a 4 x 4 array. You need to test the following 2 features:
Does each number of 1,2,...16 occur in user input? Tell the user to try again if they enter a number that they've already entered.
When the numbers are put into a square, are the sums of rows, columns, and diagonals equal to each other?
It must be done using two-dimensional array
I am having trouble with asking the user to try again if they enter a number that they have previously entered. And, numbers in 4 x 4 do not print.
What am I doing wrong? How can I fix it?
This is the code I have so far:
Scanner in = new Scanner (System.in);
int n =4;
int[][] square = new int[n][n];
int number = 0;
int num = 0;
for (int i = 0; i <16; i++){
number = num;
System.out.print ("Enter a number: ");
num = in.nextInt();
int num_2 = 0;
if (number==num || number==num_2) {
System.out.println ("Try again.");
System.out.println ("Enter a number: ");
num_2 = in.nextInt();
}
if (num > 16){
System.out.println ("Try again.");
break;
}
}
for (int i= 0; i < n; i++){
for (int j = 0; j < n; j++) {
num+=square [i][j];
System.out.print(square[i][j] + "\t");
}
}
}
}
You can try this code for 1st feature and add code for 2nd one.
int ar[][] = new int[4][4];
System.out.println("Enter Numbers");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
boolean flag = false;
int num = sc.nextInt();
if (num > 16 || num < 1) {
System.out.println("Please Enter number between 1 to 16");
flag=true;
j--;
} else {
for (int k = 0; k <= i; k++) {
for (int l = 0; l <= j; l++) {
if (ar[k][l] == num) {
System.out.println("This number already inserted...Please give another");
j--;
flag = true;
}
}
}
}
if (!flag) {
ar[i][j] = num;
}
}
}
If you can't understand anything please ask.
Hope this help.
I am having difficulties with completing this program. I am trying to make a program that creates asteriks, but then makes it into a triangle.
This is what I have already.
public class 12345 {
public static void main(String[] args) {
int n = 0;
int spaces = n;
int ast;
System.out.println("Please enter a number from 1 - 50 and I will draw a triangle with these *");
Scanner keyboard = new Scanner(System.in);
n = keyboard.nextInt();
for (int i = 0; i < n; i++) {
ast = 2 * i + 1;
for (int j = 1; j <= spaces + ast; j++) {
if (j <= spaces)
System.out.print(' ');
else
System.out.print('*');
}
System.out.println();
spaces--;
}
}
}
It is creating the asteriks, but how would I be able to continue them where they make a triangle... so they get bigger as they go, and then back smaller...
Thank you in advance!
Try moving
int spaces = n;
to AFTER the value of n is read from stdin.
This solves half your problem and hopefully gets you on the right track.
I added a few things to your code and got it to print the full triangle, where the number input in the scanner will be the number of asterisks printed in the bottom row. I.e. if the input is 3, the triangle will be two rows of 1->3; if the input is 5 then the triangle will be 3 rows of 1->3->5, and so on.
public static void main(String[] args) {
int ast;
int reverse = 1;
System.out.println("Please enter a number from 1 - 50 and I will draw a triangle with these *");
Scanner keyboard = new Scanner(System.in);
int spaces = keyboard.nextInt();
for (int i = 0; i < spaces; i++) {
ast = 2 * i + 1;
for (int j = 1; j <= spaces + ast; j++) {
if (j <= spaces) {
System.out.print(' ');
} else {
System.out.print('*');}
if (j > spaces + ast) {
for (int k = 0; k < spaces-(reverse-1); k++) {
System.out.print(' ');
}
}
int k = 0;
reverse++;
}
System.out.println();
spaces--;
}
}
}
I added another if statement after your if-else that triggers when the variable j exceeds the first loop condition. This triggers another loop that makes the output lines symmetrical by essentially repeating your first if statement.
I hope this helps =)