I'm attempting to utilize a for loop in order to make a fibonacci sequence and unfortunately when I run the method (listed below), it does not start at 0 as it should. The first number is always 2 when it should be 0. If anyone could explain to me how to make the sequence begin with 0, that would be great. Thank you.
public static final void fibonacci(){
int num = 1;
int num2 = 1;
int tnum = 0;
int startnum = 1;
System.out.println("Please input a number:");
Scanner input = new Scanner(System.in);
int x = input.nextInt();
for(int i = 0; i < x; i++){
System.out.print(num + num2 + " ");
tnum = num;
num = num2;
num2 = tnum + num2;
}
}
You should start with
num = 0
and then print num, and modifying num (then in your i-th loop, num is the i-th fibonacci number :
public static final void fibonacci() {
int num = 0;
int num2 = 1;
int tnum = 0;
int startnum = 1;
System.out.println("Please input a number:");
Scanner input = new Scanner(System.in);
int x = input.nextInt();
for (int i = 0; i < x; i++) {
System.out.print(num);
tnum = num + num2;
num = num2;
num2 = tnum;
}
}
public static final void fibonacci(){
int num = -1; //was "1"
int num2 = 1;
int tnum = 0;
int startnum = 1;
System.out.println("Please input a number:");
Scanner input = new Scanner(System.in);
int x = input.nextInt();
for(int i = 0; i < x; i++){
System.out.print(num + num2 + " ");
tnum = num;
num = num2;
num2 = tnum + num2;
}
}
here is the output i got:
*Please input a number:
9
0 1 1 2 3 5 8 13 21*
Related
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);
}
}
In the code you need to take 100 straight-angle triangles and print the biggest triangle. What am I not doing right?
import java.util.Scanner;
public class rthji {
/**
* #param args
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num = 0;
int num1 = 0;
int num2 = 0;
int num3 = 0;
int num4 = 0;
int num5 = 0;
System.out.println("Insert the length of the small side of the triangle");
num = in.nextInt();
System.out.println("Insert the length of the big of the triangle");
num1 = in.nextInt();
System.out.println("Insert the length of the last side of the triangle");
num2 = in.nextInt();
for (int i = 1; 1 >= 10; i++) {
System.out.println("Insert the length of the small side of the triangle");
num3 = in.nextInt();
System.out.println("Insert the length of the big of the triangle");
num4 = in.nextInt();
System.out.println("Insert the length of the last side of the triangle");
num5 = in.nextInt();
if (num3 * num3 + num5 * num5 != num4 * num4) {
num3 = num4 = num5 = 0;
} else if (num3 > num && num1 < num4 && num2 < num5) {
num = num3;
num1 = num4;
num2 = num5;
}
System.out.println("The ribs of the largest triangle are:" + num1 + (",") + num2 + (",") + num3);
}
}
}
Your for loop has a error and it should be changed to
for(int i=1;1<=10;i++)
In your case you are using 1 >= 10 which is logically wrong so your loop worries that it cannot go ahead because 1 is less than 10
Change your for loop like below
for(int i=1; i<=10; i++)
I am a beginner trying to program a java code to produce output numbers that is a prime factor of two and five.
For example, if input is 8, then output should be 2 4 5 8.
However, whenever I print my output, the result will be 2 5 4 5 8 5.
Please advice on where I have gone wrong.
Thank you
import java.util.Scanner;
class twofive {
public static void main(String [] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter n:");
int n = sc.nextInt();
double num = 0;
double num2 = 0;
for (int i = 1; (((Math.pow(2,i))<= n) || ((Math.pow(5,i)) <=n) || (((Math.pow(2,i))<= n) && ((Math.pow(5,i)) <=n))) ; i++) {
if (( Math.pow(2,i)) <= n)
num = (Math.pow(2,i));
int convert = (int) num;{
System.out.print(convert + " ");
}
if ((Math.pow(5,i)) <= n)
num2 = (Math.pow(5,i));
int convert2 = (int) num2;
{System.out.print(convert2 + " ");
}
}
}
}
After you review #AmedeeVanGasse 's comment, you need to fix your braces.
public static void main(String [] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter n:");
int n = sc.nextInt();
double num = 0;
double num2 = 0;
for (int i = 1; Math.pow(2,i))<= n) || ((Math.pow(5,i)) <=n) || (((Math.pow(2,i))<= n) && ((Math.pow(5,i)) <=n))) ; i++) {
if (( Math.pow(2,i)) <= n) {
num = (Math.pow(2,i));
int convert = (int) num;
System.out.print(convert + " ");
}
if ((Math.pow(5,i)) <= n) {
num2 = (Math.pow(5,i));
int convert2 = (int) num2;
System.out.print(convert2 + " ");
}
}
}
You should also review the logic in your for loop and if statements. They are full of unneeded redundancies.
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 am working on an assignment which prompts the user to input an integer, and displays that integer with the digits separated by spaces, and provides the sum of those digits. I have this working, but my individual digit display is form the last digit to the first. How can I make it display the digits from first to last?
Here is what I have so far:
import java.util.*;
public class SeparateAndSum{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
int num, temp, sum;
System.out.print("Enter a positive interger: ");
num = console.nextInt();
System.out.println();
temp = num;
sum = 0;
do
{
temp = num % 10;
sum = sum + num % 10;
num = num / 10;
System.out.print(" " + temp + " ");
}while (num > 0);
System.out.println("The sum of the digits = " + sum);
}
}
One option would be to use the String#valueOf(Integer) method.
Example
int input = 12345;
String inputStr = String.valueOf(input);
for(char c : inputStr.toCharArray()) {
// Print out each letter.
}
if you use the method String.valueOf(12345)
you can easily reverse the String with the method in this library:
https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringUtils.html#reverse(java.lang.String)
StringUtils.reverse(String.valueOf(input));
Here is the solution
import java.util.*;
public class SeparateAndSum{
static Scanner console = new Scanner(System.in);
int num, temp, sum;
System.out.print("Enter a positive interger: ");
num = console.nextInt();
System.out.println();
temp = num;
sum = 0;
ArrayList<Integer> values = new ArrayList<>();
do
{
temp = num % 10;
sum = sum + num % 10;
num = num / 10;
values.add(temp);
}while (num > 0);
Collections.reverse(values);
Iterator<Integer> it = values.iterator();
while(it.hasNext()){
System.out.println(" "+it.next()+" ");
}
System.out.println("The sum of the digits = " + sum);
}
}
BTW you should have to import ArrayList etc.
I would highly recommend putting the number into a String and then reading it, parsing it, and use the number however you want. As follows,
int input = 12345;
String inputString = input + "";
int sum = 0;
for (int i = 0; i < inputString.length(); i++) {
sum += Integer.parseInt(inputString.charAt(i) + "");
}
System.out.println(sum);
However an alternative way, not as pretty is..
int input = 12345;
int sum = 0;
while (input > 0) {
int i = (input + "").length() - 1;
int n = (int) (input / Math.pow(10, i));
input -= (int) (n * Math.pow(10, i));
sum += n;
}
System.out.println(sum);