Output is wrong it just multiplying second number with itself.
import java.util.Scanner;
public class trybew {
void factorial(int n) {
long fact = 1;
for(int i=1; i<=n; i++) {
fact *= n;
System.out.println(" "+fact);
}
}
public static void main(String[] args) {
int cnt;
trybew f1= new trybew();
Scanner s= new Scanner(System.in);
System.out.println("Enter Test case ");
cnt=s.nextInt();
int n[]= new int[cnt];
for(int i=0; i<cnt; i++) {
System.out.println("ENter NO:: ");
n[i]=s.nextInt();
}
for(int i=0; i<cnt; i++)
f1.factorial(n[i]);
}
}
void factorial(int n) {
long fact = 1;
for(int i=n; i>=1; i--)
fact *= i;
System.out.println(" "+fact);
}
Your mistake is with your code fact *= n; . You are supposed to use i here.
Change
fact *= n;
To
fact *= i;
Modified code :-
import java.util.Scanner;
public class trybew {
void factorial(int n) {
long fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i; // not *=n
System.out.println(" " + fact);
}
}
public static void trybew(String[] args) {
int cnt;
trybew f1 = new trybew();
Scanner s = new Scanner(System.in);
System.out.println("Enter Test case ");
cnt = s.nextInt();
int n[] = new int[cnt];
for (int i = 0; i < cnt; i++) {
System.out.println("ENter NO:: ");
n[i] = s.nextInt();
}
for (int i = 0; i < cnt; i++)
f1.factorial(n[i]);
}
}
Output :-
Enter Test case
2
ENter NO::
4
ENter NO::
5
1
2
6
24
1
2
6
24
120
It is better to use System.out.println(" " + fact); outside the for-loop in function void factorial(int n) .
Related
im suppose to Ask from The user enters two integers, and inputs them through the console
The first is n, and the second is m. Print all the prime numbers up to n that are in between
The difference is exactly m.
this is my code:
import java.util.Scanner;
public class EX1_A {
public static void main(String[] args)
{
Scanner in = new Scanner (System.in);
System.out.println("please enter the the limit if the prime number you want");
int n = in.nextInt();
System.out.println("please enter the gab");
int m = in.nextInt();
int arr [] = new int [n];
int c= 0;// c is arr.length
for(int i=2;i<=n;i++)
{
int j = 0;
j++;
if(Prime(i))
{
arr[j]=i;
c++;
}
}
int k = c;
while(k>0)
{
for( int j = 0; j<k;j++)
{
if(arr[k]-arr[j]==m)
{
System.out.println("the prime numbers with the gap " +m+ " is: {"+arr[k]+","+arr[j]+"}");
}
}
k--;
}
}
public static boolean Prime(int number)
{
for(int i=2; i<number; i++)
{
if(number%i == 0)
return false;
}
return true;
}
}
This version works:
public static void main(String[] args)
{
Scanner in = new Scanner (System.in);
System.out.println("please enter the the limit if the prime number you want");
int n = in.nextInt();
System.out.println("please enter the gab");
int m = in.nextInt();
int arr [] = new int [n];
int c= 0;// c is arr.length
for(int i=2;i<=n;i++)
{
if(Prime(i))
{
arr[c]=i;
c++;
}
}
int k = c;
while(k>0)
{
for( int j = 0; j<k;j++)
{
if(arr[k]-arr[j]==m)
{
System.out.println("the prime numbers with the gap " +m+ " is: {"+arr[k]+","+arr[j]+"}");
}
}
k--;
}
}
public static boolean Prime(int number)
{
for(int i=2; i<number; i++)
{
if(number%i == 0)
return false;
}
return true;
}
Change internal if where you find and save prime numbers.
int c = 0;// c is arr.length
for (int i = 2; i <= n; i++) {
if (Prime(i)) {
arr[c] = i;
c++;
}
}
My code is O(n^2), please to decrease as O(n).
My question is count of the left side odd numbers and right side odd numbers. If it is equal on the bothsides(left and right) then print the respecive element other wise print the "-1". my code logic was correct it executing the normal text cases, but it was failing to execute the large value of text cases.It shows the error as "time exceeded".It was exceeding the timelimit of the compiler.i want to decrease the time limit of my code.
example: my input is 4 1 4 3 8 and my output:-1 4 -1 -1
example 2: my input is 6 1 3 4 8 5 7 and my output: -1 -1 4 8 -1 -1
enter code here
import java.util.*;
public class Hello {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int temp = 0;
int count = 0;
int flag = 0;
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = s.nextInt();
}
for (int i = 0; i < n; i++) {
temp = a[i];
for (int j = 0; j < i; j++) {
if (a[j] % 2 != 0) {
count++;
}
}
for (int k = i + 1; k < n; k++) {
if (a[k] % 2 != 0) {
flag++;
}
}
if (count == flag) {
System.out.print(temp + " ");
} else {
System.out.print("-1 ");
}
count = 0;
flag = 0;
}
}
I have not tested it yet but it should work.
Your algorithm is O(n^2) and this one is O(n).
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++) {
a[i]=s.nextInt();
}
int total = 0;
for(int i=0;i<n;i++) {
if(a[j]%2!=0) {
total++;
}
}
int leftTotal = 0;
for(int i=0;i<n;i++) {
int k = a[i];
if (k%2!=0) {
leftTotal++;
}
if (leftTotal = (total-leftTotal)) {
System.out.println(k);
} else {
System.out.println(k);
}
}
}
enter code here
import java.util.*;
public class Hello {
public static void main(String[] args) {
//Your Code Here
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int a[]=new int[n],flag=0,count=0;
for(int i=0;i<n;i++) {
a[i]=s.nextInt();
if(a[i]%2!=0)
count++;
}
for(int i=0;i<n;i++) {
if(a[i]%2!=0)
count--;
if(flag==count)
System.out.print(a[i]+" ");
else
System.out.print("-1 ");
if(a[i]%2!=0)
flag++;
}
}
}
Now how do i add the numbers displayed and print them ?
i got how to display them but cant figure out how to add the number please would be a great help for my project
import java.util.Scanner;
public class ReadNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Number: \n");
int n = sc.nextInt();
int i = 0;
while(i <= n){
System.out.print(i);
if(i == n){
System.out.print("=");
}
else{
System.out.print("+");
}
i++;
}
}
}
You need another variable for the running total. To be more concise, use a for loop, and handle the i == n outside of the loop.
Scanner sc = new Scanner(System.in);
System.out.println("Enter Number: \n");
int n = sc.nextInt();
int i = 0, sum = n;
for (int i = 0; i < n; i++) {
sum += i;
System.out.print(i + "+");
}
System.out.println(n + "=" + sum);
You can create another variable int sum to update the sum of all i's with each loop iteration:
import java.util.Scanner;
public class ReadNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Number: \n");
int n = sc.nextInt();
int i = 0;
int sum = 0; // Create a sum variable
while(i <= n){
System.out.print(i);
sum+=i; // Add `i` to sum
if(i == n){
System.out.print("=");
System.out.println(sum); // Display `sum` after loops finish
}
else{
System.out.print("+");
}
i++;
}
}
}
So I'm beginning in the world of java programming language and I'm trying to print a christmas tree of X height. So far its working, but if for example the user input 4, it will print 4 rows + the christmas tree stump, wich mean 5. However, I would like it to be 4 INCLUDING the stump.So far I have this:
public class xmas {
public static void main(String[] args)
{
Scanner scan = new Scanner(in);
out.print("please enter a number: ");
int temp = scan.nextInt();
int x = (temp-1)*2 +1;
int y = x/2;
int z = 1;
for(int i=0; i<temp; i++)
{
for(int j=0; j<=y; j++)
{
out.print(" ");
}
for(int k = 0; k<z; k++)
{
out.print("*");
}
out.println();
y--;
z+=2;
}
for(int i =0; i<=x/2; i++)
{
out.print(" ");
}
out.println("*");
}
}
I don't know how to do that. Thanks!
Try using temp-- just after the input, like that:
int temp = scan.nextInt();
temp--;
Or decreasing your loop condition:
for(int i=0; i<temp-1; i++)
Output in both cases:
*
***
*****
*
If you just subtract one from the input, your christmas tree should be the right size. Here's what it would look like (using the Java style conventions):
public class ChristmasTree {
public static void main(String[] args) {
Scanner scanner = new Scanner(in);
out.print("Please enter a number: ");
int temp = scanner.nextInt() - 1; // note the `- 1`
int x = (temp - 1) * 2 + 1;
int y = x / 2;
int z = 1;
for(int i = 0; i < temp; i++) {
for(int j = 0; j <= y; j++) {
out.print(" ");
}
for(int j = 0; j < z; k++) {
out.print("*");
}
out.println();
y--;
z += 2;
}
for(int i =0; i<=x/2; i++) {
out.print(" ");
}
out.println("*");
}
}
import java.util.Scanner;
public class Fibonacci
{
public static void main(String[] args)
{
int count;
Scanner in = new Scanner(System.in);
System.out.println("Please enter number");
count = in.nextInt();
int[] fib = new int [count];
fib[0] = 1;
fib[1] = 1;
for (int i=2; i<count; i++)
{
fib[i] = fib[i-1] + fib[i-2];
}
for(int i=0; i<count; i++)
{
System.out.print(fib[i] + " ");
}
}
}
This is my very simple Fib program, what i cant figure out is why it always stops one number short. For example:
run: Please enter number 6 1 1 2 3 5 8 BUILD SUCCESSFUL (total time: 5
seconds)
run: Please enter number 7 1 1 2 3 5 8 13 BUILD SUCCESSFUL (total
time: 5 seconds)
I thought in my FOR loops it should be "(int i=2; i <= count;"
but when i put in greater than or equal to in both, or either FOR loop it gives me an error
Any suggestions? i know its something easy i'm overlooking
Your code is giving correct output. but still if you need one more element try to initialize array with count + 1 and then have your loop running for i <= count
public static void main(String[] args) {
int count;
Scanner in = new Scanner(System.in);
System.out.println("Please enter number");
count = in.nextInt();
int[] fib = new int [count+1];
fib[0] = 1;
fib[1] = 1;
for (int i=2; i <= count; i++){
fib[i] = fib[i-1] + fib[i-2];
}
for(int i=0; i <= count; i++){
System.out.print(fib[i] + " ");
}
}
}
Arrays are zero-based. This means, that (assuming count = 5) if you have the following array:
int[] fib = new int[5];
then you can access fib[0], fib[1], fib[2], fib[3] and fib[4]. So
for (int i = 0; i < 5; i++) {
System.out.print(fib[i] + " ");
}
would be fine. As it would access everything in fib, starting with index 0, and stopping with the last index smaller than 5, which is 4. However, if you do:
for (int i = 0; i <= 5; i++) {
System.out.print(fib[i] + " ");
}
then you will access the last index smaller than OR EQUAL TO 5, which is 5. But, as stated before, fib[5] is invalid. That's what gives you your error.
A simpler solution is to avoid needing an array in the first place and you don't need to get the size right.
public static void main(String[] args) {
System.out.println("Please enter a number");
Scanner in = new Scanner(System.in);
int count = in.nextInt();
long a = 1, b = 1;
for(int i = 0; i < count; i++) {
System.out.print(a + " ");
long c = a + b;
a = b;
b = c;
}
System.out.println();
}
There should be one more array element space for int fib[], thus the fib[count] could be stored.
import java.util.Scanner;
public class Fibonacci
{
public static void main(String[] args)
{
int count;
Scanner in = new Scanner(System.in);
System.out.println("Please enter number");
count = in.nextInt();
int[] fib = new int [count + 1];
fib[0] = 1;
fib[1] = 1;
for (int i=2; i <= count; i++)
{
fib[i] = fib[i-1] + fib[i-2];
}
for(int i = 0; i<= count; i++)
{
System.out.print(fib[i] + " ");
}
}
}
public class Fibonacci
{
private int [] fibArray;
public Fibonacci()
{
}
public void Fibonacci()
{
fibArray = new int[0];
}
public void setFibonnaci(int size)
{
fibArray = new int[size];
if(fibArray.length == 1)
{
fibArray [0] = 0;
}
else if(fibArray.length == 2)
{
fibArray[0] = 0;
fibArray[1] = 1;
fibArray[2] = 2;
}
else
{
fibArray[1] = 1;
fibArray[0] = 0;
for(int x = 2; x < fibArray.length; x++)
{
fibArray [x] = fibArray[x-1] + fibArray[x-2];
}
}
}
public int getSequence(int number)
{
if(number -1 < fibArray.length)
{
return fibArray[number - 1];
}
return -1;
}
//check the test case for getFibo
public String toString()
{
String output = "";
for (int x = 0; x < fibArray.length; x++)
{
output += x + " - " + fibArray[x];
}
return output;
}
}
Late response but new to site and just trying to help. This fib class works 100%