Thread: Lucky Numbers in between the Range of Numbers

Forum : Problem Solving   10 Days ago
User Image
Sriman

Member

Joined: Jul 15, 2016

Points: 535

Threads: 640

Posts: 161

Answer Icon Lucky Numbers in between the Range of Numbers

| Quote Date : May 14, 2018    Views:252    

I bought a New Bike and want to Get Registration Number for my Bike of Four Digits. When i en-quire in the RTA office the current series is starting from "N1" and will ends with "N2". I want to find all the Four Digits Numbers whose some is equal to my Lucky Number "L" so that i can request/apply that number for my Bike in RTA Office.

Write a program for the above problem.

Comments

User Image
Ankit123

Newbie

Joined : Dec 8, 2017

Points : 100

Threads: 8

Posts: 21

Re: Lucky Numbers in between the Range of Numbers

Reply Date : May 21, 2018


public class LuckyNumber {
public static void main(String[] args) {
int i, luckyNumber= 2190, sum = 0, n1 = 1000, n2 = 2999 ;
boolean flag = false;
outer:
for(i = n1; i <= n2; i++)
for(int j = i; j != 0; j = j/10) {
sum = sum + j%10;
if(sum==luckyNumber) {
flag = true;
break outer;
}
}
System.out.println(flag ? "Registration Number ? = " + i : "Not Matched with Lucky Number");
}
}

User Image
nitin12

Newbie

Joined : Mar 14, 2018

Points : 100

Threads: 0

Posts: 4

Re: Lucky Numbers in between the Range of Numbers

Reply Date : May 21, 2018


import java.util.*;
class LuckyNumber
{
public List<Integer> checkLuckyNumber(int number){
List<Integer>list=new ArrayList<>();
int num=0;
int n1=1000;
int n2=9999;
for(int i=n1;i<=n2;i++){
num=i;
int k=0;
int sum=0;
for(int j=0;j<=3;j++){
if(num<10){
sum=sum+num;
}
else{
k=num%10;
sum=sum+k;
num=num/10;
}
}
if(sum==number){
list.add(i);
}
}
return list;
}
public static void main(String[]args){
LuckyNumber ln=new LuckyNumber();
List<Integer>numberList=ln.checkLuckyNumber(8);
System.out.println(numberList);
}
}
?

User Image
Ankit123

Newbie

Joined : Dec 8, 2017

Points : 100

Threads: 8

Posts: 21

Re: Lucky Numbers in between the Range of Numbers

Reply Date : May 21, 2018

can you give us one example so that we think about it because the line " I want to find all the Four Digits Numbers whose sum is equal to my Lucky Number 'L' " is not giving the clear perception to understand the ques.

User Image
Asif4

Newbie

Joined : Dec 7, 2017

Points : 100

Threads: 0

Posts: 2

Re: Lucky Numbers in between the Range of Numbers

Reply Date : May 16, 2018

Can you elaborate, what do you mean by "starting from N1" and "ending with N2"?
Do you mean either
1) N1N2 where N could be 1,2,3... and 1 & 2 are hard coded in  hundredth place and unit place?
or
2) N1xyz - N2xyz where x,y,z could be 1,2,3...and 1, 2 are hard coded in thousandth place?

Load More

No More Comments