www.fatihkabakci.com

Personal Website and Computer Science TUR EN

CS480L quiz1 solutions

Last update: 10/25/2016 9:06:00 AM

By Fatih KABAKCI

All 11 questions were answered and have been publishing here. I hope these solutions will be helpful for everyone before taking the midterm exam. There is no one single correct answer for those questions. Each question can be performed differently. If you have a any question or doubt, please feel free to contact me.

Solutions

Q1)

package com.fatihkabakci;

import java.util.Scanner;

/**
 * 
 * @author fkabakci
 * Enter how many tables you need: 5
	
	This is table 1
	1 2 3 4 5 6 7 8 9 10 
	This is table 2
	2 4 6 8 10 12 14 16 18 20 
	This is table 3
	3 6 9 12 15 18 21 24 27 30 
	This is table 4
	4 8 12 16 20 24 28 32 36 40 
	This is table 5
	5 10 15 20 25 30 35 40 45 50 
 */

public class Q1 {
	public static void main(String[] args) {
		System.out.println("Enter how many tables you need:");
		Scanner in = new Scanner(System.in);
		
		int numberOfTable = in.nextInt();
		for(int i = 1; i <= numberOfTable; i++) {
			System.out.println("This is table " + i);
			for(int j = i; j <= 10 * i; j += i) {
				System.out.print(j + " ");
			}
			System.out.println();
		}
	}
}

Output:

Enter how many tables you need:
7
This is table 1
1 2 3 4 5 6 7 8 9 10 
This is table 2
2 4 6 8 10 12 14 16 18 20 
This is table 3
3 6 9 12 15 18 21 24 27 30 
This is table 4
4 8 12 16 20 24 28 32 36 40 
This is table 5
5 10 15 20 25 30 35 40 45 50 
This is table 6
6 12 18 24 30 36 42 48 54 60 
This is table 7
7 14 21 28 35 42 49 56 63 70

Q2)

package com.fatihkabakci;

import java.util.Scanner;

/**
 * 
 * @author fkabakci
 * Enter any number: 5
 * 1
 * 22
 * 333
 * 4444
 * 55555
 */
public class Q2 {
	public static void main(String[] args) {
		System.out.println("Enter any number:");
		Scanner in = new Scanner(System.in);
		
		int number = in.nextInt();
		for(int i = 1; i <= number; i++) {
			for(int j = 1; j <= i; j ++) {
				System.out.print(i);
			}
			System.out.println();			
		}
	}
}

Output:

Enter any number:
6
1
22
333
4444
55555
666666

Q3)

package com.fatihkabakci;

import java.util.Scanner;

/**
 * 
 * @author fkabakci
 * Enter any number: 5
 * 5 is a prime
 * 
 * Enter any number: 6
 * 6 is not a prime
 */
public class Q3 {
	public static void main(String[] args) {
		System.out.println("Enter any number:");
		Scanner in = new Scanner(System.in);
		int number = in.nextInt();
		if (number < 2) {
			System.out.println(number + " is not a prime");
			return;
		}

		boolean flag = false;
		for (int i = 2; i < Math.sqrt(number); i++) {
			if (number % i == 0) {
				flag = true;
				break;
			}
		}
		if (flag)
			System.out.println(number + " is not a prime");
		else
			System.out.println(number + " is a prime");
	}
}

Output:

Enter any number:
7
7 is a prime

Enter any number:
22
22 is not a prime

Q4)

package com.fatihkabakci;

import java.util.Scanner;

/**
 * 
 * @author fkabakci
 * while loop version of reversing
 * Enter your number: 123456
 * Reverse of input number is: 654321
 * 
 * Enter your number: 654321
 * Reverse of input number is: 123456
 */
public class Q4 {
	public static void main(String[] args) {
		System.out.println("Enter your number:");
		Scanner in = new Scanner(System.in);
		int number = in.nextInt();
		System.out.print("Reverse of input number is: ");
		while(number > 0) {
			System.out.print(number % 10);
			number /= 10;
		}
	}
}

Output:

Enter your number:
56789
Reverse of input number is: 98765

Q5)

package com.fatihkabakci;

import java.util.Scanner;

