In: Computer Science
class Loops{
public void printNumbers(int low, int high){
// using a for loop, print all numbers from low to high
for(int i = low; i <= high; i++){
System.out.println(i);
}
}
public int sumOfNumbers(int n){
// using a for loop, calculate and return the sum of first n
numbers
// i.e n = 5, answer = 5+4+3+2+1 = 15
int sum = 0;
for(int i = 1; i <= n; i++){
sum += i;
}
return sum;
}
public void printMultiplicationTable(int num){
// using a for loop, print the multiplication table of num (up to
first 10!)
// i.e. num = 5, 5*1=5, 5*2=10, 5*3=15, 5*4=20, 5*5=25, 5*6=30,
5*7=35...
for(int i = 1; i <= 10; i++){
System.out.println(num + " * " + i + "=" + (num *i));
}
}
public int getFactorialOfNum(int num){
// using for loop, calculate and return the factorial of
number
// i.e. factorial of 4 is: 4*3*2*1 = 24
// i.e. factorial of 5 is: 5*4*3*2*1 = 120
for(int i = 1; i <= num; i++){
factorial = factorial * i;
}
System.out.println("Factorial of "+ num +" is: "+ factorial);
}
public int pow(int base, int power){
// using for loop, calculate base raised to power
// i.e. base = 2, power = 3 = 2^3 = 2*2*2 = 8
// i.e. base = 5, power = 4 = 5^4 = 5*5*5*5 = 625
int p = 1;
for(int i = 0; i < power; i++){
p *= base;
}
return p;
}
public String reverseMyString(String input){
// using a loop, reverse a string
// i.e. input = "hello", reversed answer: "olleh"
// i.e. input = "bye", reversed answer: "eyb"
String reverse = " ";
for(int i = 0; i < input.length(); i++){
reverse = input.chartAt(i) + reverse;
}
return reverse;
}
public boolean isEven(int num){
// return true if the number is even, false otherwise
for (int i = 0; i <= 10; i++){
if(i % 2 == 0) {
return true;
}
}
}
public boolean isOdd(int num){
// return true if the number is odd, false otherwise
for(int i = 0; i <= 10; i++){
if(i % 2 != 0){
return true;
}
}
}
public boolean isPrime(int num){
// return true if the number is prime, false otherwise
for(int i = 2; i < num; ++i){
if(num % 1 == 0){
return false;
}
}
return num > 1;
}
}
Write a LoopsDriver:
Java code
import java.util.Scanner;
class Loops {
public void printNumbers(int low, int high) {
// using a for loop, print all numbers from low to high
for (int i = low; i <= high; i++) {
System.out.println(i);
}
}
public int sumOfNumbers(int n) {
// using a for loop, calculate and return the sum of first n numbers
// i.e n = 5, answer = 5+4+3+2+1 = 15
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
return sum;
}
public void printMultiplicationTable(int num) {
// using a for loop, print the multiplication table of num (up to first 10!)
// i.e. num = 5, 5*1=5, 5*2=10, 5*3=15, 5*4=20, 5*5=25, 5*6=30, 5*7=35...
for (int i = 1; i <= 10; i++) {
System.out.println(num + " * " + i + "=" + (num * i));
}
}
public void getFactorialOfNum(int num) {
int factorial = 1;
for (int i = 1; i <= num; i++) {
factorial = factorial * i;
}
System.out.println("Factorial of " + num + " is: " + factorial);
}
public int pow(int base, int power) {
// using for loop, calculate base raised to power
// i.e. base = 2, power = 3 = 2^3 = 2*2*2 = 8
// i.e. base = 5, power = 4 = 5^4 = 5*5*5*5 = 625
int p = 1;
for (int i = 0; i < power; i++) {
p *= base;
}
return p;
}
public String reverseMyString(String input) {
// using a loop, reverse a string
// i.e. input = "hello", reversed answer: "olleh"
// i.e. input = "bye", reversed answer: "eyb"
String reverse = " ";
for (int i = 0; i < input.length(); i++) {
reverse = input.charAt(i) + reverse;
}
return reverse;
}
public boolean isEven(int num) {
return num % 2 == 0;
}
public boolean isOdd(int num) {
return num % 2 == 1;
}
public boolean isPrime(int num) {
// return true if the number is prime, false otherwise
for (int i = 2; i < num; ++i) {
if (num % i == 0) {
return false;
}
}
return true;
}
}
public class Main {
public static void main(String[] args) {
int choice, num1, num2;
Scanner scan = new Scanner(System.in);
Loops loop = new Loops();
do {
System.out.print(
"\n1. Print Numbers\n2.sum of numbers\n3.Print Multiplication Table\n4.Find factorial\n5.calculate power\n6.Reverse String\n7.check for even\n8.check for odd\n9.check for Prime\n0.Exit\nEnter choice : ");
choice = scan.nextInt();
switch (choice) {
case 1: {
System.out.print("Enter value of low : ");
num1 = scan.nextInt();
System.out.print("Enter value of high : ");
num2 = scan.nextInt();
loop.printNumbers(num1, num2);
break;
}
case 2: {
System.out.print("Enter number : ");
num1 = scan.nextInt();
System.out.println(loop.sumOfNumbers(num1));
break;
}
case 3: {
System.out.print("Enter number : ");
num1 = scan.nextInt();
loop.printMultiplicationTable(num1);
break;
}
case 4: {
System.out.print("Enter number : ");
num1 = scan.nextInt();
loop.getFactorialOfNum(1);
break;
}
case 5: {
System.out.print("Enter number: ");
num1 = scan.nextInt();
System.out.print("Enter power : ");
num2 = scan.nextInt();
System.out.println(loop.pow(num1, num2));
break;
}
case 6: {
scan.nextLine();
System.out.print("Enter String : ");
String str = "";
str = scan.nextLine();
System.out.println(loop.reverseMyString(str));
break;
}
case 7: {
System.out.print("Enter number : ");
num1 = scan.nextInt();
System.out.println(loop.isEven(num1));
break;
}
case 8: {
System.out.print("Enter number : ");
num1 = scan.nextInt();
System.out.println(loop.isOdd(num1));
break;
}
case 9: {
System.out.print("Enter number : ");
num1 = scan.nextInt();
System.out.println(loop.isPrime(num1));
break;
}
case 0: {
System.out.println("Good Bye!");
break;
}
default: {
System.out.println("Invalid choice");
}
}
} while (choice != 0);
scan.close();
}
}