While Loop - Sum of even numbers between two inclusive integers - java

EDIT: Lol I would've never thought you guys would be such savages. I thought this would be the right place for a college student to come and seek for answers. If I didn't care, why would I even be here.. Basically my question is different from the other ones because I do need the first loop to state my even numbers ie: 10 12 14 16 18 20. I sincerely hoped I would get some feedback on how to reinstate the original value of firstNum for the second loop, not criticism expecting to get selected as an answer for points in a matter of seconds.
I've implemented a code that I saw as an answer in a very similar question on the forum. Yet when I compile it, the output for my sum code doesn't return anything. It's just blank. Can I get some feedback on what I am missing or doing wrong? Thank you. BTW I can only use while loop.
System.out.println("Enter an integer:");
int firstNum = keyboard.nextInt();
System.out.println("Enter another integer larger than the first one:");
int secondNum = keyboard.nextInt();
System.out.println();
int mod = firstNum % 2;
int sum = 0;
if (mod != 0)
{
firstNum++;
}
System.out.print("Even numbers: ");
while (firstNum <= secondNum)
{
System.out.print(firstNum + " ");
firstNum += 2;
}
System.out.println();
System.out.print("Sum of even numbers: ");
while (firstNum <= secondNum)
{
System.out.print(sum);
sum += firstNum;
firstNum += 2;
}

The first loop only prints the even numbers, it can not add because the firstNum reaches the secondNum.
If you want to keep the same code you have, you only need to add a new variable that prints even numbers. So the first loop will print the even numbers and the second loop will do the sum.
For example:
int evenNumber;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter an integer:");
int firstNum = keyboard.nextInt();
System.out.println("Enter another integer larger than the first one:");
int secondNum = keyboard.nextInt();
System.out.println();
int mod = firstNum % 2;
int sum = 0;
if (mod != 0) {
firstNum++;
}
evenNumber = firstNum;
System.out.print("Even numbers: ");
while (evenNumber <= secondNum) {
System.out.print(evenNumber + " ");
evenNumber += 2;
}
System.out.println();
System.out.print("Sum of even numbers: ");
while (firstNum <= secondNum) {
sum += firstNum;
firstNum += 2;
}
System.out.println(sum);
This code is not optimal, but it complies with what you need. But if you want to do better, you can do it like this:
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter an integer:");
int firstNum = keyboard.nextInt();
System.out.println("Enter another integer larger than the first one:");
int secondNum = keyboard.nextInt();
int total = 0;
System.out.print("Even Numbers: ");
while (firstNum <= secondNum) {
if (firstNum % 2 == 0) {
System.out.print(firstNum + " ");
total += firstNum;
}
firstNum++;
}
System.out.println("\nSum: " + total);

During your first while, you already changed the value of firstNum. So basically, when you arrive to the second while, you don't even enter it.
I would simply change the first loop and delete the second one :
while (firstNum <= secondNum) {
System.out.print(firstNum + " ");
sum += firstNum;
firstNum += 2;
}
System.out.println();
System.out.print("Sum of even numbers: ");
System.out.println(sum);

In your first while loop -
while (firstNum <= secondNum){
System.out.print(firstNum + " ");
firstNum += 2;
}
you keep incrementing variable firstNum until you reach secondNum, so the condition of the second while loop (firstNum <= secondNum) will never evaluates to true.
A very simple way to achieve your goal can be as follows.
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter an integer:");
int firstNum = keyboard.nextInt();
System.out.println("Enter another integer larger than the first one:");
int secondNum = keyboard.nextInt();
int total = 0;
while(firstNum <= secondNum){
if(firstNum%2 == 0)
total += firstNum;
firstNum++;
}
System.out.println("Sum: " + total);

Related

How can I print numbers between two numbers that I enter?