/**
 * 
 * @author fkabakci
 * for loop version of reversing
 * Enter your number: 123456
 * Reverse of input number is: 654321
 * 
 * Enter your number: 654321
 * Reverse of input number is: 123456
 */
public class Q5 {
	public static void main(String[] args) {
		System.out.println("Enter your number:");
		Scanner in = new Scanner(System.in);
		int number = in.nextInt();
		int numberOfDigits = String.valueOf(number).length();
		System.out.print("Reverse of input number is: ");
		for(int i = 0; i < numberOfDigits; i++) {
			System.out.print(number % 10);
			number /= 10;
		}
	}
}

Output:

Enter your number:
789012
Reverse of input number is: 210987

Q6)

package com.fatihkabakci;

import java.util.Scanner;

/**
 * 
 * @author fkabakci
 * Enter your number: 898
 * palindrome number
 * 
 * Enter your number: 1234
 * not a palindrome number
 */
public class Q6 {
	public static void main(String[] args) {
		System.out.println("Enter your number:");
		Scanner in = new Scanner(System.in);
		int number = in.nextInt();

		int length = String.valueOf(number).length();
		int[] digits = new int[length];
		for (int i = 0; i < length; i++) {
			digits[i] = number % 10;
			number /= 10;
		}

		boolean flag = true;
		for (int i = 0; i < length / 2; i++) {
			if (digits[i] != digits[length - i - 1]) {
				flag = false;
				break;
			}
		}
		if (flag)
			System.out.print("palindrome number");
		else
			System.out.print("not a palindrome number");

	}
}

Output:

Enter your number:
1001
palindrome number

Q7)

package com.fatihkabakci;

import java.util.Scanner;

/**
 * 
 * @author fkabakci
 * Enter value for Num1 : 10
 * Enter value for Num2 : 20
 * Before Swapping
 * Value of Num1 is :10
 * Value of Num2 is :20
 * After Swapping
 * Value of Num1 is :20
 * Value of Num2 is :10
 * 
 * Enter value for Num1 : 33
 * Enter value for Num2 : 22
 * Before Swapping
 * Value of Num1 is :33
 * Value of Num2 is :22
 * After Swapping
 * Value of Num1 is :22
 * Value of Num2 is :33
 */
public class Q7 {
	public static void main(String[] args) {		
		Scanner in = new Scanner(System.in);
		System.out.println("Enter value for Num1 :");
		int num1 = in.nextInt();
		System.out.println("Enter value for Num2 :");
		int num2 = in.nextInt();
		System.out.println("Before Swapping");
		System.out.println("Value of Num1 is :" + num1);
		System.out.println("Value of Num2 is :" + num2);
		System.out.println("After Swapping");
		
		int temp = num1;
		num1 = num2;
		num2 = temp;
		
		System.out.println("Value of Num1 is :" + num1);
		System.out.println("Value of Num2 is :" + num2);

	}
}

Output:

Enter value for Num1 :
5
Enter value for Num2 :
6
Before Swapping
Value of Num1 is :5
Value of Num2 is :6
After Swapping
Value of Num1 is :6
Value of Num2 is :5

Q8)

package com.fatihkabakci;

import java.util.Scanner;

/**
 * 
 * @author fkabakci
 * Enter a number : 153
 * Is an Armstrong Number:
 *
 * Enter a number : 34
 * Is not an Armstrong Number:
 */
public class Q8 {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		System.out.println("Enter a number :");
		int num = in.nextInt();
		int conditioner = num;
		int result = 0;
		while (conditioner > 0) {		
			int d = conditioner % 10;
			result += d * d * d;
			conditioner /= 10;
		}

		if (result == num)
			System.out.println("Is an Armstrong Number:");
		else
			System.out.println("Is not an Armstrong Number:");

	}
}

Output:

Enter a number :
153
Is an Armstrong Number:

Q9)

package com.fatihkabakci;

import java.util.Scanner;

/**
 * 
 * @author fkabakci
 * Enter value for a : 4
 * Enter value for b : 2
 * 1. "+" 2. "-" 3. "*" 4. "/" 5. "%"
 * choose any number: 3
 * Multiplying: 8
 */
public class Q9 {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		System.out.println("Enter value for a :");
		int a = in.nextInt();
		System.out.println("Enter value for b :");
		int b = in.nextInt();

