Adding 2 integers in 1 User input - java

I want to make the user input 2 integers in 1 input like this
Enter two integers: 2 2 // sum = 4
Here is my sample code:
Scanner s = new Scanner(System.in);
int x, num1, num2, sum;
System.out.print("*Enter 2 integer: ");
x = s.nextInt();
int sum = num1 + num2;
System.out.println(" Sum = "+ sum);
Is it possible to add 2 integers without using variables num1 and num2?
I want the output to be like this.
Enter two integers: 2 2
sum = 4

Add any number of integers:
Scanner s = new Scanner(System.in);
System.out.print("Enter some integers: ");
int sum = 0;
while (s.hasNextInt()) {
sum += s.nextInt();
}
System.out.println(" Sum = " + sum);

Case 1:
If you have only 2 values then follow the below code
import java.util.*;
class Add {
public static void main(String[] args) {
int sum = 0;
Scanner scan = new Scanner(System.in);
System.out.print("*Enter 2 integer: ");
for(int i = 0; i < 2; i++)
sum += scan.nextInt();
System.out.println(" Sum = "+ sum);
}
}
Case 2:
If you have some More than 2 numbers but you have an idea about how many numbers to sum then follow the below code.
import java.util.*;
class Add {
public static void main(String[] args) {
int sum = 0;
Scanner scan = new Scanner(System.in);
System.out.print("*Enter how many integers to sum: ");
int iterate = scan.nextInt();
System.out.print("*Enter " + iterate + " integer: ");
while(iterate-- > 0)
sum += scan.nextInt();
System.out.println(" Sum = "+ sum);
}
}

Related

print the nth digit of a number in JAVA

I want to print nth digit of a number (from left to right) in JAVA. Here's what I've tried, I'm a total noob.
public class NewClass {
public static void main(String args[]) {
System.out.println("enter number: ");
Scanner s = new Scanner(System.in);
int number = s.nextInt();
System.out.println("enter n: ");
Scanner n = new Scanner(System.in);
int num = n.nextInt();
for (int i = 1; i <= num; i++) {
number = number / 10;
};
System.out.println("The nth Digit = " + number);
}
Following #ryan 's explanation, I found a solve in my way.
package com.mycompany.lab;
import java.util.Scanner;
int reversed = 0, digit;
System.out.println("enter number: ");
Scanner s=new Scanner(System.in);
int number=s.nextInt();
while(number != 0) {
// get last digit from num
digit = number % 10;
reversed = reversed * 10 + digit;
// remove the last digit from num
number /= 10;
}
System.out.println("enter n: ");
Scanner n=new Scanner(System.in);
int num=n.nextInt();
for(int i=1;i<num;i++){
reversed = reversed / 10;
};
while(reversed > 10){
reversed = reversed % 10;
}
System.out.println("The nth Digit = " + reversed);
}
}

Rookie Java question - Scanner, int and user input

