Here is my code to find even Fibonacci numbers and to add them:
package a;
import java.util.*;
public class A {
//this about finding Even Fibonacci numbers and adding them to sum.
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
int[] n = new int[t];
int[] nn = new int[t];
int i,j,sum;
for(int a0 = 0; a0 < t; a0++){
n[a0] = in.nextInt();
}
nn[0]=1;
for(i = 0 ; i<t;i++){
sum = 0;
for(j= 1;j<n[i];j++){
nn[j] = j+nn[j-1];
if(nn[j]%2==0)
{
sum += nn[j];
}
}
System.out.println(sum); //this is not printing the output
}
}
}
Sample Input
2
10
100
Sample Output
10
44
The problem is that this line System.out.println(sum); is not printing anything.
Any ideas?
In your code you have
for(int a0 = 0; a0 < t; a0++){
n[a0] = in.nextInt();
}
The problem is that the program is waiting for you to enter t integers. I don't know what values you want there, but change it to something more like this
for(int a0 = 0; a0 < t; a0++){
n[a0] = 0;//But instead of 0 the actual number that you want to set for the value.
}
I hope you find this helpful!
I do not see a problem here. Just took the code, compiled and executed it. After specifying the value for t and also providing t input values, I saw an output on the console.
stefan#linux-3047:~$ java A
5 (t)
1 (1st of 5 values)
2 (2nd of 5 values)
3 (3rd of 5 values)
4 (4th of 5 values)
5 (5th of 5 values)
0 (System.out.println)
2 (System.out.println)
6 (System.out.println)
6 (System.out.println)
6 (System.out.println)
Multiple problems exist here.
One error at line 25 if the t for example is 5:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at application.A.main(A.java:25)
Added some System.out.println(...) to see how you can fix it cause i don't know the code you want to add:
package application;
import java.util.*;
public class A {
// This about finding Even Fibonacci numbers and adding them to sum.
public static void main(String[] args) {
System.out.println("Enter a number below:\n");
Scanner in = new Scanner(System.in);
int t = in.nextInt();
System.out.println("You entered..." + t+"\n");
// Arrays
int[] n = new int[t];
int[] nn = new int[t];
int i, j, sum;
// First For Loop
for (int a0 = 0; a0 < t; a0++) {
System.out.println("Enter a number a0..");
n[a0] = in.nextInt();
System.out.println("You entered ao=:" + a0+"\n");
}
nn[0] = 1;
// Second For Loop
for (i = 0; i < t; i++) {
sum = 0;
for (j = 1; j < n[i]; j++) {
nn[j] = j + nn[j - 1];
if (nn[j] % 2 == 0) {
sum += nn[j];
}
}
// this is not printing the output
System.out.println("Sum is:="+sum);
}
}
}
Related
I would like to write a program that prints a sequence of integer numbers written in a single line with the numbers space-separated. The input of the program is a positive integer called n, this is the number of the elements of the sequence the program should print.
For example, if n = 7, then the program should output 1 2 2 3 3 3 4.
THIS IS MY CODE:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n;
n = scanner.nextInt();
while (n == 0) {
n = scanner.nextInt();
for (int i = n; i <= n; i++) {
System.out.println(i * n);
}
}
}
}
yeah I have no clue, that's why I am asking for help ;)
Here is a way with comments
n = scanner.nextInt();
int viewNumber = 1;// for printing number
while (n > 0) { // if any number printing left
for (int i = 1; i <= viewNumber; i++) { // This loop print viewNumber viewNumber's times
System.out.print(viewNumber + " ");
n--; // decrease n value after printing a number
if(n == 0) { // break the loop if print n numbers already
break;
}
}
viewNumber++; // increase view number after showing it's that times
}
I am not sure if I understand you, but following code
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(final String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
int n;
while ((n = scanner.nextInt()) > 0) {
int count = 0;
for (int i = 1; i < n && count < n; i++) {
for (int j = 1; j <= i && count < n; j++) {
System.out.print(i + " ");
count++;
}
}
System.out.println();
}
}
}
should print this:
7
1 2 2 3 3 3 4
8
1 2 2 3 3 3 4 4
Because I am assuming behavior from your example:
For example, if n = 7, then the program should output 1 2 2 3 3 3 4.
I believe this could be rewritten to more efficient code (but more dirty code for you).
Try This, I have used 2 for loops
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter any digit");
int n;
n = scanner.nextInt();
for(int a = 0;n>=a;a++) {
for (int i = 0; a > i; i++) {
System.out.print(" "+a);
}
}
}
Input: 5
Output: 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
I am doing an exercise where I have to print 'x' (is an input) rows of numbers incrementing from 0 to 10.
If I input 3, the output should look like this
012
345
678
012
345
678
012
345
678
but instead, I get 3 rows of a 0 to 10 count.
I know it might be easy to code, but I am stuck in that!
I think I am not undestanding well the nested loops :(
public class quadrats {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int q = in.nextInt();
for (int j = 0; j < q; j++) {
for (int i = 0; i <= 10; i++) {
System.out.print(i);
}
System.out.println();
}
}
}
You don't need two loops for this. All you need is to print a newline after every 3rd letter and an extra newline after every 3rd line. Your code can be like:
public class quadrats {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int q = in.nextInt();
int lines = 0;
int letters = 0;
while (lines < q) {
System.out.print(i);
if (letters && letters % q == 0) {
System.out.println();
lines++;
}
if (lines && lines % q == 0) {
System.out.println();
letters = 0;
continue;
}
letters++;
}
}
PS: I haven't tried this code myself. But concept would be the same.
The answer above should solve your problem so I will try to explain what your code does.
Let's start with code inside first for loop:
for (int i = 0; i <= 10; i++) {
System.out.print(i);
}
System.out.println();
First we have a loop iterating through numbers from 0 to 10 and the output is:
012345678910
and a new line after that.
That means that output of your program will print above mentioned output q times.
012345678910
012345678910
012345678910
You can try with below code
public class quadrats {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int q = in.nextInt();
for (int j = 0; j < q; j++) {
for (int i = 0; i < 9; i++) {
if(i%3 == 0)
System.out.println();
System.out.print(i);
}
System.out.println();
}
}
}
Print X quandrants of X rows and X columns each
Scanner in = new Scanner(System.in);
int q = in.nextInt();
// q quadrants
for (int iQuadrat = 0; iQuadrat < q; iQuadrat++) {
// count will keep track of the last number you print
int count = 0;
// q rows
for (int iRow = 0; iRow < q; iRow++) {
// q cols
for (int iCol = 0; iCol < q; iCol++) {
System.out.print(count);
// increment the count and take its modulo 10 so it stays between 0 and 9
count = (count+1)%10;
}
// line return at the end of the row
System.out.println();
}
// line return between quadrants
System.out.println();
}
For an input of 12, it will print 12 times this quadrant
012345678901
234567890123
456789012345
678901234567
890123456789
012345678901
234567890123
456789012345
678901234567
890123456789
012345678901
234567890123
I need to be able to get this pattern using for loops:
Q. Write a program to complete the first N rows of the following output. The number of rows N should be read from the keyboard.
Intended output:
1
2 4
3 6 9
4 8 12 16
Result:
1
24
399
4161616
525252525
Attempt:
(I haven't used scanner yet, because I wanted to try understand how to do it without scanner.)
import java.util.Scanner;
public class Test {
public static void main(String [] args){
int odd = 1;
for(int i=1;i<=5;i++)
{
int no=i;
for(int j=1; j<=i;j++)
{
System.out.print(no);
no = i*i;
}
System.out.println();
}
}
}
You were very close!
for(int i = 1; i <= 5; i++) {
for(int j = 1; j <= i; j++) {
System.out.print(i * j + " ");
}
System.out.println();
}
See demo
import java.util.*;
public class Main {
Scanner input = new Scanner (System.in);
int n = 0, tn = 0, time = 0;int sum=0;
int t = input.nextInt(); //no. of test cases
for (int i =0; i<t; i++)
{
n = input.nextInt();//no. of timings
for (int j = 0; j<n; j++)
{
tn = input.nextInt(); //individual time
sum=0;
sum+=tn;
sum*=2;
}
System.out.println(t+". "+sum);
}
}
}
My output
output I am supposed to get
Can anyone tell me where I went wrong?
1.) You are setting every time sum=0 while taking new input so you are losing the previous values hence last time
sum=30
sum= 30*2 = 60
so reset the sum=0 when you are done with your first case input
2.) You need to do the multiplication after you add-up all the values so simply do the multiplication when you have the sum of all individual time values
for (int i = 0; i < t; i++) {
n = input.nextInt();// no. of timings
for (int j = 0; j < n; j++) {
tn = input.nextInt(); // individual time
// add all values first
sum += tn;
}
// multiply the total of values with 2
System.out.println(i + ". " + (sum * 2));
// now set sum=0 for next case
sum = 0;
}
Test case Output :
2
3
10
20
30
2. 120 // output of first case
2
100
50
2. 300 // output of second case
As a beginner I am doing online problems to understand arrays, this is not homework but practice. Any advice helps!
The code should take user input.
Problem:
Print two space-separated integers denoting the respective comparison scores earned by A and B.
Sample Input
5 6 7
3 6 10
Sample Output
1 1
Explanation
In this example:
A = (a0, a1, a2) where the values are (5,6,7)
B = (b0,b1,b2) where the values are (3,6,10)
Compare each individual score:
a0 > b0 ==> so A receives 1 point.
a0 = b0 ==> nobody receives a point.
b0 > a0 ==> so B receives 1 point.
A's comparison score is 1 and B's comparison score is 1. Thus, we print 1 1 on a single line.
Approach 1:
First I though of implementing this as a 2d array but I only got this far as I am not sure where to implement the comparison:
public class CompareElem2DArray{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int array2d [][]= new int[3][3];
System.out.println("Please enter 3 marks for A and 3 marks for B: ");
for(int a = 0; a<3; a++) //row
{
for(int b=0; b<3; b++)//column
{
int array2d[a][b] = in.nextInt();
}
}
for (int column = 0; column<3; column++)
{
for(int row=0; row<3; row++)
{
System.out.println( array2d[column][row]+" ");
}
}
System.out.println();
}
}
Approach 2:
This is my second attempt without using 2D arrays.
public class Comparison {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a0 = in.nextInt();
int a1 = in.nextInt();
int a2 = in.nextInt();
int b0 = in.nextInt();
int b1 = in.nextInt();
int b2 = in.nextInt();
int a[] = new int[3];
int b[] = new int[3];
int firstAns = 0;
int secondAns = 0;
for(int i = 0; i<3; i++)
{
int a[i] = in.nextInt();
System.out.println(a[i]);
}
for(int j = 0; j<3; j++)
{
int b[j] = in.nextInt();
System.out.println(b[j]);
}
for(int z = 0; z<3; z++)
{
if(a[z]>b[z])
{
firstAns++;
}
else if(a[z]<b[z])
{
secondAns++;
}
else
{
return;
}
}
System.out.println(firstAns);
System.out.println(secondAns);
}
}
You can try doing
int[] ar = {5,6,7,3,6,10};
int halflen= (ar.length)/2;
int[] result = new int[halflen];
for(int i=0,j=halflen;i<halflen;i++,j++)
{
result[i]=ar[i]-ar[j];
}
Now you have a array with 3 results , if 0 is draw whatever how is > 0 made the point you can eliminate the array inside loop if you want and add your if ther too like :
If (ar [i] > ar [j]) A++;
If (ar [i]<ar [j]) B++;
In approach 1., I think that what you need is [3][2] or [2][3] array (you have 2 sets of data, in one set there are 3 numbers).
Then the comparison goes in second loop (in this case, array2d is [3][2]):
for(int i=0; i<3; i++)
{
//comparison goes here
}
You need to compare values of array2d[i][0] with array2d[i][1].
When input is: 5 6 7 3 6 10
Your 2d array is something like:
5 6 7
3 6 10
So the second loop is comparing 5 with 3, 6 with 6 and 7 with 10.