Input
The input begins with the number t of test cases in a single line
(t<=10). In each of the next t lines there are two numbers m and n (1
<= m <= n <= 1000000000, n-m<=100000) separated by a space.
Output
For every test case print all prime numbers p such that m <= p <= n,
one number per line, test cases separated by an empty line.
Example
Input: 2 1 10 3 5
Output: 2 3 5 7
3 5
I wrote this one:
import java.util.*;
import java.lang.*;
class Main
{
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int i = 0;
long n1=0;
long n2=0;
while(i<=n){
n1 = sc.nextLong();
n2 = sc.nextLong();
if(n1==1)
{
n1++;
}
if((n1-n2)<=100000){
for(long j=n1;j<=n2;j++){
int count = 0;
for(long k=2;k<=j/2;k++){
if(j%k==0){
count = 1;
}
}
if(count==0){
System.out.println(j);
}
}
}
}
}
}
It is working fine in my system
But it is showing time exceeded on submission.
Related
import java.text.BreakIterator;
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner in= new Scanner(System.in);
System.out.println("nomber");
int n= in.nextInt();
int s= n*n;
int l = (""+s).length();
System.out.println(l);
}
}
how can I extract last digit from input in a simple way. Not so complicated like while loop.... java
Since last digit is also the remainder of dividing by 10, you can use n % 10.
The simplest way to extract last digit is modulo to 10. Example: 123 % 10 = 3
After that, if you want to continue to extract last digit (it's 2 in this case), you should assign number = number /10 that will return the remain of the number.
// Example n = 123
int lastDigit_1 = n % 10; // This will return 3
n = n /10 // This will return 12 because 123 / 10 = 12
int lastDigit2 = n % 10; // This will return 2
n = n /10 // This will return 1
int lastDigit3 = n % 10;
...
To implement the above concept, u should try using while loop for better approach.
Using while:
while(n != 0){
someVariable = n % 10;
// YOUR LOGIC HERE
n = n / 10;
}
You have to print a pattern using recursion. Given a input
N
the pattern looks like this
N
,
a
i
,
a
i
+
1
,
a
i
+
2
,.....,
N
. Where if
a
i
>
0
then
a
i
+
1
=
a
i
−
5
else
a
i
+
1
=
a
i
+
5
. It will be a decreasing sequence from
N
till
a
i
<=
0
and then an increasing sequence till
N
. (See sample test cases for better explanation)
Input format
First line contains an integer
T
denoting number of test cases.
For each of the next
T
lines, each line contains an integer
N
.
Output format
For each test case on a new line, print the required pattern.
Constraints
1
<=
T
<=
6
0
<=
N
<=
2000
Example
Input
2
16
10
Output
16 11 6 1 -4 1 6 11 16
10 5 0 5 10
Sample test case explanation
For the first test case
N=16, it will be a decreasing sequence till the printing number becomes <=0.
16 11 6 1 −4
After this point it will be a increasing sequence till the printing number becomes N
1 6 11 16
So the pattern is 16 11 6 1 −4 1 6 11 16.
My code is below but i got the output as 16,11,6,1,-4 only. Help me to correct this code
import java.util.Scanner;
public class Day3
{
public static void series(int n,boolean b)
{
int temp = n;
boolean flag=b;
System.out.println(temp+" ");
if(flag==true)
temp-=5;
else if(flag==false)
temp+=5;
if(temp<=0)
flag=false;
if(temp<=n)
series(temp,flag);
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t>0)
{
int n = sc.nextInt();
series(n,true);
t-=1;
}
}
}
Because 'n' changes in every cases. You must create another variable and keep first 'n' in that, for control if temp smaller than 'n'.
For example
import java.util.Scanner;
public class Day3
{
int firstN = 0; //added that line
public static void series(int n,boolean b)
{
int temp = n;
boolean flag=b;
System.out.println(temp+" ");
if(flag==true)
temp-=5;
else if(flag==false)
temp+=5;
if(temp<=0)
flag=false;
if(temp<=firstN) //changed that line
series(temp,flag);
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t>0)
{
int n = sc.nextInt();
firstN = n; //added that line
series(n,true);
t-=1;
}
}
}
Also a little tip;
you can use (flag) for (flag==true)
and (!flag) for (flag==false)
Just FYI : Although this can be solved using recursion, it can be solved more efficiently without using recursion. It is as simple as this:
private static void printSeries(int N) {
int T = N;
while ( T >= 0 ) {
System.out.print(T + " ");
T = T - 5;
}
while ( T <= N ) {
System.out.print(T + " ");
T = T + 5;
}
}
Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits. For example, the program should: output the individual digits of 3456 as 3 4 5 6 and the sum as 18, output the individual digits of 8030 as 8 0 3 0 and the sum as 11, output the individual digits of 2345526 as 2 3 4 5 5 2 6 and the sum as 27, and output the individual digits of 4000 as 4 0 0 0 and the sum as 4.
Moreover, the computer always adds the digits in the positive direction even if the user enters a negative number. For example, output the individual digits of -2345 as 2 3 4 5 and the sum as 14.
This is the question I'm having minor difficulties with, the only part I can't figure out is how can I print the single integers in the order that he wants, from what I learned so far I can only print them in reverse. Here's my code:
import java.util.*;
public class assignment2Q1ForLoop {
static Scanner console = new Scanner (System.in);
public static void main(String[] args) {
int usernum, remainder;
int counter, sum=0, N;
//Asaking the user to enter a limit so we can use a counter controlled loop
System.out.println("Please enter the number of digits of the integer");
N = console.nextInt();
System.out.println("Please enter your "+N+" digit number");
usernum = console.nextInt();
System.out.println("The individual numbers are:");
for(counter=0; counter < N; counter++) {
if(usernum<0)
usernum=-usernum;
remainder = usernum%10 ;
System.out.print(remainder+" ");
sum = sum+remainder ;
usernum = usernum/10;
}
System.out.println();
System.out.println("the sum of the individual digits is:"+sum);
}
}
You have to storeremainder variables in an array and then print them in the loop from last index to first as shown in this tutorial.
You can either store digits in array and then print them, or you can try something like that:
final Scanner console = new Scanner(System.in);
System.out.println("Please enter your number");
final int un = console.nextInt();
long n = un > 0 ? un : -un;
long d = 1;
while (n > d) d *= 10;
long s = 0;
System.out.println("The individual numbers are:");
while (d > 1) {
d /= 10;
final long t = n / d;
s += t;
System.out.print(t + " ");
n %= d;
}
System.out.println();
System.out.println("the sum of the individual digits is:" + s);
An idea would be : convert int to string and write a method
getChar(int index) : String
which gives you for example 4 from 3456 with
getChar(2);
See Java - Convert integer to string
Here, I wrote a code for your problem with using a stack. If you want a simple code, you can comment my solution and I will wrote another one.
Scanner c1 = new Scanner(System.in);
System.out.print("Enter the number: ");
int numb = c1.nextInt();
numb = Math.abs(numb);
Stack<Integer> digits = new Stack<Integer>();
while(numb>0){
int n = numb%10;
digits.push(n);
numb = numb/10;
}
int sum = 0;
while(!digits.isEmpty()){
int n = digits.pop();
sum+=n;
System.out.print(n+" ");
}
System.out.print(sum);
I want to generate an output like this :-
0
0 1
0 1 1
0 1 1 2
0 1 1 2 3
0 1 1 2 3 5
However, i am trying in this way to achieve , but some piece of logic is missing which i am unable to decipher.
Here's what i am trying :-
import java.util.Scanner;
class Fibonacci
{
public static void main(String arr[])
{
System.out.println("Enter a no.");
Scanner input=new Scanner(System.in);
int num=input.nextInt();
int x=0,y=1;
for(int i=0;i<=num;i++)
{
for(int j=0;j<i;j++)
{
System.out.print(j);
}
System.out.println("");
}
}
}
And it generates the output like this (consider num=6)
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3 4 5
What logic is required to get the desired output ? Would be thankful if anyone can explain me this :)
Thanks in advance !!
You need to change logic of inner loop like this by adding two previous number to current number and swap them like this.
import java.util.Scanner;
class Fibonacci
{
public static void main(String arr[])
{
int x = 0, y = 0, c = 0;
System.out.println("Enter a no.");
Scanner input = new Scanner(System.in);
int num = input.nextInt();
for (int count = 0; count < num; count++) {
System.out.print(0);
x = 0;
y = 1;
c = 0;
for (int i = 1; i <= count; i++) {
c = x + y;
y = x;
x = c;
System.out.print(" " + c);
}
System.out.println();
}
}
}
First two numbers 0 and 1 are given , no need to caculate .
Use a String to save previous line string .
Caculate next numbers , add it into your previous line string .
Here is an example :
int x = 0 , y = 1;
int num = 6;
System.out.println("0");
System.out.println("0 1");
String str = "0 1";
for(int i = 2 ; i < num ; i ++){
int amt = x + y ;
x = y;
y = amt;
str += " " + amt;
System.out.println(str);
}
In a Fibonacci series only the first two numbers are provided which are 0 and 1. The next number of the series are calculated by adding the last two numbers. The series is limited by the user by providing the number of integers it wants in the series.
Logic: The logic behind creating a Fibonacci series is to add the two integers and save them in a new variable z = x+y and then replace the first integer value by the second integer and second integer value by their sum to move one step ahead in the series x=y adn y=z.
In your problem you want the series to be printed in a right angled triangle so you need to save the series that is already printed in a string
int n = 10;
System.out.println("0\n");
System.out.println("0 1\n");
int x = 0, y=1;
int i=2, z=0;
String str = "0 1";
while(i!=10)
{
z = x+y;
str += " " + z;
x=y;
y=z;
i++;
System.out.println(str);
}
Hope this helps
I'm trying to get a for loop to start on the last digit of an integer that is given by the user through scanner. Any suggestions?
for(int i = number.length()-1;...)
I'm looking for something along those lines but that won't leave me with a compile error
You must convert the number to a String then iterate through each character.
public class IterateNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter a number:");
String num = String.valueOf(scanner.nextInt());
for(int i = num.length()-1; i >= 0; i--){
System.out.println(num.charAt(i));
}
}
}
Use integer arithmetic:
for (int i = number, digit = i % 10; i > 0; i = i / 10, digit = i % 10) {
// do something with "digit", which will iterate from last digit to first
}
Here's some sample code showing it working:
int number = 1234567890;
for (int i = number, digit = i % 10; i > 0; i = i / 10, digit = i % 10) {
System.out.println(digit);
}
Output:
0
9
8
7
6
5
4
3
2
1