Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create inverse_of_number.java #29

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions inverse_of_number.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* QUESTION:
1. The key constraint is if the number is 5 digits long, it'll contain all the digits from 1 to 5 without missing any and without repeating
any. e.g. 23415 is a 5 digit long number containing all digits from 1 to 5 without missing and repeating any digit from 1 to 5.Take a look at few other valid numbers
- 624135, 81456273 etc.Here are a few invalid numbers - 139, 7421357 etc.
2. The inverse of a number is defined as the number created by interchanging the face value and index of digits of number.e.g. for 426135 (reading from right to left,
5 is in place 1, 3 is in place 2, 1 is in place 3, 6 is in place 4, 2 is in place 5 and 4 is in place 6), the inverse will be 416253 (reading from right to left, 3 is
in place 1, 5 is in place 2,2 is in place 3, 6 is in place 4, 1 is in place 5 and 4 is in place 6) More examples - inverse of 2134 is 1243 and inverse of 24153 is 24153
3. Take as input number "n", assume that the number will follow constraints.
4. Print it's inverse.*/
// input=28346751
// output=73425681

import java.util.*;

public class Main{

public static void main(String[] args) {

Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int sum=0, count=0,temp;
while(n > 0)
{
temp = n%10;
count++;
sum = sum + (count * (int)(Math.pow(10,temp-1)));
n = n/10;
}
System.out.println(sum);
}
}
34 changes: 34 additions & 0 deletions prime_numbers_in_given_range.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// taking low and high as inputs and displaying all prime numbers >=low and <=high
import java.util.*;

public class Main{
public static void main(String[] args) {
// write your code here
Scanner scn = new Scanner(System.in);

// write ur code here
int low = scn.nextInt();
int high = scn.nextInt();
for(int i=low; i<=high; i++)
{

int count=0;

for(int j=2; (j*j)<=i; j++) // (j*j)<=i is used because if that's possible then it will have
//atleast one divisor till sqrt(i)
{

if(i%j == 0 && i!=2)
{
count++;
break;
}
}
if(count == 0)
{
System.out.println(i);
}
}

}
}
51 changes: 51 additions & 0 deletions rotate_a_number.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*question: 1. You are given two numbers n and k. You are required to rotate n, k times to the right. If k is positive, rotate to the right i.e. remove rightmost digit and make it leftmost. Do the reverse for negative value of k. Also k can have an absolute value larger than number of digits in n.
2. Take as input n and k.
3. Print the rotated number.
4. Note - Assume that the number of rotations will not cause leading 0's in the result. e.g. such an input will not be given
n = 12340056
k = 3
r = 05612340 */
import java.util.*;

public class Main{

public static void main(String[] args) {

Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int k = scn.nextInt();
int temp,count=0,temp1;
temp1 = n;
// -->condition 1: for k less than number of digits and positive, exactly k number of digits will come forward and rest array will shift backwards
// eg: n=543689 and k=2
// rotation no. 1: 954368 rotation no. 2: [89]5436 -> k digits came in front and rest array shifted
while(temp1 > 0)
{
temp1 = temp1 / 10;
count++;
}
if(k > 0)
{
k = k % count;
if(k == 0)
{
k = count;//after k=no. of digits repetations, we get actual number back
}
}
else //if k is less than 0
{
if(k == 0) k=count;
else
{
k = (-k) % count;
k = count - k;// negative k is equivalent to (count-k) value of positive k
}
}
// code described below is to shift required digits in front and shift rest array rightwards
temp = n % ((int) Math.pow(10,k));
n = n - temp;
n = n / ((int)Math.pow(10,k));
n = n + (temp * ((int ) Math.pow(10,count-k)));
System.out.println(n);
}
}