		System.out.println("1. "+" 2. "-" 3. "*" 4. "/" 5. "%"");

		System.out.println("choose any number:");
		int option = in.nextInt();
		int result = 0;
		String operation = new String();

		switch (option) {
		case 1:
			result = a + b;
			operation = "Addition";
			break;
		case 2:
			result = a - b;
			operation = "Substraction";
			break;
		case 3:
			result = a * b;
			operation = "Multiplying";
			break;
		case 4:
			result = a / b;
			operation = "Division";
			break;
		case 5:
			result = a % b;
			operation = "Remainder";
			break;
		default:
			result = 0;
			operation = "Make sure the value that you entered between 1 - 5";
		}
		System.out.println(operation + ": " + result);
	}
}

Output:

Enter value for a :
5
Enter value for b :
6
1. "+" 2. "-" 3. "*" 4. "/" 5. "%"
choose any number:
3
Multiplying: 30

Q10)

package com.fatihkabakci;

import java.util.Scanner;

/**
 * 
 * @author fkabakci
 * Enter a String: hello
 * Reverse String Result: olleh
 */
public class Q10 {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		System.out.println("Enter a String: ");
		String input = in.nextLine();
		System.out.println("Reverse String Result: ");
		for(int i = input.length() - 1; i >= 0 ; i--) {
			System.out.print(input.charAt(i));
		}
	}
}

Output:

Enter a String: 
fatih
Reverse String Result: 
hitaf

Q11a)

package com.fatihkabakci;

import java.util.Scanner;

/**
 * 
 * @author fkabakci
 * For loop version of factorial
 * Enter a number to find the factorial: 5
 * Factorial = 120
 */
public class Q11a {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		System.out.println("Enter a number to find the factorial: ");
		int number = in.nextInt();		
		int fact = 1;
		for(int i = 1; i <= number; i++) {
			fact = fact * i;
		}
		System.out.println("Factorial = " + fact);
	}
}

Output:

Enter a number to find the factorial: 
7
Factorial = 5040

Q11b)

package com.fatihkabakci;

import java.util.Scanner;

/**
 * 
 * @author fkabakci
 * While loop version of factorial
 * Enter a number to find the factorial: 7
 * Factorial = 5040
 */
public class Q11b {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		System.out.println("Enter a number to find the factorial: ");
		int number = in.nextInt();		
		int fact = 1, i = 1;
		while(i <= number) {
			fact = fact * i;
			i++;
		}
		System.out.println("Factorial = " + fact);
	}
}

Output:

Enter a number to find the factorial: 
10
Factorial = 3628800
There has been no comment yet

Name:


Question/Comment
   Please verify the image




The Topics in Computer Science

Search this site for





 

Software & Algorithms

icon

In mathematics and computer science, an algorithm is a step-by-step procedure for calculations. Algorithms are used for calculation, data processing, and automated reasoning.

Programming Languages

icon

A programming language is a formal constructed language designed to communicate instructions to a machine, particularly a computer. It can be used to create programs to control the behavior of a machine. Java,C, C++,C#

Database

icon

A database is an organized collection of data. The data are typically organized to model aspects of reality in a way that supports processes requiring information.

Hardware

icon

Computer hardware is the collection of physical elements that constitutes a computer system. Computer hardware refers to the physical parts or components of a computer such as the monitor, memory, cpu.

Web Technologies

icon

Web development is a broad term for the work involved in developing a web site for the Internet or an intranet. Html,Css,JavaScript,ASP.Net,PHP are one of the most popular technologies. J2EE,Spring Boot, Servlet, JSP,JSF, ASP

Mobile Technologies

icon

Mobile application development is the process by which application software is developed for low-power handheld devices, such as personal digital assistants, enterprise digital assistants or mobile phones. J2ME

Network

icon

A computer network or data network is a telecommunications network that allows computers to exchange data. In computer networks, networked computing devices pass data to each other along data connections.

Operating Systems

icon

An operating system is software that manages computer hardware and software resources and provides common services for computer programs. The OS is an essential component of the system software in a computer system. Linux,Windows

Computer Science

icon

Computer science is the scientific and practical approach to computation and its applications.A computer scientist specializes in the theory of computation and the design of computational systems.