I am creating a program which asks the user to enter two numbers. It will then print the numbers the user entered and the numbers between the two numbers in numerical order. I declared and initialized two variables, which are 'number1' and 'number2'.
int number1;
int number2;
do{
Scanner input = new Scanner(System.in);
System.out.print("Enter the first number: " );
number1 = input.nextInt();
System.out.print("Enter the second number: " );
number2 = input.nextInt();
if(number1 == number2)
{
System.out.println("The numbers you entered equal with each other. Try again.\n");
}
}while(number1 == number2);
if (number1 > number2)
{
for(int a = number2; a <= number1; a++)
{
System.out.print(a + " ");
}
}
else
{
for(int a = number1; a <= number2; a++)
{
System.out.print(a + " ");
}
}
How do I make it so it also prints only the numbers between 'number1' and 'number2'?
The first part of your code was fine, if you want to do it this way.
Scanner scanner = new Scanner(System.in);
int first, second;
do {
System.out.print("Enter the first number: ");
first = scanner.nextInt();
System.out.print("Enter the second number: ");
second = scanner.nextInt();
if (first == second) {
System.out.println("The numbers you entered equal with each other. Try again.\n");
}
} while (first == second);
For the second part, I'd recommend determining which number is which (smaller/bigger) instead of using duplicate code. Also you need to only cycle through numbers in between the numbers you chose, so you have to change your for cycle
//determine which number is bigger/smaller
int smaller = Math.min(first, second);
int bigger = Math.max(first, second);
for(int i = smaller+1 ; i < bigger ; i++){
System.out.print(i + " ");
}
The only odd scenario here is where you input two numbers which are adjacent, which will output no numbers. For example 3 and 4.
int max = Math.max(number1, number2);
for(int i = (max == number1 ? number2 : number1) + 1 ; i < max ; i++) {
System.out.println(i);
}

Code in Java to add two numbers is printing out sum backwards?

The following Java program (prints out addition problem of two given numbers) is printing the answer backwards ( for example, 563 instead of 365). How would I modify it so that it prints out the correct answer?
Note: I know that Im completing this problem in an unnecessarily complicated manner, but this is because we are only allowed to use primitive data types.
Thankyou.
//getting the number from user
Scanner linput = new Scanner(System.in);
System.out.print("Enter the first number:");
num = linput.nextInt();
System.out.println ("Enter the second number:");
num2 = linput.nextInt();
System.out.println (num);
System.out.println ("+");
System.out.println(num2);
System.out.println ("=======");
//making a copy of the input number
temp = num;
temp2 = num2;
//counting digits in the input number
while(num > 0)
{
num = num / 10;
count++;
}
while(num2 > 0)
{
num2 = num2 / 10;
count2++;
}
int answer = 0;
while(temp > 0 && temp2>0)
{
digit = temp % 10;
temp = temp / 10;
count--;
digit2 = temp2 % 10;
temp2 = temp2 / 10;
count2--;
answer = digit+digit2;
System.out.print(answer);
}
You problem statement is very unclear. If it's just simple addition of two numbers then you've really overcomplicated your implementation.
Scanner input = new Scanner(System.in);
System.out.print("Enter the first number:");
int num = input.nextInt();
System.out.print("Enter the second number:");
int num2 = input.nextInt();
System.out.print(num + " + " + num2 + " = " + (num + num2));
I totally agree with all comments that this is over complicated to do a sum. But the answer to the initial question why the result is printed backwards is that that you calculate the sum from right to left (units, tens, hundreds, ...) but print them left to right (default behavior of print)

Input numbers until 0, then calculate average

Okey, i need to make a program to calculate average. But, i need to input numbers, and when i want to stop i can input zero. Then, my program need to sum all entered numbers and calculate average of entered numbers. I made almost everyithing in my code but idk how to make a formula to calculate average, my program sum numbers and then divide by last entered number.
Scanner input = new Scanner(System.in);
System.out.println("Input numbers, 0 for stop!");
int number = input.nextInt();
int number1 = 1;
while (number != 0) {
number1 = (number + number1) / number; //here is my problem?
number = input.nextInt();
}
System.out.println("Average is: " + number1);
Your code is written great except you calculate the average wrong,
If you want to calculate the average on the fly you will need to know how much numbers you have read so far..
e.g:
Scanner input = new Scanner(System.in);
System.out.println("Input numbers, 0 for stop!");
int number = input.nextInt();
int average = number;
int counter = 1;
while (number != 0) {
average= (average * counter + number) / (counter + 1);
counter++;
number = input.nextInt();
}
System.out.println("Average is: " + average);
this code will give you the average after each step.
Another (simpler solution) is:
Scanner input = new Scanner(System.in);
System.out.println("Input numbers, 0 for stop!");
int number = input.nextInt();
double sum = 0;
int counter = 0;
while (number != 0) {
sum += number;
counter++;
number = input.nextInt();
}
System.out.println("Average is: " + sum/counter);
Average is the sum total of the numbers divided by the number of numbers, so you need to keep a running count of how many numbers you are inputting, as well as a running sum, and at the end, you divide the sum by the count. The sum should be a double just in case you end up with a fraction
Scanner input = new Scanner(System.in);
System.out.println("Input numbers, 0 for stop!");
double sum = 0;
int count = 0;
int number = input.nextInt();
while (number != 0) {
sum += number;
count++;
number = input.nextInt();
}
System.out.println("Average is: " + sum/count);

