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++)
Related
This piece of code is supposed to take 2 numbers and find the factorial of every number between and incuding said numbers. I'm not getting the right output however and cant figure out what im doing wrong.
Scanner scan = new Scanner(System.in);
long result = 1;
int m = scan.nextInt();
int n = scan.nextInt();
scan.close();
if (n > 0 && m > 0) //want factorial greater than zero
for(int j = n; j <= m; j++)
{
for(int i = 1; i <= j; i++)
{
result = result * i; //find factorial
}
System.out.println(result);
}
if(n <= 0 || m <= 0) //if value is les than zero
{
System.out.println("Not Valid!");
}
Something like should work:
public class RangeFactorial {
public static void main(String[] args) throws Exception {
Scanner scan = new Scanner(System.in);
int max = scan.nextInt();
int min = scan.nextInt();
if (max < 0 || min < 0) {
System.out.println("Invalid Params");
}
for (int i = min; i <= max; i++) {
System.out.println("Factorial for " + i + " is: " + factorial(i));
}
scan.close();
}
private static int factorial(int i) {
if (i <= 1) {
return 1;
}
return i * factorial(i-1);
}
}
Note that the code assumes that max/min fall in place, I've omitted the logic to decide the max/min integer from the given inputs. You'll need to add this.
You forgot to reset 'result' to 1.
Also, there is no need to have another if statement if it is only checking for the negation of the first one, just use an else.
I also fixed the code style guidelines to follow the standard Java ones as:
The curly bracket style you used is the one usually used in C/C++.
Even if if-statements or loops contain only one line after them, it is good etiquette to use curly brackets anyway, in Java.
Take a look at the Google Java Style Guide if you want to know more.
Scanner scan = new Scanner(System.in);
long result = 1;
int m = scan.nextInt();
int n = scan.nextInt();
scan.close();
if (n > 0 && m > 0){
for(int j = n; j <= m; j++){
result = 1; //You forgot to reset 'result'
for(int i = 1; i <= j; i++){
result *= i;
}
System.out.println(result);
} else { // No need for another if statement
System.out.println("Not Valid!");
}
package Testing;
import java.util.Scanner;
public class test {
public static void main (String[] args){
Scanner scan1 = new Scanner(System.in);
System.out.print("Input positive integer: ");
int n = scan1.nextInt();
for (int i = 2; i < Math.sqrt(n); i++){
if (n % i == 0){
System.out.print(n/i);
}
}
}
}
When I run this code it ends up printing the numbers 2510 whenever I input 50. What did I do wrong?
As was mentioned in the comments, you're very close, but you need to stop iterating after you find an answer. This can be done with the break keyword, which exits the innermost loop.
for (int i = 2; i < Math.sqrt(n); i++){
if (n % i == 0){
System.out.print(n/i);
break;
}
}
You have to stop when you find the first prime factor. And do not forget, that in your example Math.sqrt(n) is calculated every loop.
public static int getLargestFactor(int num) {
for (int i = 2, max = (int)Math.sqrt(num); i <= max; i++)
if (num % i == 0)
return num / i;
return num;
}
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;
}
Here is my code to create a program that takes a user input and lists multiples of 7 that relate to that number.
For example: The user inputs 3, I need the output to be "7, 14, 21".
Currently if I enter a number less than 7, the program complies without printing an output, but as soon as I enter 7 or any number higher than 7 then the program compiles and prints exactly what I need it to.
So the problem I need to fix is to be able to enter a number lower than 7 and recieve the correct output.
Thanks in advance!
import java.util.Scanner;
public class MultiplesOfSeven {
public static void main(String[] args){
int j = 0;
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
for(j = 1; j <= n; j++){
if(j % 7 == 0){
System.out.print(j + " ");
for (int counter = 0 ; counter < n ; counter++) {
System.out.print(j*(2 + counter) + " ");
}
}
}
}
Don't overthink the loop here. As alternatives, both which mean you can delegate the % check, consider
for (j = 0; j < n; ++j){
// output (j + 1) * 7;
}
or, the less elegant due to your having to write 7 in three places
for (j = 7; j <= n * 7; j += 7){
// output j
}
This code is preventing your programm from printing anythingk, when you enter a number below 7:
if(j % 7 == 0){
% is the modulo operator.
It says: do what is in the brackets if the number I have counted to (j) has no reminder if I divide it by 7.
So what you have to do is count to the number entered (using a for loop) and print the mulitplication of the current number times 7.
It's not printing anything because when the number you inputted is less than seven and greater than zero, the code inside
if(j%7==0)
does not get executed. I think your code should be like this.
for (j = 1; j <= n; j++) {
if (j % 7 == 0) {
System.out.print(j + " ");
}
for (int counter = 0; counter < n; counter++) {
System.out.print(j * (2 + counter) + " ");
}
}
import java.util.Scanner;
public class MultiplesOfSeven {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
for(int j = 1; j <= n; j++) {
System.out.print(7*j + " ");
}
}
}
This is simple solution of your problem. This will work for all cases. Keep code simple good luck.
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++) {