Small Factorials
class TestClass {
Q) What is the string made of?
---------------------------------------------------------------------------------
You are given a string, which contains entirely of decimal digits (0-9). Each digit is made of a certain number of dashes, as shown in the image below. For instance 1 is made of 2 dashes, 8 is made of 7 dashes and so on.
Input Output
You are asked to calculate factorials of some small positive integers.
Input
An integer T, denoting the number of testcases, followed by T lines, each containing a single integer N.
An integer T, denoting the number of testcases, followed by T lines, each containing a single integer N.
Output
For each integer N given at input, output a single line the value of N!
For each integer N given at input, output a single line the value of N!
Input Constraint
1 <= T <= 100
1 <= N <= 100
1 <= N <= 100
Input Output
4
1 1
2 2
5 120
3 6
1000000 Output will be larger than int and long so you have to take Array or BigInteger type datatype
Answer
---------------------------------------------------------------------------------
import java.math.BigInteger;
import java.io.BufferedReader;
import java.io.InputStreamReader;
class TestClass {
public static void main(String args[] ) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
int N = Integer.parseInt(line);
for (int i = 0; i < N; i++) {
line=br.readLine();
long g=Long.parseLong(line);
BigInteger d=new BigInteger("1");
BigInteger c=new BigInteger("1");
BigInteger b=new BigInteger("1");
for(int f=1;f<=g;f++){
b=b.multiply(c);
d=new BigInteger("1");
c=c.add(d);
}
System.out.println(b.toString());
}
}
}
Q) What is the string made of?
---------------------------------------------------------------------------------
You are given a string, which contains entirely of decimal digits (0-9). Each digit is made of a certain number of dashes, as shown in the image below. For instance 1 is made of 2 dashes, 8 is made of 7 dashes and so on.
You have to write a function that takes this string message as an input and returns a corresponding value in terms of a number. This number is the count of dashes in the string message.
Note:
0 consists of 6 dashes, 1 consists of 2 dashes, 2 consists of 5 dashes, 3 consists of 5 dashes, 4 consists of 4 dashes, 5 consists of 5 dashes, 6 consists of 6 dashes, 7 consists of 3 dashes [though the figure shows that 7 consists of 4 dashes but due to minor mistake in the problem please write your solution assuming 7 consists of 3 dashes], 8 consists of 7 dashes, 9 consists of 6 dashes.
Constraints
- String message will contain at least one digit, but not more than 100
- Each character in code will be a digit ('0'-'9').
12134 18
Answer
---------------------------------------------------------------------------------
import java.io.BufferedReader;
import java.io.InputStreamReader;
class TestClass {
public static void main(String args[] ) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
String st=line;
int count=0;
for (int i=0; i<st.length(); i++) {
if(st.charAt(i)=='1'){
count=count+2;
}else if(st.charAt(i)=='2'){
count=count+5;
}else if(st.charAt(i)=='3'){
count=count+5;
}else if(st.charAt(i)=='4'){
count=count+4;
}else if(st.charAt(i)=='5'){
count=count+5;
}else if(st.charAt(i)=='6'){
count=count+6;
}else if(st.charAt(i)=='7'){
count=count+3;
}else if(st.charAt(i)=='8'){
count=count+7;
}else if(st.charAt(i)=='9'){
count=count+6;
}else if(st.charAt(i)=='0'){
count=count+6;
}
}
System.out.println(count);
}
}
Comments
Post a Comment