Hello there fellow Overflowers!
I'm getting started with Java and want to create a small program where it's possible to type in 5 numbers and get the sum and average printed out.
The program runs and does it's job, but I feel that there must be a way smarter method / way then what I've done.
import java.util.Scanner;
public class Calc {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("5 random numbers");
int num1 = input.nextInt();
int num2 = input.nextInt();
int num3 = input.nextInt();
int num4 = input.nextInt();
int num5 = input.nextInt();
int sum = num1 + num2 + num3 + num4 + num5;
int avg = (num1 + num2 + num3 + num4 + num5)/5;
System.out.println("Sum is" + sum + " and the average is " + avg);```
Is it possible to declare all the numbers in one line or something?
You can use BufferedReader. Try this. Here, you need to mention all numbers in a single line.
Ex: 1 2 3 4 5
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] values = line.split(" ");
int[] arr = new int[values.length];
int sum=0;
for (int i = 0; i < values.length; i++) {
arr[i] = Integer.parseInt(values[i]);
sum+=arr[i];
}
double avg = sum/arr.length;
System.out.println("Sum is" + sum + " and the average is " + avg);

Calculate Average in arrays

I want to calculate the average numbers using arrays. I want the program asks for the amount of grades and after I want to put the grade numbers.
After I want to get the average output in a double.
This is my code so far:
public class Average {
public static void main(String[] args)
{
//int n = MyConsole.readInt("Enter number of grades: " );
int a = MyConsole.readInt("Enter grade 1: " );
int b = MyConsole.readInt("Enter grade 2: " );
int c = MyConsole.readInt("Enter grade 3: " );
int[] numbers = new int[]{a,b,c};
numbers[0] = a;
numbers[1] = b;
numbers[2] = c;
int sum = 0;
for(int i=0; i < numbers.length ; i++)
sum = sum + numbers[i];
double average = sum / numbers.length;
System.out.println("Average value of array elements is : " + average);
}
}
Don't know what your class MyConsole is doing, but I guess is a Scanner:
Your code improved will be something like this:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of grades: " );
int n = sc.nextInt();
int sum = 0;
for (int i = 0; i < n; i++) {
System.out.print("Enter grade "+ (i + 1) + ": ");
int a = sc.nextInt();
sum += a;
}
double average = sum / n;
System.out.println("Average value of array elements is : " + average);
}
OUTPUT (2 grades):
Enter number of grades: 2
Enter grade 1: 1
Enter grade 2: 5
Average value of array elements is : 3.0
OUTPUT (5 grades):
Enter number of grades: 5
Enter grade 1: 10
Enter grade 2: 20
Enter grade 3: 30
Enter grade 4: 10
Enter grade 5: 50
Average value of array elements is : 24.0
NOTE
double average = sum / n;
performs an int division, so you won't have any decimal places! I would propose a fast cast:
double average = sum / (double) n;
With new output:
Enter number of grades: 2
Enter grade 1: 1
Enter grade 2: 4
Average value of array elements is : 2.5
GUESS using your own class:
public static void main(String[] args) {
int sum = 0;
int n = MyConsole.readInt("Enter number of grades: " );
for (int i = 0; i < n; i++) {
int a = MyConsole.readInt("Enter grade "+ (i + 1) + ": ");
sum += a;
}
double average = sum / n;
System.out.println("Average value of array elements is : " + average);
thank you !
Sorry for the poor explanation.
This is my first question
this it the code after edit:
import java.util.Scanner;
public class Average {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of grades: ");
int n = sc.nextInt();
int sum = 0;
int[] numbers = new int[n];
for(int i=0; i < numbers.length ; i++)
{
System.out.println("Enter grade " + (i + 1) + " :");
int a = sc.nextInt();
sum = sum + a;
}
double average = sum / (double) n;
System.out.println("Average value of array elements is : " + average);
sc.close();
}
}
Program to Calculate Average Using Arrays:
public class Inter1 { //name of the class
public static void main(String[] args) {//main method
int number[]={40,56,23,56,87,23,78}; //declaring the int array
int sum=0;
for (int s:number){ //for each
sum +=s;
}
int ave=sum/number.length; //to get the average
System.out.println("the average is "+ave); //out put
}
}
package inter1;
import static java.time.Clock.system;
import java.util.Scanner;
public class Inter1 {
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
int total=0;
System.out.println("Enter how many number that do u wanna enter ?? ");
int num= in.nextInt();
int numbers[]=new int[num];
for (int i=0;i<numbers.length;i++){
System.out.println(i+1+":"+"enter the your numbers ? ");
numbers[i]=in.nextInt();
}
for (int i=0;i<numbers.length;i++){
total+=numbers[i];
}
int average =total/numbers.length;
System.out.println("the average is "+average);
}
}
public class Inter1 { //name of the class
public static void main(String[] args) { //main method
System.out.println("==============================");
int num[]={34,56,78,78,34,2,33,99,100,56}; //int array
int total=0;
for (int i=0;i<num.length;i++){ //for loop
total+=num[i];
}
int avrage1=total/num.length; //output
System.out.println("The average is "+avrage1);
}
}

How to use Java to calculate average from input?

I need a program that should add x numbers. The numbers should come from user input so I need some sort of loop. I have gotten as far as shown below, but I'm stuck since I have no idea how to add a new number without deleting the previous?
System.out.println("How many numbers to use?");
int number = keyboard.nextInt();
for (int i = 0; i<number ; i++) {
System.out.println("whats the number");
double first = keyboard.nextDouble();
}
If all you need is the average, you don't need to keep all the numbers you get from user input. Just keep one variable that holds their sum.
define the sum variable outside the loop (initialized to 0), and add to it each number you get from user input.
int number = keyboard.nextInt();
double sum = 0;
for (int i = 0; i<number ; i++)
{
System.out.println("whats the number");
sum += keyboard.nextDouble();
}
double average = sum / number;
public class Average {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double sum = 0;
int num;
System.out.println("enter how many num");
num = sc.nextInt();
System.out.println("please enter " + num + " numbers");
for (int i = 0; i < num; i++) {
sum += sc.nextDouble();
}
double avg = sum / num;
System.out.println("Average of " + num + " numbers is:" + avg);
}
}

control structure, repetition exercise: How to get the sum of the digits of a number?

Guys can you please help me answer this exercise using for loop without using string methods.
Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits. For example, the program should output the individual digits of 3456 as 3 4 5 6 and the sum as 18,and output the individual digits of -2345 as 2 3 4 5 and the sum as 14.
This is the code:
package MyPackage;
import java.util.*;
public class Integer
{
public static void main(String args[])
{
Scanner console = new Scanner (System.in);
int input;
int sum = 0;
int num1 = 0;
int counter = 1;
String num = "";
System.out.print("enter a number: ");
input = console.nextInt();
if (input == (-input))
{
input = input * (-1);
num = String.valueOf(input);
num1 = num.length();
System.out.print("the digits of " + input + " are: ");
for (int i = 0; i < num1; i++ )
{
String var = num.substring(i,counter);
int var1 = Character.getNumericValue(var.charAt(0));
System.out.print(var + " ");
sum = sum + var1;
counter++;
}
System.out.println();
System.out.println("the sum is: " + sum);
}
else
{
num = String.valueOf(input);
num1 = num.length();
System.out.print("the digits of " + input + " are: ");
for (int i = 0; i < num1; i++ )
{
String var = num.substring(i,counter);
int var1 = Character.getNumericValue(var.charAt(0));
System.out.print(var + " ");
sum = sum + var1;
counter++;
}
System.err.println();
System.out.println("the sum is: " + sum);
}
}
}
Iterating all the digits from right to left is easy enough - you just keep dividing by 10 and keeping the remainder.
Since you need to print them from left to right, but there don't seem to be any constraint on the memory usage, you could just keep them in a list, and print it backwards:
int num = ...; // inputed from user
List<Integer> digits = new LinkedList<>();
int sum = 0;
// Extract the digits and the sum
while (num != 0) {
int digit = num % 10;
digits.add (digit);
sum += digit;
num /= 10;
}
// Print backwards:
System.out.print ("The digits are: ");
for (int i = digits.size() - 1; i >= 0; --i) {
System.out.print (digits.get(i) + " ");
}
System.out.println();
System.out.println("Their sum is: " + sum);

Categories