I tried to write code for an factorial number program,following code
public class Factorial {
public static void main(String[] args) {
Scanner in = new Scanner(System. in );
System.out.println("Enter the number whose factorial you want: ");
int n = in .nextInt();
int f = 1;
for (int i = n; i & gt; 0; i--) //error show what's wrong in for loop {
f = f * i;
}
System.out.println("Factorial of " + n + " is " + f);
}
}
Yes your error point is & gt; which is not an operator actual operator is >. > is XML encoded from of >.
Use this:
for (int i = n; i > 0; i--)
Instead of
for (int i = n; i & gt; 0; i--)
This happens usually when you copy past code from somewhere.
Complete code should be:
public class Factorial {
public static void main(String[] args) {
Scanner in = new Scanner(System. in );
System.out.println("Enter the number whose factorial you want: ");
int n = in .nextInt();
int f = 1;
for (int i = n; i > 0; i--) //error show what's wrong in for loop {
f = f * i;
}
System.out.println("Factorial of " + n + " is " + f);
}
}
Related
I'm currently using this code, and I'm trying to output the total sum of how many prime numbers first and then the prime numbers list
Here is my code:
import java.util.*;
public class PrimeTime {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int r, s = 0;
System.out.print("Input number: ");
r = sc.nextInt();
System.out.println();
for (int i = 2; i < r; i++) {
int f = 0;
for (int j = 2; j < i; j++) {
if (i%j == 0)
f = 1;
}
if (f == 0) {
System.out.print(i+ " ");
s = s +1;
}
}
System.out.println("N =" +s);
}
}
This is the result I get:
Input number: 10
2 3 5 7 N = 4
But what I'm looking for is this result:
Input number: 10
N = 4
2 3 5 7
I don't know how, I tried putting the S.O.P in different places and I can't figure out how.
If you want to display N = 4 before 2 3 5 7, you can append the numbers (i.e. 2 3 5 7) to a StringBuilder and print the same after printing N = 4 e.g.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int r, s = 0;
StringBuilder numbers = new StringBuilder();
System.out.print("Input number: ");
r = sc.nextInt();
System.out.println();
for (int i = 2; i < r; i++) {
int f = 0;
for (int j = 2; j <= Math.sqrt(i); j++) {
if (i % j == 0)
f = 1;
}
if (f == 0) {
numbers.append(i).append(' ');// Append to StringBuilder instead of printing
s = s + 1;
}
}
System.out.println("N =" + s);
System.out.println(numbers);
}
}
A sample run:
Input number: 10
N =4
2 3 5 7
Note: for checking the primality, checking up to Math.sqrt(i) is sufficient i.e. you should replace j < i with j <= Math.sqrt(i).
You can store the numbers in a list and then print them after the loop is over
Don't worry if you have not studied lists yet cause you will have to soon anyways.
here's the code:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int r, s = 0;
LinkedList<Integer> list = new LinkedList<>();
System.out.print("Input number: ");
r = sc.nextInt();
System.out.println();
for (int i = 2; i < r; i++) {
int f = 0;
for (int j = 2; j < i; j++) {
if (i%j == 0)
f = 1;
}
if (f == 0) {
//System.out.print(i+ " ");
list.add(i);
s = s +1;
}
}
System.out.println("N = " +s);
for(int i = 0; i<list.size(); i++ ) {
System.out.print(list.get(i) + " ");
}
}
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
This is what I have so far but for some reason the program states the operation is complete without allowing the user to enter 1 or 2 to continue or not. Thanks in advance..
My code:
import java.util.Random;
public final class Kidwell_Lab09 {
public static Random generator = new Random();
public static void main(String[] args) {
int x;
int[] randomNumbers = new int[20];
do
{
Random generator = new Random();
for (int i = 0; i < randomNumbers.length; i++){
int n = generator.nextInt(10)+1;
randomNumbers[i] = n;
}
for (int i = 0; i < randomNumbers.length; i++) {
System.out.println("Number " + i + " : " + randomNumbers[i]);
}
System.out.print("Do you wish to restart the program, Enter 1 for YES, 2 for NO: ");
x = generator.nextInt();
}
while ( x == 1 );
}
}
You should be reading the value from System.in rather than from generator.nextInt().
Here is the corrected code.
import java.util.Random;
public final class Kidwell_Lab09 {
public static Random generator = new Random();
public static void main(String[] args) {
int x;
int[] randomNumbers = new int[20];
Scanner inputReader = new Scanner(System.in);
do {
Random generator = new Random();
for (int i = 0; i < randomNumbers.length; i++) {
int n = generator.nextInt(10) + 1;
randomNumbers[i] = n;
}
for (int i = 0; i < randomNumbers.length; i++) {
System.out.println("Number " + i + " : " + randomNumbers[i]);
}
System.out.print("Do you wish to restart the program, Enter 1 for YES, 2 for NO: ");
x = inputReader.nextInt();
} while (x == 1);
}
}
Getting the second highest and second lowest java without using array array sort or any sorting method, min_value and max_value just for loop and if statement I already started to code this is what ive done so far i cant think of how can i get the second lowest and second highest
package Lab2;
import java.util.Scanner;
public class hehe {
public static void main(String[] args) {
int fh, sh, fl, sl, x;
Scanner s = new Scanner(System.in);
System.out.print("Enter 5 numbers: ");
x = fh = sh = sl = fl = s.nextInt();
int a,b,c,d;
int mid = 0;
for (int i = 0; i < 4; ++i) {
int n = s.nextInt();
if (fh < n) {
fh = n;
}
if (fl > n) {
fl = n;
}
for(int y=0;y<5;y++){
}
}
System.out.println("The First highest is: " + fh);
System.out.println("The Second highest is: " + sh);
System.out.println("The Second lowest is: " + sl);
System.out.println("The First lowest is: " + fl);
System.out.println(mid);
}
}
Just improve upon your condition to cover all cases,
// Initialize your variables like this,
fh = sh = Integer.MIN_VALUE;
fl = sl = Integer.MAX_VALUE;
// Change your for loop condition to below and get input only inside the loop. Not before it.
for (int i = 0; i < 5; ++i) {
if (fh < n) { // It means you have received the highest known number
sh = fh; // The existing highest becomes the second highest now
fh = n; // n should now be the (first) highest rightfully
} else if (sh < n) { // This means n was not the highest, but second highest
sh = n;
}
// Do the same for lowest also.
Obviously, sorting the array is the obvious solution so I assume this is some sort of test assignment. Anyway, I guess the easiest way is just to have variables for both highest and 2nd highest (and same for lowest). For example, to find the 2nd highest:
List<Integer> values = new ArrayList<>();
for(int i = 0; i < 20; i++) {
values.add((int) (Math.random() * 10));
}
int highest = Integer.MIN_VALUE;
int second = Integer.MIN_VALUE;
for(int n: values) {
if(n > highest) {
second = highest;
highest = n;
} else if(n > second) {
second = n;
}
}
System.out.println("2nd highest: " + second);
if (fh < n) {
sh = fh;
fh = n;
}else if (sh < n)
{
sh = n;
}
if (fl > n) {
s1=f1
fl = n;
}else if (s1 > n)
{
s1 = n;
}
You need to also update the value of variable accordingly:
public static void main(String[] args) {
int fh, sh, fl, sl, x;
Scanner s = new Scanner(System.in);
System.out.print("Enter 5 numbers: ");
x = fh = sh = sl = fl = s.nextInt();
int a,b,c,d;
int mid = 0;
for (int i = 0; i < 4; ++i) {
int n = s.nextInt();
if (fh < n) {
sh = fh; // We have got new fh so sh is old value of fh.
fh = n;
}
if (fl > n) {
sl = fl;
fl = n;
}
}
System.out.println("The First highest is: " + fh);
System.out.println("The Second highest is: " + sh);
System.out.println("The Second lowest is: " + sl);
System.out.println("The First lowest is: " + fl);
}
I need some help with my java project. What I want to do is to let the user put in a number and my program will then print that number starter from 1. I have also added field width to 5. What I want to do now is to make line jumps: For example the first line will have 1 character, the next will have 2, then next will have 3 and so on. The field width from the start will also increase on every line. Here is my code:
import java.util.Scanner;
public class ProjectB {
public static void main(String[] args) {
printNumbersB(0);
}
public static void printNumbersB(int x){
Scanner input = new Scanner(System.in);
System.out.print("Please put in: ");
x = input.nextInt();
for(int y = 1; y <= x; y++){
System.out.printf("%5d", y);
input.close();
}
}
}
How output should be:
http://imgur.com/2QLTRlA
Try this:
int n = 45;
int counter = 1;
for (int i = 1; i < n; i++) {
for (int j = i; j < counter + i; j++) {
System.out.printf("%" + counter + "d", j);
}
i += counter - 1;
System.out.println();
counter++;
}
I do not understand what you want to do. But from what I understand this is what you want:
public static void main(String[] args) {
printNumbersB();
}
public static void printNumbersB(){
Scanner input = new Scanner(System.in);
System.out.print("Please put in: ");
x = input.nextInt();
//Should use String Builder
String accumlationString = "";
for(int y = 1; y <= x; y++){
System.out.printf(accumulationString + "%5d", y);
accumulationString = accumulationString + "%5d";
;
input.close();
}
}
}
FINAL EDIT:
This works but their is a more efficient way to do this:
public static void printNumbersB() {
Scanner input = new Scanner(System.in);
System.out.print("Please put in: ");
int x = input.nextInt();
input.close();
//Should use String builder
String tab = "";
int lineNumber;
int num = 1;
breakfor:
for (int i = 1; i <= x; i++) {
System.out.println();
lineNumber = i;
tab = tab + " ";
for (int j = lineNumber; j > 0; j--) {
System.out.print(tab + num);
num = num + 1;
if(num > x)
break breakfor;
}
}
}
public static void main(String[] args) {
printNumbersB();
}