Java programming questions
for practise
1. Hello World Program:
Write a Java program that prints “Hello World!” to the console.
Solution:
Java
// Java Program to Print "Hello World"
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
AI-generated code. Review and use carefully. More info on FAQ.
2. Add Two Numbers:
Write a program that takes two integers as input and prints their sum.
Example:
Input: 2 3
Output: 5
3. Swap Two Numbers:
Create a Java program that swaps the values of two variables.
Example:
Input: a=2, b=5
Output: a=5, b=2
Java programming questions
for practise
4. Convert Integer to Binary:
Write a program that converts an integer to its binary representation.
Example:
Input: 9
Output: 1001
5. Factorial of a Number:
Calculate the factorial of a given number.
Example:
Input: 5
Output: 120
6. Add Complex Numbers:
Implement a program that adds two complex numbers.
Example:
Input: 1+2i, 4+5i
Output: 5+7i
7. Calculate Simple Interest:
Compute the simple interest given principal amount, rate, and time.
Example:
Input: P = 10000, R = 5, T = 5
Output: 2500
Java programming questions
for practise
8. Print Pascal’s Triangle:
Generate and print Pascal’s triangle up to a given number of rows.
Example (for N = 5):
11
121
1331
14641
9. Sum of Fibonacci Series:
Find the sum of Fibonacci numbers at even indexes.
Example (for n = 4):
Sum = 0 + 1 + 3 + 8 + 21 = 33
10. Pyramid Number Pattern:
Print the following pyramid pattern:
***
*****
*******