public static void main(String[] args) {
Scanner x = new Scanner(System.in);
int n = 0;
int g = 0;
int term = 0;
int temp = 0;
int sum = 0;
int factor = 1;
System.out.print("Input N:");
n = x.nextInt();
g = n;
if (n <= 0) {
System.out.println("Please enter a positive integer");
System.exit(0);
}
if (n > 0) {
System.out.print("The factors are:");
while (factor < n) {
if (n % factor == 0) {
System.out.print(factor + ",");
}
factor++;
}
}
}
If I input number 8, the factors are 1,2, and 4. What I am trying to achieve is to add the factors of 8 which are 1,2 and 4, which would result in 7.
import java.util.Scanner;
public class Demo {
public static void main(String[] args)
{
Scanner x=new Scanner(System.in);
int n=0;int g=0; int term=0;int temp=0;
int sum=0; int factor=1;
System.out.print("Input N:");
n=x.nextInt();
g=n;
int number = 0;
if (n<=0)
{
System.out.println("Please enter a positive integer");
System.exit(0);
}
if (n>0)
{
System.out.print("The factors are:");
while (factor<n)
{
if (n%factor==0)
{
System.out.println(factor+",");
number+=factor;
}
factor++;
}
}
System.out.println("Sum = "+number);
}
}
import static java.lang.System.*;
import java.util.*;
import java.io.*;
public class Main
{
public static void main(String[] args)
{
Scanner kb = new Scanner(in);
out.print("Enter a number: ");
int num = kb.nextInt();
int sum = 0;
int x = 1;
for(x = 1; x <= num; x++){
if (num % x == 0){
sum = sum + x;
}
}
out.print(sum);
}
}
Related
what's wrong with this?
if condition is not executing.
import java.util.Scanner;
public class ArmstrongNum {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("ENTER THE NUMBER");
int n = sc.nextInt();
int temp = n;
int rem = 0;
int sum = 0;
while (n != 0) {
rem = n % 10;
sum = sum + (rem * rem * rem);
n = n / 10;
}
if (temp == n) {
System.out.println("number is a AMSTRONG");
} else {
System.out.println("NUMBER IS NOT AMSTRONG");
}
}
}
Your logic is wrong. if(temp==n) { is after while(n!=0) { so the only way it could be true is if temp == 0.
Your Calcualtion is fine but your compare is wrong. For example the number 153, which is a armstrong number, your variables will have the following values at the end of the loop:
temp = 153
rem = 1
sum = 153
n = 0
So you should not compare temp to n temp == n , how you currently do but temp to sum temp == sum
Also take in mind that your check will only work for three digits numbers, because your power is allways three. So for other armstrong numbers it simply won't work, for example:
54748 = 55 + 45 + 75 + 45 + 85 = 3125 + 1024 + 16807 + 1024 + 32768
In this solution i used the Math libary and the power but there are more ways if you don't like this to calc the length of an number
public class ArmstrongNum {
private static final Scanner SC = new Scanner(System.in);
private static boolean isArmstrongNumber(int n){
int temp = n;
int rem;
int length = (int) (Math.log10(n) + 1);
int sum = 0;
while (n != 0) {
rem = n % 10;
sum = sum + (int) Math.pow(rem, length);
n = n / 10;
}
return temp == sum;
}
public static void main(String[] args) {
System.out.print("ENTER THE NUMBER: ");
int n = SC.nextInt();
if (isArmstrongNumber(n)) {
System.out.printf("%d is a Armstrong number", n);
} else {
System.out.printf("%d is no Armstrong number", n);
}
}
}
Try another way
public static void main(String[] args) {
try {
Scanner sc= new Scanner(System.in);
System.out.println("ENTER THE NUMBER");
int n = sc.nextInt();
if (n == 0) {
throw new Exception("Number is 0");
}
int sum = 0;
String number = String.valueOf(n);
char[] chars = number.toCharArray();
double length = chars.length;
for (char item : chars) {
double result = Math.pow(Double.parseDouble(String.valueOf(item)), length);
sum = sum + (int) result;
}
if (sum == n ) {
System.out.println("number is a AMSTRONG");}
else {
System.out.println("NUMBER IS NOT AMSTRONG");
}
} catch (Exception exception) {
System.out.println(exception.getMessage());
}
}
how do I make a loop statement to print the sum of positive numbers from 1 to the value assigned to int from input from a scanner?
public static void main(String[] args) {
int num = 0;
int sum = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Input a number over value 1 ");
num = Integer.parseInt(sc.nextLine());
if (num <= 0) {
System.out.println("the number has be an Integer");
return;
}
for (int i = 1; i <= num; i++) {
System.out.println(i + "+");
sum += i;
// I have no idea what I can do from here one?
}
}
public static void getData() {
System.out.println("Please enter user input ");
Scanner sc = new Scanner(System.in);
int input = sc.nextInt();
if (input == 0) {
System.out.println("user input cann't accept.");
} else {
int sumValue = 0;
for (int j = 1; j <= input; j++) {
sumValue = sumValue + j;
}
System.out.println(sumValue);
}
}
How could I use the following method to sum the integers of two numbers in a separate method? I'm trying to teach myself how to use overloaded methods but this is starting to confuse me. Thanks!
public static void sumNUmber(){
System.out.println("Enter a number");
Scanner in = new Scanner(System.in);
int num = in.nextInt();
int sum = 0;
while (num > 0) {
sum = sum + num % 10;
num = num / 10;
}
System.out.println(sum);
public static void main(String[] args) throws ClassNotFoundException {
System.out.println("Enter a number");
Scanner in = new Scanner(System.in);
int num = in.nextInt();
System.out.println(sumDigits(num));
}
public static int sumDigits(int num) {
int sum = 0;
while (num > 0) {
sum += num % 10;
num = num / 10;
}
return sum;
}
output
Enter a number
234
9
You probably want to take your core algorithm and put it into a single function and then call it twice, once for each number. For example,
// Core algorithm.
public static int sumDigits(int num) {
int sum = 0;
while (num > 0) {
sum += num % 10;
num = num / 10;
}
return sum;
}
public static void sumNumber() {
Scanner in = new Scanner(System.in);
System.out.println("Enter a number");
int numA = in.nextInt();
System.out.println("Enter another number");
int numB = in.nextInt();
int totalDigits = sumDigits(numA) + sumDigits(numB);
System.out.println(totalDigits);
}
//Merry Xmas :D
public int sumNumbers(int num) {
int returnval;
if (num < 10) {
return num;
} else if (num < 100) {
returnval = Math.floor(num/10) + (num%10);
} else if (num < 1000) {
returnval = Math.floor(num/100) + Math.floor(num/10) + (num%10);
} //repeat as needed
return 0;
}
I've got a problem with some simple code. I haven't seen where is the problem in my code. It returns false when it should return true, since 153 is an Armstrong Number.
Following is my code:
public class Armstrong {
static double nArms, unidad, decena, centena, aux;
Armstrong(){
}
Armstrong(double nArms){
this.nArms = nArms;
}
public boolean esArmstrong(double nArms){
aux = nArms % 100;
centena = nArms / 100;
decena = aux / 10;
unidad = aux % 10;
this.nArms = Math.pow(unidad, 3) + Math.pow(decena, 3) +Math.pow(centena, 3);
if(this.nArms == nArms){
return true;
}else{
return false;
}
}
public static void main(String[] args) {
Armstrong arms = new Armstrong();
System.out.println(arms.esArmstrong(153));
}
}
You are using double when you intend to do integer arithmetic. For example, when you write
centena = nArms / 100;
you are doing floating point division, (and centena is assigned the value 1.53) but you want to perform integer division. Use int, long (or BigInteger) instead.
As others already mentioned never use Double for Integer calculation
Now If I were you I would have optimized my code to this
int check=0;
int n=num; // num is user-input number
while(n>0)//suppose n =153
{
int rem=n%10;
check=check+(int)Math.pow(rem,3);
n=n/10;
}
if(check==num)
System.out.println(num+" is Armstrong");
/*First time while loop runs rem = 3 and n= 15
So check = 0+3*3*3=27
Second time while loop runs rem = 5 and n= 1
So check = 27+5*5*5 = 152
Again n =1 so rem = 1 and check = 152+1*1*1 = 153
This time the thw while fails the exits
ultimately check == num so it is armstrong */
import java.util.Scanner;
public class Armstrong {
public static void main(String args[]) {
System.out.println("Input number of digit to find out Armstrong");
Scanner sc = new Scanner(System.in);
int r = sc.nextInt();
String s[] = new String[r];
System.out.println("Please enter digits");
StringBuilder sb = new StringBuilder(r);
for (int i = 0; i < r; i++) {
String userInput = sc.next();
s[i] = userInput;
sb.append(s[i]);
}
int e = Integer.parseInt(sb.toString()); //this is the Integer value entered to check Armstrong number
int d;
int k[] = new int[r];
for (int j = 0; j < r; j++) {
d = Integer.parseInt(s[j]);
k[j] = d * d * d * d* d* d;
}
int m[] = new int[r + 1];
int n[] = new int[r];
for (int l = 1; l <= r; l++) {
n[l - 1] = m[l - 1] + k[l - 1];
m[l] = n[l - 1];
}
if (e == m[r]) {
System.out.println("Entered number is Armstrong number");
} else {
System.out.println("Entered number is not an Armstrong number");
}
}
public void isArmstrong(String n)
{
char[] s=n.toCharArray();
int sum=0;
for(char num:s)
{
int i=Integer.parseInt(Character.toString(num));
int cube=i*i*i;
sum +=cube;
}
if(sum==Integer.parseInt(n))
{
System.out.println("Its an Armstrong Number");
}
else
{
System.out.println("Its not an Armstrong Number");
}
}
import java.util.*;
public class Main
{
public static void check_armstrong(int n)
{
/*Function to check whether a number is an armstrong number or not
Print true if yes else false */
int sum=0;
int temp=n;
while(n>0){
int remainder=n%10;
sum+=remainder*remainder*remainder;
n=n/10;
}
if(temp==sum){
System.out.println(true);
}else
System.out.println(false);
/* Do not change the code beyond this point*/
}
public static void main(String[]args)
{
Scanner sc=new Scanner(System.in);
int n =sc.nextInt();
check_armstrong(n);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
int a, c, d, e = 0, temp = 0;
a = sc.nextInt();
c = a;
int z = a;
while (c > 0) {
c = c / 10;
temp++;
}
System.out.println("//");
int temp2 = temp;
while (temp2 > 0) {
int temp1 = 1;
d = a % 10;
for (int i = 0; i < temp; i++) {
temp1 = temp1 * d;
}
e = e + temp1;
a = a / 10;
temp2--;
}
if (z == e) {
System.out.println("number is armstrong");
} else {
System.out.println("number is not armstrong");
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 5 months ago.
Improve this question
I could get the largest without using arrays but, unable to get the smallest one.
public static void main(String[] args)
{
int smallest=0;
int large=0;
int num;
System.out.println("enter the number");
Scanner input=new Scanner(System.in);
int n=input.nextInt();
for(int i=0;i<n;i++)
{
num=input.nextInt();
if(num>large)
{
large=num;
}
System.out.println("the largest is:"+large);
//gives the largest number in n numbers
code for the smallest..
if(i==0&&num>0)
small=num;
if(num<small)
small=num;
System.out.println(small);
}
Try this :
int smallest = Integer.MAX_VALUE;
for(int i=0;i<n;i++)
{
num=input.nextInt();
if(num>large)
{
large=num;
}
if(num<smallest){
smallest=num;
}
public static void main(String[] args) {
int smallest = 0;
int large = 0;
int num;
System.out.println("enter the number");//how many number you want to enter
Scanner input = new Scanner(System.in);
int n = input.nextInt();
num = input.nextInt();
smallest = num; //assume first entered number as small one
// i starts from 2 because we already took one num value
for (int i = 2; i < n; i++) {
num = input.nextInt();
//comparing each time entered number with large one
if (num > large) {
large = num;
}
//comparing each time entered number with smallest one
if (num < smallest) {
smallest = num;
}
}
System.out.println("the largest is:" + large);
System.out.println("Smallest no is : " + smallest);
}
Try this...This simple
import java.util.Scanner;
class numbers
{
public static void main(String args[])
{
int x, y, z;
System.out.println("Enter three integers ");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();
z = in.nextInt();
if ( x > y && x > z )
System.out.println("First number is largest.");
else if ( y > x && y > z )
System.out.println("Second number is largest.");
else if ( z > x && z > y )
System.out.println("Third number is largest.");
else
System.out.println("Entered numbers are not distinct");
}
}
public class Main {
public static void main(String[] args) {
int i = 10;
int j = 20;
int k = 5;
int x = (i > j && i > k) ? i : (j > k) ? j : k;
int y = (i < j && i < k) ? i : (j < k) ? j : k;
System.out.println("Largetst Number : "+x);
System.out.println("Smallest Number : "+y);
}
}
Output:
Largetst Number : 20
Smallest Number : 5
Try the code mentioned below
public static void main(String[] args) {
int smallest=0; int large=0; int num;
System.out.println("enter the number");
Scanner input=new Scanner(System.in);
int n=input.nextInt();
num=input.nextInt();
smallest = num;
for(int i=0;i<n-1;i++)
{
num=input.nextInt();
if(num<smallest)
{
smallest=num;
}
}
System.out.println("the smallest is:"+smallest);
}
#user3168844: try the below code:
import java.util.Scanner;
public class LargestSmallestNum {
public void findLargestSmallestNo() {
int smallest = Integer.MAX_VALUE;
int large = 0;
int num;
System.out.println("enter the number");
Scanner input = new Scanner(System.in);
int n = input.nextInt();
for (int i = 0; i < n; i++) {
num = input.nextInt();
if (num > large)
large = num;
if (num < smallest)
smallest = num;
System.out.println("the largest is:" + large);
System.out.println("Smallest no is : " + smallest);
}
}
public static void main(String...strings){
LargestSmallestNum largestSmallestNum = new LargestSmallestNum();
largestSmallestNum.findLargestSmalestNo();
}
}
import java.util.Scanner;
public class LargestSmallestNum {
public void findLargestSmallestNo() {
int smallest = Integer.MAX_VALUE;
int large = 0;
int num;
System.out.println("enter the number");
Scanner input = new Scanner(System.in);
int n = input.nextInt();
for (int i = 0; i < n; i++) {
num = input.nextInt();
if (num > large)
large = num;
if (num < smallest)
smallest = num;
System.out.println("the largest is:" + large);
System.out.println("Smallest no is : " + smallest);
}
}
public static void main(String...strings){
LargestSmallestNum largestSmallestNum = new LargestSmallestNum();
largestSmallestNum.findLargestSmalestNo();
}
}
import java.util.Scanner;
public class LargestSmallestNumbers {
private static Scanner input;
public static void main(String[] args) {
int count,items;
int newnum =0 ;
int highest=0;
int lowest =0;
input = new Scanner(System.in);
System.out.println("How many numbers you want to enter?");
items = input.nextInt();
System.out.println("Enter "+items+" numbers: ");
for (count=0; count<items; count++){
newnum = input.nextInt();
if (highest<newnum)
highest=newnum;
if (lowest==0)
lowest=newnum;
else if (newnum<=lowest)
lowest=newnum;
}
System.out.println("The highest number is "+highest);
System.out.println("The lowest number is "+lowest);
}
}