Not printing perfect numbers - java

Here is my code :-
package javaapplication;
import java.util.Scanner;
public class perfect02 {
public static void main(String args[]) {
int i = 1, sum = 0;
System.out.println("Enter maximum range : ");
Scanner kb = new Scanner(System.in);
int a = kb.nextInt();
System.out.println("Enter minimum range : ");
Scanner kb2 = new Scanner(System.in);
int b = kb2.nextInt();
System.out.println("perfect number in the given range are :");
for (int n = b; n <= a; n++) {
while (i < n) {
if (n % i == 0) {
sum = sum + i;
}
i++;
}
if (sum == n)
System.out.println(+n + " ");
}
}
}
Why program is not printing perfect numbers ?
I have checked code many times but i am unable to find the solution .Please tell me what is going wrong in my code .Thanks in advance
Any help would be appreciated ....

Here, I looked into the perfect number generation and fixed it for you!
public static void main(String args[]) {
Scanner kb = new Scanner(System.in);
System.out.println("Enter minimum range : ");
int b = kb.nextInt();
System.out.println("Enter maximum range : ");
int a = kb.nextInt();
kb.close();
System.out.println("Perfect number in the given range are :");
for (int n = b; n <= a; n++) {
int sum = 0;
int i = 1;
while (i < n) {
if (n % i == 0)
sum = sum + i;
i++;
}
if (sum == n)
System.out.println(n + " is perfect");
}
}
You should've had the sum and i variables declared inside the for loop, so they would be reset for each number!

Related

I need to display prime numbers within a range using min. of 1 while & for loop. And getting the wrong output

import java.util.Scanner;
public class PrimeN {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int a, b;
a = sc.nextInt();
b = sc.nextInt();
while (a < b) {
for (int i = a; i <= b; i++) {
int count = 0;
for (int j = 1; j <= i; j++) {
if (i % j == 0) {
count++;
}
} //count if equals 2 the prime numbers displayed
if (count == 2) {
System.out.println(i);
a++;
}
}
}
}
}
This is what my output looks like. I dont understand why last prime numbers are being repeatedly printed.
What you should probably do is breaking task into steps.
Get the input
satrt finding the prime numbers from 1 to the max
create a list of already found prime numbers
start a loop from 1 to max
each iteration loop trough already found primes and perform number % prime == 0
if none of numbers in prime numbers can fulfils the condition add number to the list
if found number is greater or equal to min, save the index of it (startIndex).
print the input by iterating trough list from startIndex and printing the numbers
this workflow guarantees you will not consider multiplicant of two big prime numbers as prime number with better performance
You missplaced your first statement, your while-loop is a little bit scary, but here your fix:
import java.util.Scanner;
public class PrimeN{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int a,b;
a = sc.nextInt();
b = sc.nextInt();
for(int i = a; i <= b; i++){
int count = 0;
for(int j = 1; j <= i; j++){
if(i%j == 0){
count++;
}
}//count if equals 2 the prime numbers displayed
if(count == 2){
System.out.println(i);a++;
}}}}
Found a way to solve the ques.
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a, b;
a = sc.nextInt();
b = sc.nextInt();
for(int n = a; n <= b; n++){
boolean prime = true;
int i = 2; //1 will be a factor always
while(i <= n/2){
if(n%i == 0){
//is not prime
prime = false;
break;
}i++;
}
if(prime){
System.out.print(n+ " ");
}
}}}
Just remove your while(a<b) around the two for loops and then your code works fine
like this
import java.util.Scanner;
public class PrimeN {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int a, b;
a = sc.nextInt();
b = sc.nextInt();
// System.out.println("a<b");
int i = a;
while(i<=b)
{
int count = 0;
for (int j = 1; j <= i; j++) {
if (i % j == 0) {
count++;
}
} // count if equals 2 the prime numbers displayed
if (count == 2) {
System.out.println("a" + a + " i:" + i);
a++;
}
i++;
}
}
}
To find out what the actual error is, print out the values of a,b,i and j in each iteration.

How do I break up my numbers into multiple lines?

