Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Take two integers p & q ... Sum should be printed of p to next q numbers including p .. if q <= 0 , then it will take the value of q again ...
Input :::: 3 2
Output :::: 7 (p=3 & q = 2 ..... So from 3 to next 2 numbers are 3 & 4 as it will be included in the sum ... Now we will have to print the sum of 3+4 and that's 7 )
Input ::: 4 -1 1
Output :::. 4 ( as the next number is 4 )
That means we have to start counting from the taken integer ....
Solve it and drop the solution here ......
import java.util.Scanner;
public class Ahmed {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int p=sc.nextInt();
int q=sc.nextInt();
int sum=0;
if(q>=0){
for(int i=1; i<=q ;i++) {
sum=p+sum;
p++;
}
}
else{
q=sc.nextInt();
for(int i=1; i<=q ;i++) {
sum=p+sum;
p++;
}
}
System.out.println(sum);
}
}
if I take input 4 -1 -1 there is a error. Loop will continue until I take q input a positive number or 0;
Correct input 4 -1 -1 2 output 9.
Generally, when we have a loop and we dont know how many times it will repeat, we can use while.
Create a function like this:
private int readPositiveInt(Scanner sc){
int i = -1
while (i <= 0) {
i = sc.nextInt();
}
return i;
}
Then, you replace this line of code int q=sc.nextInt(); by this:
int q = readPositiveInt(sc);
Finally, once q will be positive for sure, you can remove these lines:
else{
q=sc.nextInt();
for(int i=1; i<=q ;i++) {
sum=p+sum;
p++;
}
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 days ago.
Improve this question
Link of problem
Code
import java.util.*;
class Codechef {
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
int t = read.nextInt();
for (int i = 0; i < t; i++) {
ArrayList < Integer > a = new ArrayList < Integer > ();
int n = read.nextInt();
for (int j = 1; j <= n; j++) {
int ele = read.nextInt();
a.add(ele);
}
int k = 0, count = 1;
while (k < n - 1) {
if (a.get(k) != a.get(k + 1)) {
count++;
}
k++;
}
System.out.println(count);
}
}
}
Input
4
1
5
2
1 1
3
1 2 3
4
2 1 2 2
Expected Output
1
1
3
3
My output - It is giving runtime error in the output.`
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
So I am doing this project. And I need to
Write a program that finds the factors and number of factors for an integer.
Factors will be listed on 1 line for smallest to largest with a space separating each.
Continuously accept input until a 0 is entered, which is the sentinel. Do not factor 0.
I got the factoring part. This is the output I am currently getting.
Enter an Number
12
The factors are
1 2 3 4 6 12
It stops immediately after giving the factors. I am not sure on how to implement it to re prompt. I have tried loops but its not working. Also,How can I exclude 1 and the entered number.
This is how my output should look. It should stop once 0 is entered.
Enter a number: 12
There are 4 factors for the number 12: 2 3 4 6
Enter a number: 25
There are 1 factors for the number 25: 5
Enter a number: 100
There are 7 factors for the number 100: 2 4 5 10 20 25 50
Enter a number: 13
There are 0 factors for the number 13:
Enter a number: 0
Here is the code.
package com.FactorsProgram;
import jdk.swing.interop.SwingInterOpUtils;
import java.sql.SQLOutput;
import java.util.Scanner;
//Java Program to print all factors of a number using function
public class Main {
public static void main(String[] args) {
int N;
Scanner scanner;
scanner = new Scanner(System.in);
System.out.println("Enter an Number");
N = scanner.nextInt();
// Calling printFactors method to print all
// factors of N
printFactors(N);
}
//This method prints all factors of N
public static void printFactors(int N) {
int i;
//Check for every number between 1 to N, whether it divides N. If K
//divides N, it means K is a factor of N
System.out.println("factors for the number " );
for (i = 1; i <= N; i++) {
if (N % i == 0) {
System.out.print(i + " ");
}
}
}
}
You had the right idea with having a loop - you need to loop and check that n isn't 0. E.g.:
System.out.println("Enter an Number");
n = scanner.nextInt();
while (n != 0) {
// Calling printFactors method to print all
// factors of N
printFactors(n);
System.out.println("Enter an Number");
n = scanner.nextInt();
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm totally new in Java programming , I wonder how may I write program that reads an arbitrary number of positive integers from the keyboard and then prints them in reverse order. The reading stops when the user inputs a negative number. An example of an execution:
Enter positive integers. End by giving a negative integer.
Integer 1: 5
Integer 2: 10
Integer 3: 15
Integer 4: 20
Integer 5: -7
Number of positive integers: 4
In reverse order: 20, 15, 10, 5
Get your input inside a do-while loop until the loop condition becomes false, means y is negative. Since you've got i value incremented before your loop condition is false, decrement i value by 1 outside loop.
Next, inside the decrementing for loop, print your variables in decreasing index of array, so it will be printed in reverse order of input.
import java.util.*;
/**
*
* #author Capt. Jack Sparrow, pirate lord of the seven seas
*/
public class Example {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner s1 = new Scanner(System.in);
int[] x = new int[50];
int i =0, y;
do
{
System.out.println("Enter new positive integer: ");
y = s1.nextInt();
x[i] = y;
i++;
}while( y >= 0);
i--;
System.out.println("Number of positive integers: "+i);
System.out.print("In reverse order: ");
for(int j = i; j >0; j--)
{
System.out.print(x[j-1] + " ");
}
System.out.println();
}
}
Question:
The Utopian tree goes through 2 cycles of growth every year. The first growth cycle occurs during the spring, when it doubles in height. The second growth cycle occurs during the summer, when its height increases by 1 meter.
Now, a new Utopian tree sapling is planted at the onset of the spring. Its height is 1 meter. Can you find the height of the tree after N growth cycles?
Input Format
The first line contains an integer, T, the number of test cases.
T lines follow. Each line contains an integer, N, that denotes the number of cycles for that test case.
Constraints
1 <= T <= 10
0 <= N <= 60
Output Format
For each test case, print the height of the Utopian tree after N cycles.
//FINALLY, HOPE so .. WHAT QUESTION IS SAYING..
INITIALLY VALUE IS 1 .. IF SPRING OCCURS.. IT'S VALUE WILL BE DOUBLED.. THAT MEANS .. IT WILL BE MULTIPLIED BY 2.. BUT IF SUMMER OCCUR IT'S VALUE WILL BE ADDED BY 1...
If i give input:
2 //here 2 is the number of question..
0
1
So, Output must be:
1
2
Another example,
sample of output:
2
3
4
So, Sample of input will be:
6
7
HOPE SO.. YOU UNDERSTAND WHAT QUESTION IS ASKING, HERE NOW WE HAVE TO MAKE A PROGRAM INTO JAVA....
Okay as further i made a program for this..
package com.logical03;
import java.util.Scanner;
public class MainProgram{
public static void main(String[] args){
int num=1;
int[] array=new int[100];
Scanner in=new Scanner(System.in);
System.out.println("Enter the number of Questions: ");
int n_Elements=in.nextInt();
System.out.println("Enter the values now: ");
for(int i=1; i<=n_Elements; i++){
array[i]=in.nextInt();
}
for(int i=1; i<=n_Elements; i++){
if(array[i]==0){
System.out.println("\n1");
}
else{
for(int j=1; j<=array[i]; j++){
if(j%2!=0){
num=num*2;
}
else{
num=num+1;
}
}
System.out.println(num);
}
}
}
}
As i run into here .. it adds the second number of question into my output.. Suppose..
If i give input as:
2
3
4
So, output must suppose to be:
6
7
Which is correct!!
But My program gives the output as:
6
27 //which is incorrect..becoz it adds the sum of above number :(
Mistake - int num = 1; should be declared in inside parent loop to refresh it's value.
public static void main(String[] args) {
int[] array = new int[100];
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of Questions: ");
int n_Elements = in.nextInt();
System.out.println("Enter the values now: ");
for (int i = 1 ; i <= n_Elements ; i++) {
array[i] = in.nextInt();
}
for (int i = 1 ; i <= n_Elements ; i++) {
int num = 1;
if (array[i] == 0) {
System.out.println("\n1");
} else {
for (int j = 1 ; j <= array[i] ; j++) {
if (j % 2 != 0) {
num = num * 2;
} else {
num = num + 1;
}
}
System.out.println(num);
}
}
}
Output
Enter the number of Questions:
2
Enter the values now:
3
4
6
7
My approach is to take on account that first cycle (2 * height) occurs on odds indexes, and second cicle (1 + height) occurs on even indexes, from 1 to n (inclusive), starting index 0 is always 1.
return IntStream.rangeClosed(1, n)
.reduce(1, (acc, idx) -> idx % 2 != 0 ? acc * 2 : acc + 1);
This is my first contribution, only learning to code and solve algorithms, I had to find a workable solution with simple to follow code credit to http://www.javainterview.net/HackerRank/utopian-tree
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
//receive input
Scanner in = new Scanner(System.in);
//no of test cases
int T=in.nextInt();
//no of cycles
int[] N = new int[T];
for(int i=0;i<T;i++){
N[i]=in.nextInt();
}
int height=1;
for(int i=0;i<N.length;i++){
height=1;
for(int j=1;j<=N[i];j++){
if((j%2) ==1)
height=height*2;
else
height++;
}
System.out.println(height);
}
}
}//this the end of the class
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Write a program that returns number of decimal digits in the given input string.
ex :
Input: 2345abc423
output: 7
This is my code:
Scanner input = new Scanner(System.in);
String s = input.nextLine();
int len = s.length();
int numbers = 0 ;
for(int i=0; i < len; i++){
char c = s.charAt(i);
int num = (int) c;
while(num <= 57 && num >= 48){
System.out.println(numbers);
numbers++;
}
}
System.out.print(numbers);
/////
sorry because my question wasn't obvious
my program always return 0 , not the number of decimal digits
thanks #Keppil i tried if and it worked
thanks
Your program will never exit the while loop since the condition never changes. You should just use an if instead:
if (num <= 57 && num >= 48) {
System.out.println(numbers);
numbers++;
}
Solution that abuses regex:
System.out.println(input.replaceAll("\\D", "").length());