Inputting two separate integer numbers with no space between them?

I am making a lottery game and I am wondering if there is a way to type in two integer numbers, like 3 and 4 but type them in as 34 and have them read individually? I need to have them read individually as the game will reward if one of the numbers entered matches one of the randomly generated numbers. My code so far is this:
import java.util.Scanner;
import java.util.Random;
public class Lottery {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
Random random = new Random();
int guess, guess2, counter;
int win1000, win10, win1;
int number = random.nextInt(10); int number2 = random.nextInt(10);
System.out.println("Welcome to the Super Lottery! \nLet's Play!");
System.out.println("Round 1 - you have 5 chances to win money");
System.out.print("Enter your guess: ");
guess = keyboard.nextInt();
guess2 = keyboard.nextInt();
System.out.println("The lottery number is " + number + number2);
win1 = 1;
win10 = 10;
win1000 = 1000;
counter = 1;
while(counter < 5) {
number = random.nextInt(10); number2 = random.nextInt(10);
System.out.print("Enter your guess: ");
guess = keyboard.nextInt(); guess2 = keyboard.nextInt();
System.out.println("The lottery number is " + number + number2);
counter++;
if(guess == number2 && guess2 == number) {
System.out.println("You guessed both numbers, but in a different order! You win $" + win10);
}
else if(guess == number && guess2 == number2) {
System.out.println("CONGRATULATIONS! You win the Super Lottery!" + win1000);
}
else if(guess == number || guess2 == number2) {
System.out.println("You guessed one number! You win $" + win1);
}
}
}
}
You can use the substring method to separate the numbers by their index so for a visual example:
String numbers = "12345";
System.out.println(numbers.substring(0,1));
System.out.println(numbers.substring(1,2));
System.out.println(numbers.substring(2,3));
System.out.println(numbers.substring(3,4));
System.out.println(numbers.substring(4,5));
}
Which will print out:
1
2
3
4
5
If you want to separate with a comma you could do something like this:
String numbers = "12345";
char[] temp = numbers.toCharArray();
for(int i = 0; i < temp.length; i++) {
System.out.print(temp[i] + ",");
}
Output:
1,2,3,4,5,
Since random.nextInt has 10 as an argument, that means the random generated number will be from 0-9.
With that being said I am going to assume the number that the player will enter will have be from 00 to 99.
You can use a String for that and then split it into two pieces.
Check this code example:
Scanner keyboard = new Scanner(System.in);
String number = keyboard.nextLine();
int guess, guess2;
guess = Integer.valueOf((number.substring(0, 1)));
guess2 = Integer.valueOf((number.substring(1, 2)));
System.out.println(guess);
System.out.println(guess2);
Try something like:
Scanner in = new Scanner(System.in);
int number = in.nextInt();
int firstNumber = number / 10;
int secondNumber = number % 10;
System.out.println("Result: "+ firstNumber + " & " + secondNumber);

Perfect number from input

I'm trying to print out if a number is perfect or not by having the user enter in a number. When I enter a perfect number like 6, for example, it tells me that it is not a perfect number and can't figure out why. My final code needs to print out like 6 = 1 + 2 + 3. But I'm not that far yet.
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a positive integer: ");
int n = scanner.nextInt();
int sum = 0;
for(int i = 1;i<n;i++){
if(n%2==0)
sum += i;
}
if(sum==n){
System.out.println(n + " is a positive number");
}
else {
System.out.println(n + " is not a positive number");
}
if(n%2==0)
should be
if(n%i==0)
Otherwise your sum would be the sum of all numbers from 1 to n-1 for even n, or 0 for odd n.
Your code would look like:
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a positive integer: ");
int n = scanner.nextInt();
int sum = 0;
for(int i = 1;i<n;i++){
if(n%i==0)
sum += i;
}
if(sum==n){
System.out.println(n + " is a perfect number");
System.out.print(n+" = ");
for(int i=1;i<n;i++)
if(n%i==0) System.out.print((i!=1?" + ":" ") + i);
}
else {
System.out.println(n + " is not a perfect number");
}
Formatting the output without using the ternary operator:
System.out.println(n + " is a perfect number");
System.out.print(n+" = ");
for(int i=1;i<n;i++)
if(n%i==0) {
if (i==1)
System.out.print(i);
else
System.out.print(" + " + i);
}
Check this out. :)
public static String isPerfect(int num){
int product = 1;
for (int x=1; x<num; x++){
product *= x;
if (product == num){
return num + " is a perfect number";
}
}
return num + " is a not perfect number";
}

Categories