Run the program with starting number=13, upper bound= 112, step_size=3
Continue printing numbers as long as number
I've tried to split it and use break without success for this task.
public class Bounds {
public static void main(String[] args) {
int startingNumber;
int upperBound = 112;
int stepSize =3;
int count =0;
Scanner input = new Scanner(System.in);
System.out.println("Enter starting number: ");
startingNumber=input.nextInt();
System.out.println("Enter upper bound number");
upperBound = input.nextInt();
System.out.print(startingNumber + " ");
System.out.print(" ");
while (startingNumber <= upperBound) {
System.out.print((startingNumber += stepSize) + " ");
if ((count%10) == 0)
count += 3;
System.out.print( " ");
input.close();
}
}
}
too complicated, what about using a for loop
public static void main(String[] args) {
int startingNumber;
int upperBound = 112;
int stepSize =3;
int count =0;
Scanner input = new Scanner(System.in);
System.out.println("Enter starting number: ");
startingNumber = input.nextInt();
System.out.println("Enter upper bound number");
upperBound = input.nextInt();
int range = upperBound - startingNumber;
for (int i = startingNumber; i < upperBound; i+=stepSize) {
System.out.println(i);
count++;
}
System.out.println("done: count value: "+count);
}
What did you have in mind for the following?
if ((count%10) == 0)
count += 3;
So you only increment count when the remainder is zero. That would only work once. But what other action do you take? Perhaps you meant do do something like this.
count += 3;
if ((count%10 == 0) {
System.out.println();
}

Java sum from 1 to n with output showing work

I have a homework problem that I've almost finished, but I'm just stuck on how to output it correctly.
Write a program that reads a positive integer n and prints the sum of all integers from 1 to n as follows:
1+2+…+n=n(n+1)/2
The output does not contain any spaces.Example of input: 5 Corresponding output: 1+2+3+4+5=15
Here's my code:
import java.util.Scanner;
public class Homework2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int sum = 0;
for (int i = 0;i <= n; i++) {
sum = sum + i;
}
System.out.printf("the sum of %d is %d%n", n, sum);
}
}
What I have in the printf command is just a placeholder until I can figure out the correct output.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 1;i <= n; i++) {
if (i != n) {
System.out.print(i + "+");
} else {
System.out.print(i + "=");
}
}
System.out.print(n*(n+1)/2);
}
the above will work. You must be strict to the expected output.
You need to print within the loop.
And you should start at 1, like the instructions say
for (int i = 1;i < n; i++) {
sum = sum + i;
System.out.printf("%d+", i);
}
sum += n;
System.out.printf("%d=%d\n", n, sum);
You can deal with the case of printing n and = separately like so:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Print the sum of all integers from 1 to n program");
System.out.println("=================================================");
System.out.print("Please enter n: ");
int n = scanner.nextInt();
int sum = 0;
for(int i = 1; i < n; i++) {
System.out.print(i + "+");
sum += i;
}
sum += n;
System.out.print(n + "=" + sum);
}
}
Try it here!
Example output:
Print the sum of all integers from 1 to n program
=================================================
Please enter n: 5
1+2+3+4+5=15

Create a java program to display multiples of a number

