Please answer # 2 , I am using Codelite . Thanks Write a program to generate and
ID: 3792212 • Letter: P
Question
Please answer # 2 , I am using Codelite . Thanks
Write a program to generate and display a table of n and n^2, for integer values of n ranging from 1 to 10. Include appropriate column headings. Make sure the table and headings are formatted into two well-aligned columns. Write a program that uses two for loops. The first for loop prints the odd numbers from 1 to 13. The second for loop prints the even numbers from 2 to 16. Write a program that asks the user to enter four digits (separated by spaces) and then prints those digits in English. For example Enter three digits: 1 2 3 4 You entered digits: one two three four Your program should use a switch structure inside a for loop. The factorial of a positive integer n is the product of all integers from 1 to n. For example: 6! = 1 middot 2 middot 3 middot 4 middot 5 middot 6 Write a program that uses nested for loops to generate a table of the first 10 positive integers and their factorials. Include appropriate column headings. Make sure the table and headings are formatted into two well-aligned columns.Explanation / Answer
I have used Java to solve all 4 problems.
problem 1:
import java.util.*;
class multiplyTable
{
public static void main(String args[])
{
// System.out.println(myMethod());
System.out.print("n");
System.out.print(" n*n");
System.out.println();
for(int i=1;i<=10;i++)
{
System.out.print(i);
System.out.print(" "+i*i);
System.out.println();
}
}
}
o/p:
n n*n
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
10 100
problem 2
import java.util.*;
class oddEven
{
public static void main(String args[])
{
// System.out.println(myMethod());
System.out.println("odd numbers from 1 to 13");
for(int i=1;i<=13;i+=2)
{
System.out.print(i+" ");
}
System.out.println();
System.out.println("even numbers from 2 to 16");
for(int i=2;i<=16;i+=2)
{
System.out.print(i+" ");
}
}
}
o/p:
odd numbers from 1 to 13
1 3 5 7 9 11 13
even numbers from 2 to 16
2 4 6 8 10 12 14 16
problem 3:
import java.util.*;
import java.io.*;
class Numbers
{
public static void main(String args[]) throws IOException
{
// System.out.println(myMethod());
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter numbers");
//String str=sc.next();
String str=br.readLine();
String[] result = str.split("\s");
int[] num = new int[result.length];
for (int x=0; x<result.length; x++) {
num[x]=Integer.parseInt(result[x]);
}
System.out.println("entered digits");
for(int i=0;i<num.length;i++)
{
System.out.print(num[i]+" ");
}
System.out.println();
System.out.println("you entered digits");
for(int i=0;i<num.length;i++)
{
switch(num[i]){
case 1:
System.out.print("one ");
break;
case 2:
System.out.print("two ");
break;
case 3:
System.out.print("three ");
break;
case 4:
System.out.print("four ");
break;
case 5:
System.out.print("five ");
break;
case 6:
System.out.print("six ");
break;
case 7:
System.out.print("seven ");
break;
case 8:
System.out.print("eight ");
break;
case 9:
System.out.print("nine ");
break;
case 10:
System.out.print("ten ");
break;
}
}
}
o/p:
enter numbers
2 5 8
entered digits
2 5 8
you entered digits
two five eight
problem 4:
import java.util.*;
import java.io.*;
class factTable
{
public static void main(String args[]) throws IOException
{
// System.out.println(myMethod());
System.out.print("n");
System.out.print(" n!");
System.out.println();
for(int i=1;i<=10;i++)
{
int fact=1;
System.out.print(i);
for (int j=1;j<=i;j++)
{
fact=fact*j;
}
System.out.print(" "+fact);
System.out.println();
}
}
}
o/p:
n n!
1 1
2 2
3 6
4 24
5 120
6 720
7 5040
8 40320
9 362880
10 3628800
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.