Printing all perfect and non perfect numbers 6 to n - java

I'm trying to print all from 6 to n but when i run the code it gives me all the numbers but not the right perfect numbers. For example when I enter 30 it prints out all numbers but it says only 6 and 7 are perfect numbers only 7 is not a perfect number and 28 is.
import java.util.Scanner;
public class PerfectN {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number that is 6 or above: ");
int n = scanner.nextInt();
int sum = 0;
if(n<6){
System.out.println("Incorrect number.");
}
else
for(int i = 6;i<=n;i++){
for(int j = 1; j<i; j++){
if(i%j==0)
sum += j;
}
if(sum==i){
System.out.println(i + " is a perfect number.");
}
else
System.out.println(i + " is not a perfect number.");
}
}
}

You need to reset the sum variable to zero for each number you go through:
for (int i = 6; i <= n; i++) {
int sum = 0;
for (int j = 1; j < i; j++) {

Related

Number Triangles with alternating number

Hello All,
I was wondering if someone could help me with making a number triangle in Java that looks like the one below using nested while loops. Would someone be able to help me out?
4
56
789
1234
56789
I have a variable 'i' on the outer loop determining how many rows the triangle will be and a variable 'j' on the inner loop determine which number the triangle will begin with. the numbers have to stay between [1-9].
Can anyone help me out?
Try This, It Will Work... It accepts rows & number through user and in first for loop it runs the loop till the number of rows and second loop print the number as per the value of I in pattern and if condition to check if the number is 10 then reset the number with 1 to start the numbering again.
import java.util.Scanner;
public class Pattern {
public static void main(String[] args) {
int rows, number = 1;
Scanner sc = new Scanner(System.in);
System.out.println("Enter no of rows");
rows = sc.nextInt();
System.out.println("Enter no to start with");
number = sc.nextInt();
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(number + " ");
++number;
if (number == 10) {
number = 1;
}
}
System.out.println();
}
}
}
Try.. r is for number of rows and v is the value
Scanner sc = new Scanner(System.in);
int r = sc.nextInt();
int v = sc.nextInt();
int i = v - 1;
int j = 1;
while(j != r + 1){
int k = 0;
int ans = 0;
while( k < j){
i = i + 1;
if(i == 10){
i = 1;
}
ans = ans * 10 + i;
k = k + 1;
}
System.out.println(ans);
j = j + 1;
}

2D Array: Magic Square

The question I am working on is:
A square with the side length n is filled with numbers 1,2,3,...n2 is a magic square if the sum of elements in each row, column, and two diagonals is the same value.
Write a program that reads in a value of 16 values from the keyboard and tests whether they form a magic square when put into a 4 x 4 array. You need to test the following 2 features:
Does each number of 1,2,...16 occur in user input? Tell the user to try again if they enter a number that they've already entered.
When the numbers are put into a square, are the sums of rows, columns, and diagonals equal to each other?
It must be done using two-dimensional array
I am having trouble with asking the user to try again if they enter a number that they have previously entered. And, numbers in 4 x 4 do not print.
What am I doing wrong? How can I fix it?
This is the code I have so far:
Scanner in = new Scanner (System.in);
int n =4;
int[][] square = new int[n][n];
int number = 0;
int num = 0;
for (int i = 0; i <16; i++){
number = num;
System.out.print ("Enter a number: ");
num = in.nextInt();
int num_2 = 0;
if (number==num || number==num_2) {
System.out.println ("Try again.");
System.out.println ("Enter a number: ");
num_2 = in.nextInt();
}
if (num > 16){
System.out.println ("Try again.");
break;
}
}
for (int i= 0; i < n; i++){
for (int j = 0; j < n; j++) {
num+=square [i][j];
System.out.print(square[i][j] + "\t");
}
}
}
}
You can try this code for 1st feature and add code for 2nd one.
int ar[][] = new int[4][4];
System.out.println("Enter Numbers");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
boolean flag = false;
int num = sc.nextInt();
if (num > 16 || num < 1) {
System.out.println("Please Enter number between 1 to 16");
flag=true;
j--;
} else {
for (int k = 0; k <= i; k++) {
for (int l = 0; l <= j; l++) {
if (ar[k][l] == num) {
System.out.println("This number already inserted...Please give another");
j--;
flag = true;
}
}
}
}
if (!flag) {
ar[i][j] = num;
}
}
}
If you can't understand anything please ask.
Hope this help.

Java Loop for n integer sum

I have problem with the following question:
Write a program that prompts the user to enter an integer, n. The
program will print the following results:
Sum of all even numbers between 1 and n (inclusive)
Sum of all odd numbers between 1 and n (inclusive)
Here the code I have so far, when I hit run I am getting exponentially large numbers that keep multiplying. I am aware my code is wrong. I dont know what I am doing wrong. Thank you.
package assig;
import java.util.Scanner;
public class Assignment4_Question1 {
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
System.out.println("Enter an integer: ");
int n = in.nextInt();
int evenSum = 0;
int oddSum= 0;
for (int i = 1; 1<=n; i++){
if(i % 2 == 0){
evenSum = evenSum + i;
} else if (i % 2 != 0){
oddSum = oddSum + i;
}
System.out.println(evenSum);
System.out.println(oddSum);
}
}
You loop is never ending !
for (int i = 1; 1<=n; i++)
So you are saying the loop will go infinte if you n is bigger than 1!
for (int i = 1; i<=n; i++)
The difference in here that you loop will go until i reach to n.
You must change :
for (int i = 1; 1<=n; i++)
to:
for (int i = 1; i<=n; i++)

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

Printing a triangle of prime numbers using for loop

I am trying to make a triangle of prime numbers .. the number of rows will be user defined
for example if user gives "4" as number of rows then the program should show a triangle of prime numbers containing 4 rows
input :4
output :
2
3 5
7 11 13
17 19 23 29
These are two tasks: get the list of primes (or just something like nextPrime(int)) and get the triangle, both are very simple.
For the implementation of first, you may look at Next Prime number Java only working with certain numbers
For the implementation of second, just do something like:
for (int i = 1; i <= n; ++i) {
for (int j = 0; j < i; ++j) {
nextPrime = ...
System.out.print (nextPrime + " ");
}
System.out.println ();
}
Let say you already have your primes (sufficient amount).
int[] primes = ...;
Then for N strings create array is StringBuilders and fill them up with your numbers:
StringBuilder[] builders = new StringBuilder[N];
int count = 0;
for(int row = 0; row < N; row++) {
int size = row + 1;
builders[row] = new StringBuilder();
for(int i = 0; i < size; i++) {
builders[row].append(primes[count++]);
builders[row].append(' ');
}
}
And then you may print them
for(StringBuilder sb : builders)
System.out.println(sb);
//please try these
import java.util.*;
public class primePtt1
{
public static boolean isPrimeNumber(int num) {
int c=0;
for (int i = 1; i <= num; i++) {
if (num % i == 0)
c++;
}
if (c==2)
return true;
else
return false;
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter no. of rows : ");
int rows = sc.nextInt();
int counter = 2;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
/* Try to find next prime number by incrementing counter and testing it for primality */
while(!isPrimeNumber(counter)){
counter++;
}
System.out.print(counter+" ");
counter++;
}
System.out.println();
}
}
}

Categories

Resources