0% found this document useful (0 votes)
5 views13 pages

Week 1

The document contains multiple Java programs that demonstrate basic programming concepts such as user input, conditional statements, loops, and calculations. Key functionalities include age verification for voting, checking divisibility by 5, calculating factorials, identifying positive or negative numbers, and summing natural numbers. Additionally, it includes countdowns for rocket launches and checks for spring season dates.

Uploaded by

bb7431
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views13 pages

Week 1

The document contains multiple Java programs that demonstrate basic programming concepts such as user input, conditional statements, loops, and calculations. Key functionalities include age verification for voting, checking divisibility by 5, calculating factorials, identifying positive or negative numbers, and summing natural numbers. Additionally, it includes countdowns for rocket launches and checks for spring season dates.

Uploaded by

bb7431
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Week 1

import java.util.Scanner;

class agefindingforvote{

public static void main(String[] args) {

Scanner tyson=new Scanner(System.in);

System.out.print("Give the age of the person:");

int age=tyson.nextInt();

if(age>=18)

System.out.print("The person's age is"+age+"and can vote");

else

System.out.print("The person's age is"+age+"and cannot vote");

import java.lang.System;

import java.util.Scanner;

class Divisibleby5

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

int ajai=sc.nextInt();

System.out.print("The number is:"+ajai);

if(ajai%5==0)

System.out.print("\nThe number" +ajai+ "is divisible by 5");


Week 1

else

System.out.print("\nThe number" +ajai+ "is not divisible by 5");

import java.util.Scanner;

public class FactorialCalculator{

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

// Take user input

System.out.print("Enter a positive integer: ");

int n = sc.nextInt();

// Check if the number is positive

if(n < 0) {

System.out.println("Factorial is not defined for negative numbers.");

else {

int factorial = 1;

int i = 1;

// Calculate factorial using while loop

while(i <= n) {

factorial *= i;
Week 1

i++;

// Display the factorial result

System.out.println("The factorial of " + n + " is: " + factorial);

import java.util.Scanner;

public class FactorialCalculator2{

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

// Take user input

System.out.print("Enter a positive integer: ");

int n = sc.nextInt();

// Check if the number is positive

if(n < 0) {

System.out.println("Factorial is not defined for negative numbers.");

else {

int factorial = 1;

// Calculate factorial using for loop

for(int i = 1; i <= n; i++) {

factorial *= i;

}
Week 1

// Display the factorial result

System.out.println("The factorial of " + n + " is: " + factorial);

import java.util.Scanner;

class findingthenumber{

public static void main(String[] args) {

Scanner tyson=new Scanner(System.in);

System.out.print("Give any number:");

int number=tyson.nextInt();

if(number==0)

System.out.print("Zero");

else if(number>0)

System.out.print("Positive");

else

System.out.print("Negative");

import java.util.Scanner;
Week 1

class firstnumberisgratest{

public static void main(String[] args) {

Scanner tyson=new Scanner(System.in);

System.out.println("Enter the first number:");

int a=tyson.nextInt();

System.out.println("Enter the Second number:");

int b=tyson.nextInt();

System.out.println("Enter the third number:");

int c=tyson.nextInt();

if(a<b && a<c)

System.out.println("Is the first number is smallest? YES");

else

System.out.println("Is the first number is smallest? No");

import java.util.Scanner;

class numberislargestthenthree

public static void main(String[] args)

Scanner tyson=new Scanner(System.in);

System.out.print("Enter the first number:") ;

int a=tyson.nextInt();
Week 1

System.out.print("Enter the Second number:");

int b=tyson.nextInt();

System.out.print("Enter the third number:");

int c=tyson.nextInt();

if(a>3)

System.out.print("Is the first number is largest? Yes\n");

else

System.out.print("Is the first number is largest? No\n");

if(b>3)

System.out.print("Is the second number is largest? Yes\n");

else

System.out.print("Is the second number is largest? No\n");

if(c>3)

System.out.print("Is the third number is largest? Yes\n");

else

System.out.print("Is the third number is largest? No\n");

}
Week 1

}import java.util.Scanner;

public class RocketLaunch {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

// Taking user input for countdown start value

System.out.print("Enter the countdown start value: ");

int counter = sc.nextInt();

// Countdown using while loop

while (counter >= 1) {

System.out.println(counter);

counter--; // Decrement counter

// After countdown reaches 1, print "Liftoff!"

System.out.println("Rocket lunched");

import java.util.Scanner;

public class RocketLaunch2{

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

// Taking user input for countdown start value

System.out.print("Enter the countdown start value: ");


Week 1

int counter = sc.nextInt();

// Countdown using for-loop

for(int i = counter; i >= 1; i--) {

System.out.println(i);

// After countdown ends, print "Liftoff!"

System.out.println("Rocket Lunched");

import java.util.Scanner;

class SpringSeason {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

// Input month and day from user

System.out.print("Enter the month (1-12): ");

int month = sc.nextInt();

System.out.print("Enter the day (1-31): ");

int day = sc.nextInt();

// Check if it's Spring Season (March 20 to June 20)

if ((month == 3 && day >= 20 && day <= 31) ||

(month == 4 && day >= 1 && day <= 30) ||

(month == 5 && day >= 1 && day <= 31) ||

(month == 6 && day >= 1 && day <= 20)) {


Week 1

System.out.println("It's a Spring Season");

} else {

System.out.println("Not a Spring Season");

import java.util.Scanner;

class sumofnaturalnumbers{

public static void main(String[] args) {

Scanner tyson=new Scanner(System.in);

System.out.print("Give any natural number:");

int n=tyson.nextInt();

int a=0;

if(n>=0)

a=n*(n+1)/2;

System.out.print("The sum of" +n+ "natural numbers is:"+a);

else{

System.out.print("The number" +n+ "is not a natural number");

import java.util.Scanner;

class sumofnaturalnumbers{

public static void main(String[] args) {


Week 1

Scanner tyson=new Scanner(System.in);

System.out.print("Give any natural number:");

int n=tyson.nextInt();

int a=0;

if(n>=0)

a=n*(n+1)/2;

System.out.print("The sum of" +n+ "natural numbers is:"+a);

else{

System.out.print("The number" +n+ "is not a natural number");

import java.util.Scanner;

public class SumOfNaturalNumbers3{

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

// Take user input for n

System.out.print("Enter a natural number (n): ");

int n = sc.nextInt();

// Check if the entered number is natural (greater than 0)

if(n <= 0) {

System.out.println("Please enter a valid natural number (n > 0)");

}
Week 1

else {

// Calculate sum using for loop

int sumForLoop = 0;

for(int i = 1; i <= n; i++) {

sumForLoop += i;

// Calculate sum using formula n*(n+1)/2

int sumFormula = n * (n + 1) / 2;

// Display the result

System.out.println("Sum using for loop: " + sumForLoop);

System.out.println("Sum using formula: " + sumFormula);

// Compare both results

if(sumForLoop == sumFormula) {

System.out.println("Both results are correct.");

else {

System.out.println("Results are not matching. Check your logic.");

import java.util.Scanner;

public class SumUntilZero {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);


Week 1

// Initialize total to 0.0

double total = 0.0;

double number;

// Use while loop to keep asking for input until user enters 0

System.out.println("Enter numbers to add (Enter 0 to stop):");

while(true) {

number = sc.nextDouble(); // Read user input

// Check if user entered 0

if(number == 0) {

break; // Exit the loop when user enters 0

// Add the number to the total

total += number;

// Display the total sum

System.out.println("The total sum is: " + total);

import java.util.Scanner;

public class SumUntilZeroOrNegative {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);


Week 1

// Initialize total to 0.0

double total = 0.0;

double number;

// Use infinite while loop

System.out.println("Enter numbers to add (Enter 0 or a negative number to stop):");

while(true) {

number = sc.nextDouble(); // Read user input

// Check if user entered 0 or a negative number

if(number <= 0) {

break; // Exit the loop when user enters 0 or a negative number

// Add the number to the total

total += number;

// Display the total sum

System.out.println("The total sum is: " + total);

You might also like