Note that the following solutions are made in Python 3.x
CLI Step 1
1. Print a multiplication table.
Solution - JAVA
class MultiplicationTables { public static void main (String args[]) { for (int i = 2; i < 6; i++) { for (int j = 0; j < 10; j++) { System.out.print(i + " X " + j + " = "+ (i * j)); } System.out.println (" "); } } }
Solution - Python
# loop for timestables of 7, 8, 9 for i in range(7,10): print ("Multiplication table of " + str(i) + " is presented below:") # loop to iterate 10 times for j in range(1,11): print(i,'x',j,'=',i*j)
2. Print a multiplication table as shown below.
0 0 2 0 3 6 0 4 8 12 0 5 10 15 20 0 6 12 18 24 30 0 7 14 21 28 35 42
Solution - JAVA
class MultiplicationTablesTriangle { public static void main (String args[]) { for (int i = 0; i < 8; i++) { for (int j = 0; j < i; j++) { System.out.print(" "+ (i * j)); } System.out.println (" "); } } }
Solution - Python
# loop to make triangle for i in range(1,8): print('') # create a new line # loop to iterate i times for j in range(0,i): print(' ',i*j, end='')
3. Write a program to convert 69 Fahrenheit to Centigrade.
Solution - JAVA
class F2C { public static void main (String args[]) { float centi; float fht = 69; centi = (fht - 32) * 5/9; System.out.println ("The centigrade equivalent of 69F is " + centi + "C" ); } }
Solution - Python
Celsius = (69 - 32) * 5.0/9.0 print ("Temperature: 69 Fahrenheit = ", Celsius, " C")
4. Write a program to convert 69 Centigrade to Fahrenheit.
Solution - JAVA
class C2F { public static void main (String args[]) { float centi = 69; float fht; fht = centi * 9/5 - 32; System.out.println ("The Fahrenheit equivalent of 69C is " + fht + "F" ); } }
Solution - Python
Fahrenheit = 9.0/5.0 * 69 + 32 print ("Temperature: 69 Celsius = ", Fahrenheit, " F")
5. Write a program to calculate the factorial of 10.
Solution - Java using for loop
public class Factorial { public static void main(String args[]) { int n, c, fact = 1; n = 10; for ( c = 2 ; c <= n ; c++ ) { fact = fact*c; } System.out.println("Factorial of "+n+" is = "+fact); } }
Solution - Java using recursive calling
public class Factorial { public static void main(String args[]) { Calculation obj_one = new Calculation(); int a = obj_one.fact(4); System.out.println("The factorial of the number is : " + a); } } class Calculation { int fact(int n) { int result; if(n==1) return 1; result = fact(n-1) * n; return result; } }
Solution - Python
Using for loop
def factorial(x): factResult = 1 for i in xrange(2, x + 1): factResult *= i return factResult print (factorial(10))
Solution - Python
Using while loop
def factorial(n): factResult = 1 while n >= 1: factResult = factResult * n n = n - 1 return factResult print (factorial(10))
Solution - Python
Another solution using math.
import math print (math.factorial(10))
Solution - Python
Another solution using recursive calling of the function.
def factorial(n): if n == 0: return 1 else: return n * factorial(n-1) print (factorial(10))
9. Write a program to get the following output.
^^^^^ ##### ^^^^^ ##### ^^^^^
Solution - Java
public class HashCaret { public static void main(String[] args) { int row = 5, column; while (row >= 1) { column = 1; while (column <= 5) { System.out.print(row % 2 == 1 ? "*" : "#"); ++column; } --row; System.out.println(); } } }
Solution - Python 3.4 and 2.7
for i in range(1,6): if i%2: print("#####") else: print("^^^^^")