SlideShare una empresa de Scribd logo
1 de 13
Introduction to Computer
Lab 01
Write a program that asks the user for their name and age, and then
prints out a greeting that includes their name and age.
public class Greeting {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Ask the user for their name
System.out.print("What is your name? ");
String name = scanner.nextLine();
// Ask the user for their age
System.out.print("What is your age? ");
int age = scanner.nextInt();
// Print out a greeting that includes the user's name and age
System.out.println("Hello, " + name + "! You are " + age + " years old.");
}
}
Write a program that takes the base and height of a triangle as
input and prints its area.
import java.util.Scanner;
public class TriangleArea {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the base of the triangle: ");
double base = scanner.nextDouble();
System.out.print("Enter the height of the triangle: ");
double height = scanner.nextDouble();
double area = 0.5 * base * height;
System.out.println("The area of the triangle is: " + area);
}
}
Write a Java program that prints the first 10 even numbers
using a for loop.
public class EvenNumbers {
public static void main(String[] args) {
for (int i = 2; i <= 20; i += 2) {
System.out.print(i + " ");
}
}
Write a program that takes an integer as input and prints out
the first n terms of the Fibonacci sequence using a for loop.
public class Fibonacci {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = scanner.nextInt();
int a = 0, b = 1;
for (int i = 1; i <= n; i++) {
System.out.print(a + " ");
int sum = a + b;
a = b;
b = sum;
}
}
}
Write a program that takes an integer as input and calculates
the factorial of that number using a for loop.
public class Factorial {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
int factorial = 1;
for (int i = 1; i <= number; i++) {
factorial *= i;
}
System.out.println("The factorial of " + number + " is " + factorial);
}
}
Write a program that takes an integer as input and prints
whether it is even or odd.
public class EvenOrOdd {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
if (number % 2 == 0) {
System.out.println(number + " is even");
} else {
System.out.println(number + " is odd");
}
}
}
Write a program that takes two integers as input and
determines which one is larger.
public class LargerNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number: ");
int number1 = scanner.nextInt();
System.out.print("Enter the second number: ");
int number2 = scanner.nextInt();
if (number1 > number2) {
System.out.println(number1 + " is larger than " + number2);
} else if (number1 < number2) {
System.out.println(number2 + " is larger than " + number1);
} else {
System.out.println("The two numbers are equal");
}
}
Write a program that takes an integer as input and determines
whether it is positive, negative, or zero.
public class PositiveNegative {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
if (number > 0) {
System.out.println("The number is positive");
} else if (number < 0) {
System.out.println("The number is negative");
} else {
System.out.println("The number is zero");
}
}
}
Write a Java program that takes an integer as input and prints
the multiplication table for that number using a while loop.
public class MultiplicationTable {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Ask the user for the number to generate the multiplication table for
System.out.print("Enter a number: ");
int number = scanner.nextInt();
// Initialize the counter and the product
int i = 1;
int product;
// Generate the multiplication table using a while loop
while (i <= 10) {
product = number * i;
System.out.println(number + " x " + i + " = " + product);
i++;
}
}
Write a program that takes an input string from the user and
reverses the order of the characters.
public class StringReverse {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Ask the user for the input string
System.out.print("Enter a string: ");
String input = scanner.nextLine();
// Initialize an empty string to hold the reversed string
String reversed = "";
// Reverse the order of the characters in the input string
for (int i = input.length() - 1; i >= 0; i--) {
reversed += input.charAt(i);
}
// Print out the reversed string
System.out.println("The reversed string is: " + reversed);
}
Write a program that print 100 stars on 10 lines, 10 stars on
each line.
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
Write a program that finds and prints out all prime numbers
between 1 and a given number.
import java.util.Scanner;
public class PrimeNumbers {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = input.nextInt();
System.out.print("Prime numbers between 1 and " + n + " are: ");
for (int i = 2; i <= n; i++) {
boolean isPrime = true;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.print(i + " ");

Más contenido relacionado

Similar a Lab01.pptx

Star pattern programs in java Print Star pattern in java and print triangle ...
Star pattern programs in java Print Star pattern in java and  print triangle ...Star pattern programs in java Print Star pattern in java and  print triangle ...
Star pattern programs in java Print Star pattern in java and print triangle ...Hiraniahmad
 
import java.util.Scanner;public class ArrayOperation {    inp.pdf
import java.util.Scanner;public class ArrayOperation {     inp.pdfimport java.util.Scanner;public class ArrayOperation {     inp.pdf
import java.util.Scanner;public class ArrayOperation {    inp.pdfangelsfashion1
 
SumNumbers.java import java.util.Scanner;public class SumNumbe.pdf
SumNumbers.java import java.util.Scanner;public class SumNumbe.pdfSumNumbers.java import java.util.Scanner;public class SumNumbe.pdf
SumNumbers.java import java.util.Scanner;public class SumNumbe.pdfankkitextailes
 
java program assigment -2
java program assigment -2java program assigment -2
java program assigment -2Ankit Gupta
 
Review Questions for Exam 10182016 1. public class .pdf
Review Questions for Exam 10182016 1. public class .pdfReview Questions for Exam 10182016 1. public class .pdf
Review Questions for Exam 10182016 1. public class .pdfmayorothenguyenhob69
 
JAVA PRACTICE QUESTIONS v1.4.pdf
JAVA PRACTICE QUESTIONS v1.4.pdfJAVA PRACTICE QUESTIONS v1.4.pdf
JAVA PRACTICE QUESTIONS v1.4.pdfRohitkumarYadav80
 
Import java
Import javaImport java
Import javaheni2121
 
JAVA Question : Programming Assignment
JAVA Question : Programming AssignmentJAVA Question : Programming Assignment
JAVA Question : Programming AssignmentCoding Assignment Help
 
java program assigment -1
java program assigment -1java program assigment -1
java program assigment -1Ankit Gupta
 
VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...
VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...
VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...Nithin Kumar,VVCE, Mysuru
 
Anjalisoorej imca133 assignment
Anjalisoorej imca133 assignmentAnjalisoorej imca133 assignment
Anjalisoorej imca133 assignmentAnjaliSoorej
 
CODEimport java.util.; public class test { public static voi.pdf
CODEimport java.util.; public class test { public static voi.pdfCODEimport java.util.; public class test { public static voi.pdf
CODEimport java.util.; public class test { public static voi.pdfanurag1231
 
I need to create a data type that implement the following interface -.docx
I need to create a data type that implement the following interface -.docxI need to create a data type that implement the following interface -.docx
I need to create a data type that implement the following interface -.docxmckerliejonelle
 
DS LAB RECORD.docx
DS LAB RECORD.docxDS LAB RECORD.docx
DS LAB RECORD.docxdavinci54
 
Prompt a user to enter a series of integers separated by spaces and .pdf
Prompt a user to enter a series of integers separated by spaces and .pdfPrompt a user to enter a series of integers separated by spaces and .pdf
Prompt a user to enter a series of integers separated by spaces and .pdfFootageetoffe16
 

Similar a Lab01.pptx (20)

Star pattern programs in java Print Star pattern in java and print triangle ...
Star pattern programs in java Print Star pattern in java and  print triangle ...Star pattern programs in java Print Star pattern in java and  print triangle ...
Star pattern programs in java Print Star pattern in java and print triangle ...
 
3.Lesson Plan - Input.pdf.pdf
3.Lesson Plan - Input.pdf.pdf3.Lesson Plan - Input.pdf.pdf
3.Lesson Plan - Input.pdf.pdf
 
import java.util.Scanner;public class ArrayOperation {    inp.pdf
import java.util.Scanner;public class ArrayOperation {     inp.pdfimport java.util.Scanner;public class ArrayOperation {     inp.pdf
import java.util.Scanner;public class ArrayOperation {    inp.pdf
 
SumNumbers.java import java.util.Scanner;public class SumNumbe.pdf
SumNumbers.java import java.util.Scanner;public class SumNumbe.pdfSumNumbers.java import java.util.Scanner;public class SumNumbe.pdf
SumNumbers.java import java.util.Scanner;public class SumNumbe.pdf
 
07-Basic-Input-Output.ppt
07-Basic-Input-Output.ppt07-Basic-Input-Output.ppt
07-Basic-Input-Output.ppt
 
Java programs
Java programsJava programs
Java programs
 
Java final lab
Java final labJava final lab
Java final lab
 
java program assigment -2
java program assigment -2java program assigment -2
java program assigment -2
 
Review Questions for Exam 10182016 1. public class .pdf
Review Questions for Exam 10182016 1. public class .pdfReview Questions for Exam 10182016 1. public class .pdf
Review Questions for Exam 10182016 1. public class .pdf
 
JAVA PRACTICE QUESTIONS v1.4.pdf
JAVA PRACTICE QUESTIONS v1.4.pdfJAVA PRACTICE QUESTIONS v1.4.pdf
JAVA PRACTICE QUESTIONS v1.4.pdf
 
Import java
Import javaImport java
Import java
 
JAVA Question : Programming Assignment
JAVA Question : Programming AssignmentJAVA Question : Programming Assignment
JAVA Question : Programming Assignment
 
java program assigment -1
java program assigment -1java program assigment -1
java program assigment -1
 
VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...
VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...
VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...
 
Anjalisoorej imca133 assignment
Anjalisoorej imca133 assignmentAnjalisoorej imca133 assignment
Anjalisoorej imca133 assignment
 
LAB1.docx
LAB1.docxLAB1.docx
LAB1.docx
 
CODEimport java.util.; public class test { public static voi.pdf
CODEimport java.util.; public class test { public static voi.pdfCODEimport java.util.; public class test { public static voi.pdf
CODEimport java.util.; public class test { public static voi.pdf
 
I need to create a data type that implement the following interface -.docx
I need to create a data type that implement the following interface -.docxI need to create a data type that implement the following interface -.docx
I need to create a data type that implement the following interface -.docx
 
DS LAB RECORD.docx
DS LAB RECORD.docxDS LAB RECORD.docx
DS LAB RECORD.docx
 
Prompt a user to enter a series of integers separated by spaces and .pdf
Prompt a user to enter a series of integers separated by spaces and .pdfPrompt a user to enter a series of integers separated by spaces and .pdf
Prompt a user to enter a series of integers separated by spaces and .pdf
 

Último

AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024SynarionITSolutions
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Principled Technologies
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 

Último (20)

AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 

Lab01.pptx

  • 2. Write a program that asks the user for their name and age, and then prints out a greeting that includes their name and age. public class Greeting { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Ask the user for their name System.out.print("What is your name? "); String name = scanner.nextLine(); // Ask the user for their age System.out.print("What is your age? "); int age = scanner.nextInt(); // Print out a greeting that includes the user's name and age System.out.println("Hello, " + name + "! You are " + age + " years old."); } }
  • 3. Write a program that takes the base and height of a triangle as input and prints its area. import java.util.Scanner; public class TriangleArea { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the base of the triangle: "); double base = scanner.nextDouble(); System.out.print("Enter the height of the triangle: "); double height = scanner.nextDouble(); double area = 0.5 * base * height; System.out.println("The area of the triangle is: " + area); } }
  • 4. Write a Java program that prints the first 10 even numbers using a for loop. public class EvenNumbers { public static void main(String[] args) { for (int i = 2; i <= 20; i += 2) { System.out.print(i + " "); } }
  • 5. Write a program that takes an integer as input and prints out the first n terms of the Fibonacci sequence using a for loop. public class Fibonacci { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); int n = scanner.nextInt(); int a = 0, b = 1; for (int i = 1; i <= n; i++) { System.out.print(a + " "); int sum = a + b; a = b; b = sum; } } }
  • 6. Write a program that takes an integer as input and calculates the factorial of that number using a for loop. public class Factorial { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); int number = scanner.nextInt(); int factorial = 1; for (int i = 1; i <= number; i++) { factorial *= i; } System.out.println("The factorial of " + number + " is " + factorial); } }
  • 7. Write a program that takes an integer as input and prints whether it is even or odd. public class EvenOrOdd { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); int number = scanner.nextInt(); if (number % 2 == 0) { System.out.println(number + " is even"); } else { System.out.println(number + " is odd"); } } }
  • 8. Write a program that takes two integers as input and determines which one is larger. public class LargerNumber { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the first number: "); int number1 = scanner.nextInt(); System.out.print("Enter the second number: "); int number2 = scanner.nextInt(); if (number1 > number2) { System.out.println(number1 + " is larger than " + number2); } else if (number1 < number2) { System.out.println(number2 + " is larger than " + number1); } else { System.out.println("The two numbers are equal"); } }
  • 9. Write a program that takes an integer as input and determines whether it is positive, negative, or zero. public class PositiveNegative { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); int number = scanner.nextInt(); if (number > 0) { System.out.println("The number is positive"); } else if (number < 0) { System.out.println("The number is negative"); } else { System.out.println("The number is zero"); } } }
  • 10. Write a Java program that takes an integer as input and prints the multiplication table for that number using a while loop. public class MultiplicationTable { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Ask the user for the number to generate the multiplication table for System.out.print("Enter a number: "); int number = scanner.nextInt(); // Initialize the counter and the product int i = 1; int product; // Generate the multiplication table using a while loop while (i <= 10) { product = number * i; System.out.println(number + " x " + i + " = " + product); i++; } }
  • 11. Write a program that takes an input string from the user and reverses the order of the characters. public class StringReverse { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Ask the user for the input string System.out.print("Enter a string: "); String input = scanner.nextLine(); // Initialize an empty string to hold the reversed string String reversed = ""; // Reverse the order of the characters in the input string for (int i = input.length() - 1; i >= 0; i--) { reversed += input.charAt(i); } // Print out the reversed string System.out.println("The reversed string is: " + reversed); }
  • 12. Write a program that print 100 stars on 10 lines, 10 stars on each line. public class Main { public static void main(String[] args) { for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { System.out.print("*"); } System.out.println(); } } }
  • 13. Write a program that finds and prints out all prime numbers between 1 and a given number. import java.util.Scanner; public class PrimeNumbers { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a number: "); int n = input.nextInt(); System.out.print("Prime numbers between 1 and " + n + " are: "); for (int i = 2; i <= n; i++) { boolean isPrime = true; for (int j = 2; j < i; j++) { if (i % j == 0) { isPrime = false; break; } } if (isPrime) { System.out.print(i + " ");