I'm trying to Write the main method of a Java program that has the user enter two integers, i and n. If either integer is less than 2, output “Please enter numbers above 1.” Otherwise, output the n positive multiples of i, separated by spaces.
I'm close but can't figure out how to do display the multiples.
Here's what a sample run should look like:
Enter i: 4
Enter n: 6
6 multiples of 4 are: 8 12 16 20 24 28
import java.util.*;
public class HW5Problem3 {
public static void main (String [] args) {
int i = 0;
int n = 0;
Scanner input = new Scanner(System.in);
System.out.print("Enter i: ");
i = input.nextInt();
System.out.print("Enter n: ");
n = input.nextInt();
if ((i <= 1) || (n <= 1)) {
System.out.println("Please enter numbers above 1");
System.exit(1);
}
System.out.print(n + " multiples of " + i + " are: ");
}
}
import java.util.Scanner;
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int i = 0;
int n = 0;
//It's use to verify the inputs (i and n).
do{
System.out.print("Enter i :");
i = input.nextInt();
System.out.print("\nEnter n :");
n = input.nextInt();
if(i >= 1 || n <= 1){
System.out.println("Please enter numbers above 1 \n");
}
}while(i <= 1 || n <= 1);
System.out.print(n + " multiples of " + i + " are: ");
for (int counter = 0 ; counter < n ; counter++) {
System.out.print(i*(2 + counter) + " ");
}
}
You'll need to create a loop (for loop or while loop) to iterate from 2 to n+1, and multiply i by your loop variable, outputting each value inside the loop
U can use following method in that class
public static void mult(int i,int n){
int[] arr=new int[n];
int count=2;
for(int x=0;x<n;x++){
arr[x]=i*count++;
}
for(int y=0;y<arr.length;y++){
System.out.print(arr[y]+" ");
}
and now your final code looks like
import java.util.*;
public class HW5Problem3 {
private int i = 0;
private int n = 0;
public static void main(String[] args) {
int i = 0;
int n = 0;
Scanner input = new Scanner(System.in);
System.out.print("Enter i: ");
i = input.nextInt();
System.out.print("Enter n: ");
n = input.nextInt();
if ((i <= 1) || (n <= 1)) {
System.out.println("Please enter numbers above 1");
System.exit(1);
} else {
System.out.print(n + " multiples of " + i + " are: ");
mult(i, n);
}
}
public static void mult(int i, int n) {
int[] arr = new int[n];
int count = 2;
for (int x = 0; x < n; x++) {
arr[x] = i * count++;
}
for (int y = 0; y < arr.length; y++) {
System.out.print(arr[y] + " ");
}
}
}
n is the multiplier, i is the factor, right? In programming the multiplier is the loop maximum:
System.out.print(n + " multiples of " + i + " are: ");
for (int inc=1; inc<=n; inc++) {
System.out.print(" " + i*inc);
}
This prints out: 4 8 12 16 20 24
If you really want to have this as output: 8 12 16 20 24 28 copy/paste this line:
for (int inc=2; inc<=(n+1); inc++)

Factorial Issue

I am new at coding Java, but I need to write a program that is from integer 1 to n. The program would ask the user to enter a positive number and if it is not positive then it will ask for another number.
Once the positive integer is entered for n in the program, it shows how the n factorial is computed followed by the result.
So my program will show everything correct except the result, it is not multiplying all the numbers together. If someone can point me in the right direction on how to get this solved that would be great!
CODE:
import java.util.Scanner;
public class Problem5 {
public static void main(String[] args){
int n, i =1;
Scanner kbd = new Scanner(System.in);
System.out.print("Enter n: ");
n = kbd.nextInt();
while (n <= 0) {
System.out.print("Enter n: ");
n = kbd.nextInt();
}
for (i = 1; i <= n; i++){
System.out.print( i+ "*");
}
System.out.print(" is " + n * i);
}
}
Output:
Enter n: 5
1*2*3*4*5* is 30
As you can see for the result it should be 120 and not 30.
Just change that part
for (i = 1; i <= n; i++){
System.out.print( i+ "*");
}
System.out.print(" is " + n * i);
for
int result = 1;
for (i = 1; i <= n; i++){
System.out.print( i+ "*");
result *= i;
}
System.out.print(" is " + result);
Your last print was wrong as you simply multiplied n with i which is a simple multiplication and have nothing to do with factorial.
Your program is doing exactly one computation ( " is " + n * i) and this computation is not doing a factorial. You probably want to do the multiplication more than once - and with different numbers.
You are not doing the calculation properly. You just show the end result of n*i`.
In the below solution, I've taken an int fact = 1 and I'm multiplying it with the value of i inside the for loop and assigning back the result to factvariable. That's the core part. Thats how you get 1*2*3...*n = n!
import java.util.Scanner;
public class SomeArrayQuestion {
public static void main(String[] args) {
int n, i = 1;
Scanner kbd = new Scanner(System.in);
System.out.print("Enter n: ");
n = kbd.nextInt();
while (n <= 0) {
System.out.print("Enter n: ");
n = kbd.nextInt();
}
int fact = 1;
for (i = 1; i <= n; i++) {
System.out.print(i + "*");
fact = fact * i;
}
System.out.print(" is " + fact);
}
}
import java.util.Scanner;
public class Problem5 {
public static void main(String[] args){
int n, i =1;
Scanner kbd = new Scanner(System.in);
System.out.print("Enter n: ");
n = kbd.nextInt();
while (n <= 0) {
System.out.print("Enter n: ");
n = kbd.nextInt();
}
int result = 1;
for (i = 1; i <= n; i++){
System.out.print( i+ "*");
result *= i;
}
System.out.print(" is " + result);
}
}
Output:
Enter n: 5
1*2*3*4*5* is 120

Categories

Resources