java practice set 7
/*
Java Tutorial: Practice Questions on Java Methods
1. Write a Java method to print the multiplication table of a number n.
2. Write a program using functions to print the following pattern:
*
**
***
****
3. Write a recursive function to calculate the sum of first n natural numbers.
4. Write a function to print the following pattern:
****
***
**
*
5. Write a function to print the nth term of the Fibonacci series using recursion.
6. Write a function to find the average of a set of numbers passed as arguments.
7. Repeat problem 4 using Recursion.
8. Repeat problem 2 using Recursion.
9. Write a function to convert Celsius temperature into Fahrenheit.
10. Repeat problem 3 using an iterative approach
*/
package com.company;
public class cwh_chapter7_practice_set {
//problem1 multiplication of n
static void multiplication(int n) {
for (int i = 1; i <= 10; i++) {
System.out.format("%d X %d = %d\n", n, i, n * i);
}
}
//problem2 pattern ascending order of *
//problem 8 pattern 1 using recursion
static void pattern1(int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
//using recursion
static void pattern1_rec(int n) {
if (n > 0) {
pattern1_rec(n - 1);
for (int i = 0; i < n; i++) {
System.out.print("*");
}
System.out.println();
}
}
//problem4 pattern descending order of *
//problem7 pattern 54 using recursion
static void pattern2 (int n) {
for (int i = 0; i < n; i++) {
//for (int j = n-1; j > 1; j--)
for(int j=n;j>i;j--) {
System.out.print("*");
}
System.out.println();
}
}
//using recursion
/*
*/
static void pattern2_rec(int n) {
if (n > 0) {
for (int i = n - 1; i >= 0; i--) {
System.out.print("*" + " ");
}
System.out.println();
pattern2_rec(n - 1);
}
}
// pattern1_rec(3)
// pattern1_rec(2) + 3 times star and new line
// pattern1_rec(1) + 2 times star and new line + 3 times star and new line
// pattern1_rec(0) + 1 times star and new line + 2 times star and new line + 3 times star and new line
//problem 3.Write a recursive function to calculate the sum of first n natural numbers.
// sum(n) = 1 + 2 + 3... + n
// sum(n) = 1 + 2 + 3... + n-1 + n
// sum(n) = sum(n-1) + n
// sum(3) = 3 + sum(2)
// sum(3) = 3 + 2 + sum(1)
// sum(3) = 3 + 2 + 1
static int sumRec(int n) {
// Base condition
if (n == 1) {
return 1;
}
return n + sumRec(n - 1);
}
// problem 5. print the nth term of the Fibonacci series using recursion.
static int fib(int n) {
/* if(n==1){
return 0;
}
else if(n==2){
return 1;
} */
if (n == 1 || n == 2) {
return n - 1;
} else {
return fib(n - 1) + fib(n - 2);
}
}
//problem 6
static int average_finder(int ...arr){
int sum = 0;
for (int num: arr) {
sum = sum + num;
}
return sum/arr.length;
}
//problem 9 Write a function to convert Celsius temperature into Fahrenheit.
static float C_to_F(float c){
float temp=(c * 9/5) + 32;
return temp;
}
public static void main(String[] args) {
// Problem 1
multiplication(5);
// Problem 2
pattern1(3);
//pattern1_rec(4);
// Problem 3
int c = sumRec(4);
System.out.println(c);
// Problem 4
pattern2(4);
// Problem 5
//fibonacci series - 0, 1, 1, 2, 3, 5, 8, 13, 21, 34
int result = fib(7);
System.out.println(result);
//problem 6 find the average of a set of numbers passed as arguments.
int value = average_finder(5, 4, 9);
System.out.println(value);
//problem 7
pattern2(6);
pattern2_rec(5);
// Problem 8
pattern1(5);
//pattern1_rec(4);
//problem 9
float temp =C_to_F(100);
System.out.println(temp);
}
}
Comments
Post a Comment