Im trying to create a descending triangle of numbers like this in Java:
4
3 3
2 2 2
1 1 1 1
The user inputs the first number and then it's supposed to create the descending triangle and honestly I don't know how to figure it out.
I haven't really tried that much I'm just lost :)
Here's my code so far:
public static void numberlines(){
Scanner in = new Scanner(System.in);
System.out.println("Welcome to number lines, enter a number and I'll give you some other numbers in a line...");
int usernum;
int counter = 0;
int count = 0;
char tab = 9;
int i;
usernum = getInt();
while (counter != usernum){
if (usernum > counter) {
usernum--;
System.out.println(+ usernum);
}else if (usernum < counter){
usernum++;
System.out.println(+usernum);
}
}//while
}//numberlines
Right now it just prints the descending line of numbers but I'm pretty sure there's a lot more to it. If anyone has any suggestions or ideas that would be awesome. Thanks
You can do this by nested while loops:
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.println("Welcome to number lines, enter a number and I'll give you some other numbers in a line...");
int usernum;
int i = 1;
usernum = in.nextInt();
while(i <= usernum){
int j = 1;
while(j<=i){
System.out.print(usernum-i+1);
j++;
}
System.out.println();
i++;
}//while
}//numberlines
You can use this:
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int usernum = s.nextInt();
for(int i = 1; i <= usernum; i++){
for(int j = 1; j <= i; j++){
System.out.print(usernum-i+1);
}
System.out.println();
}
}
Related
import java.util.Scanner;
public class PrimeN {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int a, b;
a = sc.nextInt();
b = sc.nextInt();
while (a < b) {
for (int i = a; i <= b; i++) {
int count = 0;
for (int j = 1; j <= i; j++) {
if (i % j == 0) {
count++;
}
} //count if equals 2 the prime numbers displayed
if (count == 2) {
System.out.println(i);
a++;
}
}
}
}
}
This is what my output looks like. I dont understand why last prime numbers are being repeatedly printed.
What you should probably do is breaking task into steps.
Get the input
satrt finding the prime numbers from 1 to the max
create a list of already found prime numbers
start a loop from 1 to max
each iteration loop trough already found primes and perform number % prime == 0
if none of numbers in prime numbers can fulfils the condition add number to the list
if found number is greater or equal to min, save the index of it (startIndex).
print the input by iterating trough list from startIndex and printing the numbers
this workflow guarantees you will not consider multiplicant of two big prime numbers as prime number with better performance
You missplaced your first statement, your while-loop is a little bit scary, but here your fix:
import java.util.Scanner;
public class PrimeN{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int a,b;
a = sc.nextInt();
b = sc.nextInt();
for(int i = a; i <= b; i++){
int count = 0;
for(int j = 1; j <= i; j++){
if(i%j == 0){
count++;
}
}//count if equals 2 the prime numbers displayed
if(count == 2){
System.out.println(i);a++;
}}}}
Found a way to solve the ques.
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a, b;
a = sc.nextInt();
b = sc.nextInt();
for(int n = a; n <= b; n++){
boolean prime = true;
int i = 2; //1 will be a factor always
while(i <= n/2){
if(n%i == 0){
//is not prime
prime = false;
break;
}i++;
}
if(prime){
System.out.print(n+ " ");
}
}}}
Just remove your while(a<b) around the two for loops and then your code works fine
like this
import java.util.Scanner;
public class PrimeN {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int a, b;
a = sc.nextInt();
b = sc.nextInt();
// System.out.println("a<b");
int i = a;
while(i<=b)
{
int count = 0;
for (int j = 1; j <= i; j++) {
if (i % j == 0) {
count++;
}
} // count if equals 2 the prime numbers displayed
if (count == 2) {
System.out.println("a" + a + " i:" + i);
a++;
}
i++;
}
}
}
To find out what the actual error is, print out the values of a,b,i and j in each iteration.
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;
}
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'm new to coding, taking a online course at the moment. Now I'm stuck, and I cant find anything to help min move on. The task is to randomly print the amount of number given to the program. Input 5, and the program gives you 5 random numbers. After that sorting them in odd and even. This is all fine.
My problem is that I don't know how to count the numbers in each array (not adding them together) but counting how many odd numbers there is and how any even numbers there is.
Looking for help and guidance.
P.s sorry if there is an answer out there, did my best to find it before asking. d.s
import java.util.*;
public class RandomNbrs {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
System.out.println("How many random numbers do you want? (0-999)");
int numb = scan.nextInt();
System.out.println("Your random numbers:");
int array[] = new int[numb];
for(int i =0; i < numb; i++){
array [i] = (int) (0 + 1000 * Math.random());
System.out.print(array[i]);
System.out.print(" ");
}
System.out.println();
System.out.println();
System.out.println("Even numbers: ");
for(int j =0; j < numb; j++){
if(array[j] %2 == 0){
System.out.print(array[j]);
System.out.print(" ");
}
}
System.out.println();
System.out.println();
System.out.println("Odd numbers: ");
int oddNbr = 0;
for(int k =0; k < numb; k++){
if(array[k] %2 == 1){
System.out.print(array[k]);
System.out.print(" ");
}
}
}
}
There are a few concepts that you've probably misunderstood.
You say you need to create two different array for odd and even numbers. However, in the code that you have provided I only see 1 array called 'array'. Note that array[j] and array[k] refer to the same array. j and k are variables.
So, in case you really need two different array for the two numbers, you need to create two more arrays.
There are several ways, but this would be the simplest (might not necessarily be the best though) going by your code:
import java.util.*;
public class RandomNbrs
{public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
System.out.println("How many random numbers do you want? (0-999)");
int numb = scan.nextInt();
System.out.println("Your random numbers:");
int array[] = new int[numb];
int temp,countOdd=0,countEven=0;
for(int i =0; i < numb; i++){
//I am storing the random generated number in a temporary variable
//Then i check if it is odd or even and increase the count by using a counter
temp= (int) (0 + 1000 * Math.random());
if (temp%2 == 0):
countEven++;
else
countOdd++;
array[i]=temp
System.out.print(array[i]);
System.out.print(" ");
}
System.out.println();
System.out.println();
//Here i am creating two new arrays as you specified.
//if you just want to print the odd and even numbers AND their couns, you dont necessarily need to make these two arrays.
int arrayOdd[] = new int[countOdd];
int arrayEven[]=new int[countEven];
//Counter for accessing the elements of the two new arrays
int counter1=0,counter2=0;
//Filling the two new arrays with odd and even numbers from the prev array
for(int j =0; j < numb; j++){
if(array[j] %2 == 0){
arrayEven[counter1++]=array[j];
}
else
arrayOdd[counter2++]=array[j];
}
//Print odd
System.out.println("Odd:");
for(int k =0; k < countOdd; k++){
System.out.print(arrayOdd[k]);
System.out.print(" ");
}
System.out.println("Total odd numbers="+countOdd);
//or arrayOdd.length() also gives the count.
//Similar for printing even numbers
}}
Additionally you can also use ArrayList where you can dynamically and directly store odd and even generated random numbers in 2 arrays and you do not need the third common array. Let me know if you'd like to know how to this one. :)
this is how I solved the task:
public class testTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
int oddCounter=0;
int evenCounter=0;
Scanner scan = new Scanner(System.in);
System.out.println("How many random numbers do you want? (0-999)");
int numb = scan.nextInt();
System.out.println("Your random numbers:");
int array[] = new int[numb];
for(int i =0; i < numb; i++){
array [i] = (int) (0 + 1000 * Math.random());
System.out.print(array[i]);
System.out.print(" ");
}
System.out.println();
System.out.println();
for(int j =0; j < numb; j++){
if(array[j] %2 == 0){
evenCounter++;
}
}
System.out.println("Dessa " + evenCounter + " Even numbers: ");
for(int j =0; j < numb; j++){
if(array[j] %2 == 0){
System.out.print(array[j]);
System.out.print(" ");
}
}
System.out.println();
System.out.println();
for(int k =0; k < numb; k++){
if(array[k] %2 == 1){
oddCounter++;
}
}
System.out.println("Dessa " + oddCounter + " Odd numbers: ");
for(int k =0; k < numb; k++){
if(array[k] %2 == 1){
System.out.print(array[k]);
System.out.print(" ");
}
}
}
}
I'm trying to print all from 6 to n but when i run the code it gives me all the numbers but not the right perfect numbers. For example when I enter 30 it prints out all numbers but it says only 6 and 7 are perfect numbers only 7 is not a perfect number and 28 is.
import java.util.Scanner;
public class PerfectN {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number that is 6 or above: ");
int n = scanner.nextInt();
int sum = 0;
if(n<6){
System.out.println("Incorrect number.");
}
else
for(int i = 6;i<=n;i++){
for(int j = 1; j<i; j++){
if(i%j==0)
sum += j;
}
if(sum==i){
System.out.println(i + " is a perfect number.");
}
else
System.out.println(i + " is not a perfect number.");
}
}
}
You need to reset the sum variable to zero for each number you go through:
for (int i = 6; i <= n; i++) {
int sum = 0;
for (int j = 1; j < i; j++) {