How to display the sum of the cubes of the digits? [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Code:
import java.util.*;
import java.io.*;
class Cubesum {
public static void main(String args[]){
int input=0;
int num1,num2,num3;
//read the number
System.out.println("Enter a Number");
Scanner console = new Scanner(System.in);
input= Integer.parseInt(console.nextLine());
// now let us print the cube of digits
//i.e if number is 123 we will print 1^3, 2^3 and 3^3
//we will also add 1 and 3 to output the sum of first
//and last digits
int number = input; //number is a temp variable
int counter = 0; //counter is used to count no of digits
while(number>0){
int t= number%10;
System.out.println("Cube of "+t +" is "+(t*t*t));
counter = counter+1;
number = number/10;
}
}
}
Output:
Enter a Number
**223**
Cube of 3 is 27
Cube of 2 is 8
Cube of 2 is 8
How do I add the cubes of these numbers? For example, 27+8+8 would be 43

Maybe you want to do something like this:
int number = input;
int sum = 0;
while (number > 0) {
int digit = number % 10;
sum += digit * digit * digit;
number /= 10;
}

Try this code.
int sum=0;
while(number>0){
int t= number%10;
System.out.println("Cube of "+t +" is "+(t*t*t));
sum=sum+(t*t*t);
counter = counter+1;
number = number/10;
}
System.out.println(sum);

Here a solution:
int sum = 0;
while(number>0){
int t= number%10;
System.out.println("Cube of "+t +" is "+(t*t*t));
sum += t*t*t;
counter = counter+1;
number = number/10;
}
System.out.println(sum);

You seem new to Java, so here is a more simple (and readable) example for you:
import java.util.*;
import java.io.*;
class Cubesum {
public static void main(String args[]){
int num1,num2,num3;
Scanner console = new Scanner(System.in);
//read the numbers
System.out.println("Enter the first number");
num1 = Integer.parseInt(console.nextLine());
System.out.println("Enter the second number");
num2 = Integer.parseInt(console.nextLine());
System.out.println("Enter the third number");
num3 = Integer.parseInt(console.nextLine());
int output = (int1*int1*int1)+(int2*int2*int2)+(int3*int3*int3)
System.out.println("result is: " + output);
}
}
You want to get each number individually then create the result.
Sample input:
2
2
3
Output should be:
43

Related

Java reading every other line in text file

I've written some code that has a scanner read from a text file on my computer, but when running the code, the scanner only reads every other number that's in the text file.. any ideas?
Note: For the grades.txt, this is the file
"3 8 1 13 18 15 7 17 1 14"
import java.util.*;
import java.io.*;
public class GradeAverage
{
public static void main(String[] args) throws IOException
{
Scanner scanner = new Scanner(new File("C:\\Users\\Media - Graphics\\Documents\\SCHOOL\\NCVPS\\GradeAverage\\grades.txt"));
int i = 0;
int sum = 0;
int lineNumber = 0;
int average = 0;
while(scanner.hasNextInt()){
System.out.println(scanner.nextInt());
sum = sum+scanner.nextInt();
lineNumber++;
}
System.out.println("The sum of the numbers: "+sum);
System.out.println("The number of scores: "+lineNumber);
average = sum/lineNumber;
System.out.println("The average of the numbers: "+average);
}
}
Here's what it outputs:
3
1
18
7
1
The sum of the numbers: 67
The number of scores: 5
The average of the numbers: 13
Every time you call nextInt() in your loop, you consume one int. So when you do
System.out.println(scanner.nextInt());
sum = sum+scanner.nextInt();
You consume two int(s). You want something like
int t = scanner.nextInt();
System.out.println(t);
sum += t;
Also your average is currently an int (I would expect a float or double).
double average = sum/(double)lineNumber;
Don't forget to remove int average = 0; (or modify this and average accordingly).

How to link one with another parts [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I have this homework. I'm having a hard time linking the first part with the second. This is what I have. I know I'm missing something at some point to indicate that from the number input it should add the next 100 numbers.
package test;
import java.util.Scanner;
public class Ex216 {
public static void main(String[] args) {
// Write a program in Java that reads an integer from the keyboard and makes the sum of the next 100 numbers, showing the result on screen
Scanner myInput = new Scanner(System.in);
int =a
int sum;
System.out.print("Enter first integer: ");
a = myInput.nextInt();
for (int n = a; n <= 100; n++)
System.out.printf("Sum = %d\n", sum);
}
}
This is what is casing me the trouble.
first of all,int =a is not a valid expression. it should be int a; then as you want to add next 100 number from the given value, you need to add those values into sum, such as sum = sum+number.
Here is a code snippet:
Scanner myInput = new Scanner(System.in);
// correct declaration
int a;
// initialize sum with zero.
int sum=0;
System.out.print("Enter first integer: ");
a = myInput.nextInt();
//for simplicity,start value n from a and loop until n reaches a+100.
for (int n = a; n <= 100+a; n++) {
sum = sum + n;
}
System.out.println("Sum = "+ sum);

How to increment input value by two in Java [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
My program asks the individual to input 2 numbers (ie; 10 and 20).
I would like the output to be:
Even numbers: 10 12 14 16 18 20
My code:
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;
while ((firstNum < secondNum) && mod == 0)
{
firstNum = firstNum + 2;
System.out.print("Even numbers" +firstNum);
}
You are close to the result you are after, just need to rearrange the order of a couple of lines and add one if check.
I have made a complete example that runs as expected and allows for both odd and even numbers. You can replace your code with the following:
public static void main(String[] args)
{
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;
//If first number is odd, increase by one to make it even.
if (mod != 0)
{
firstNum++;
}
System.out.print("Even Numbers: ");
while (firstNum <= secondNum)
{
System.out.print(firstNum + " ");
firstNum = firstNum + 2;
}
keyboard.close();
}

Java Largest and smallest [duplicate]

This question already has answers here:
Java program to find the largest & smallest number in n numbers without using arrays [closed]
(8 answers)
Closed 6 years ago.
I have a slight issue with my program. I need to ask the user to input as many numbers as they want and then the program will tell them what is the smallest and largest number. My issue is when all is said and done it prints out "the largest number is 0" and "the smallest number is 0". It always says that even if i never enter 0. I was wondering what was wrong with the program. Any pointers or helpers would be fantastic. Again to repeat, the issue im having is that the smallest and largest come back as 0's no matter what.
import java.util.Scanner;
public class LargestAndSmallest {
public static void main(String[] args) {
int smallest = 0;
int large = 0;
int num;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the numer");
int n = keyboard.nextInt();
num = keyboard.nextInt();
while (n != -99) {
System.out.println("Enter more numbers, or -99 to quit");
n = keyboard.nextInt();
}
for (int i = 2; i < n; i++) {
num = keyboard.nextInt();
if (num > large) {
large = num;
System.out.println(large);
}
if (num < smallest) {
smallest = num;
}
}
System.out.println("the largest is " + large);
System.out.println("the smallest is " + smallest);
}
}
I used this code as in the first place: Java program to find the largest & smallest number in n numbers without using arrays
import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class LargestAndSmallest {
public static void main(String... args) {
final Scanner keyboard = new Scanner(System.in); //init the scanner
System.out.println("Enter a number");
final Set<Integer> ints = new HashSet<>(); //init a set to hold user input
int n; //declare a variable to hold each number
while ((n = keyboard.nextInt()) != -99) { //loop until 99 is entered
ints.add(n); //add user input to our set
System.out.println("Enter more numbers, or -99 to quit.");
}
//output aggregate info
System.out.println("the largest is " + Collections.max(ints));
System.out.println("the smallest is " + Collections.min(ints));
}
}

How to do an addition from random numbers by the user?

I am new to Java and I would like some help. I have to solve this problem and I have it almost 90% solved:
Prompt the user to enter number of students. It must be a number that is perfectly divisible by 10 i.e. (number % 10) = 0
Check user input. If user input is not divisible by 10, keep asking the user for input until he enter a right number.
Accept user input and generate that many random numbers in the range from 0 to 100.
Print a matrix of random numbers and calculate the sum and average of all these random numbers and print them to the user.
Format sum and average to three decimal points.
This is my code so far:
import java.text.DecimalFormat;
import java.util.Scanner;
public class Calculator10 {
public static void main(String[] args) {
Scanner user_input = new Scanner(System.in);
int num;
do {
System.out.print("Enter a number: ");
num = user_input.nextInt();
} while(num % 10 != 0);
double numb;
DecimalFormat dec = new DecimalFormat("0.00");
for (int i=0; i<num; i++){
numb = Math.abs(Math.random() * ( 0 - 100 ));
System.out.print(" " +dec.format(numb) + " ");
}
}
}
As you can see, I have solved until the first part of # 4. I am not sure how I could sum all those random numbers displayed on the screen after user input. Of course, we have to store them in an array but I tried to do that but couldn't. So, how could I complete step #4 and 5? I would appreciate any help. Thanks a lot guys.
Here is how you should do it:
import java.text.DecimalFormat;
import java.util.Scanner;
public class Calculator10 {
public static void main(String[] args) {
Scanner user_input = new Scanner(System.in);
int num;
do {
System.out.print("Enter a number: ");
num = user_input.nextInt();
} while(num % 10 != 0);
double numb;
double sum=0;
DecimalFormat dec = new DecimalFormat("0.00");
for (int i=0; i<num; i++){
numb = Math.random() * ( 100 - 0));
System.out.print(" " + dec.format(numb) + " ");
sum += numb;
}
System.out.println("The sum is: " + dec.format(sum));
System.out.println("The average is:" + dec.format(sum/num));
}
}
Please note that I have slightly changed the way you were generating the random numbers which obviates the need to use Math.abs(). Also see the following answer to see how to generate random numbers between two different values:
Generating random numbers with Java
You do not need to store them in an array. Just declare int sum = 0 at the start and do sum += numb each time you generate a random number. Also, you are generating random numbers in a strange way. Take a look at the java.util.Random class.

Categories