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;
}
Related
hello I'm a newbie and I am having a hard time doing this program. I was wondering if there is any work around to get the value in the else statement be used outside the statement itself or any other way for this to work. The program is suppose to stop when there are 10 odd numbers in the array which i tried to do by putting it in a do while loop and assigning variable k to add 1 value if the the else statement detects an odd number but the value of k is only usable in the else statement.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num[] = new int[100];
System.out.println("This program stops when 10 odd numbers are entered.");
do {
int j = 0;
int k = 0;
for (int i = 0; i <= num.length; i++) {
System.out.println("Please enter element " + i);
num[j] = sc.nextInt();
if (j % 2 == 0)
System.out.println("Entered number is even");
else
k = k + 1;
}
} while (k != 11);
}
}
In java you can't access variables that were declared in an inner scope { } from a parent scope. Which means that in this example:
if(x==2) {
int k = 10;
} else {
int j = 300;
}
You can't access k nor j.
In your code, since k was declared in the do while scope, it can be accessed from the if else statement. The problem is that since k was declared in the do while scope, every time the do is executed your k redefined with value 0.
The solution is to declare k in a parent scope i.e. the main scope:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num[] = new int[100];
int k = 0;
int j = 0;
System.out.println("This program stops when 10 odd numbers are entered.");
do {
for (int i = 0; i <= num.length; i++) {
System.out.println("Please enter element " + i);
num[j] = sc.nextInt();
if (num[j] % 2 == 0)
System.out.println("Entered number is even");
else
k = k + 1;
j = j + 1;
}
} while (k != 11);
}
}
Also, I think that you forgot to increment your j, which is also will redefined in every while, and in the if statement you check if j % 2 == 0 and not the actual number that came from the user. Good Luck !
Th problem in your code is scope of the variable k which available only inside the do-while loop but the while (k != 11); is outside the loop.
So, you must declare the k outside the loop.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num[] = new int[100];
int k = 0;
System.out.println("This program stops when 10 odd numbers are entered.");
do {
int j = 0;
for (int i = 0; i <= num.length; i++) {
System.out.println("Please enter element " + i);
num[j] = sc.nextInt();
if (j % 2 == 0)
System.out.println("Entered number is even");
else
k = k + 1;
}
} while (k != 11);
}
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 have problem with the following question:
Write a program that prompts the user to enter an integer, n. The
program will print the following results:
Sum of all even numbers between 1 and n (inclusive)
Sum of all odd numbers between 1 and n (inclusive)
Here the code I have so far, when I hit run I am getting exponentially large numbers that keep multiplying. I am aware my code is wrong. I dont know what I am doing wrong. Thank you.
package assig;
import java.util.Scanner;
public class Assignment4_Question1 {
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
System.out.println("Enter an integer: ");
int n = in.nextInt();
int evenSum = 0;
int oddSum= 0;
for (int i = 1; 1<=n; i++){
if(i % 2 == 0){
evenSum = evenSum + i;
} else if (i % 2 != 0){
oddSum = oddSum + i;
}
System.out.println(evenSum);
System.out.println(oddSum);
}
}
You loop is never ending !
for (int i = 1; 1<=n; i++)
So you are saying the loop will go infinte if you n is bigger than 1!
for (int i = 1; i<=n; i++)
The difference in here that you loop will go until i reach to n.
You must change :
for (int i = 1; 1<=n; i++)
to:
for (int i = 1; i<=n; i++)
I am trying to make a triangle of prime numbers .. the number of rows will be user defined
for example if user gives "4" as number of rows then the program should show a triangle of prime numbers containing 4 rows
input :4
output :
2
3 5
7 11 13
17 19 23 29
These are two tasks: get the list of primes (or just something like nextPrime(int)) and get the triangle, both are very simple.
For the implementation of first, you may look at Next Prime number Java only working with certain numbers
For the implementation of second, just do something like:
for (int i = 1; i <= n; ++i) {
for (int j = 0; j < i; ++j) {
nextPrime = ...
System.out.print (nextPrime + " ");
}
System.out.println ();
}
Let say you already have your primes (sufficient amount).
int[] primes = ...;
Then for N strings create array is StringBuilders and fill them up with your numbers:
StringBuilder[] builders = new StringBuilder[N];
int count = 0;
for(int row = 0; row < N; row++) {
int size = row + 1;
builders[row] = new StringBuilder();
for(int i = 0; i < size; i++) {
builders[row].append(primes[count++]);
builders[row].append(' ');
}
}
And then you may print them
for(StringBuilder sb : builders)
System.out.println(sb);
//please try these
import java.util.*;
public class primePtt1
{
public static boolean isPrimeNumber(int num) {
int c=0;
for (int i = 1; i <= num; i++) {
if (num % i == 0)
c++;
}
if (c==2)
return true;
else
return false;
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter no. of rows : ");
int rows = sc.nextInt();
int counter = 2;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
/* Try to find next prime number by incrementing counter and testing it for primality */
while(!isPrimeNumber(counter)){
counter++;
}
System.out.print(counter+" ");
counter++;
}
System.out.println();
}
}
}
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 =)