Activity No. 1
Java program to get a number from the user and print whether it is positive or negative.
Code:
import java.util.Scanner;
public class Exercise1 {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Input number: ");
int input = in.nextInt();
if (input > 0)
{
System.out.println("Number is positive");
}
else if (input < 0)
{
System.out.println("Number is negative");
}
else
{
System.out.println("Number is zero");
}
}
}
==============================================
Activity no 2
Java program that prints the greatest number from the user's input
Code:
import java.util.Scanner;
public class Ifelse2 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Input the 1st number: ");
int num1 = in.nextInt();
System.out.print("Input the 2nd number: ");
int num2 = in.nextInt();
System.out.print("Input the 3rd number: ");
int num3 = in.nextInt();
if (num1 > num2 || num1 > num3) {
System.out.println("The greatest: " + num1);
}
else if (num2 > num1 || num2 > num3) {
System.out.println("The greatest: " + num2);
}
else {
System.out.println("The greatest: " + num3);
}
}
}
================================================
Activity No. 3
Java program that asks the user to enter a number either positive or negative. it will print "zero" if the number is zero. Otherwise, print "positive" or "negative". iIf the number entered by the user is greater than 10000000. The message "Positive large number" appears on the screen . if the number entered by the the user is -100000000, the message "Negative large number will appear on the screen"
Code:
import java.util.Scanner;
public class ifelseabsolute {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Input value: ");
double input = in.nextDouble();
if (input > 0)
{
if (input < 1)
{
System.out.println("Positive small number");
}
else if (input > 1000000)
{
System.out.println("Positive large number");
}
else
{
System.out.println("Positive number");
}
}
else if (input < 0)
{
if (Math.abs(input) < 1)
{
System.out.println("Negative small number");
}
else if (Math.abs(input) > 1000000)
{
System.out.println("Negative large number");
}
else
{
System.out.println("Negative number");
}
}
else
{
System.out.println("Zero");
}
}
}
================================================
Activity no . 4
Java program to enter 5 numbers and find their sum and average.
Code:
import java.util.Scanner;
public class ForLoopConditional {
public static void main(String[] args) {
int i,n=0,s=0;
double avg;
{
System.out.println("Input the 5 numbers : ");
}
for (i=0;i<5;i++)
{
Scanner in = new Scanner(System.in);
n = in.nextInt();
s +=n;
}
avg=s/5;
System.out.println("The sum of 5 no : " +s);
System.out.println("The Average is : " +avg);
}
}
===========================================
Activity no. 5
Java program that keeps a number from the user and generates an integer between 1 and 7 and displays the name of the weekday.
Code:
import java.util.Scanner;
public class NameofWeekday {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Input number: ");
int day = in.nextInt();
System.out.println(getDayName(day));
}
// Get the name for the Week
public static String getDayName(int day) {
String dayName = "";
switch (day) {
case 1: dayName = "It's Monday";
break; // To break Case 1
case 2: dayName = "It's Tuesday";
break;// To break Case 2
case 3: dayName = "It's Wednesday";
break; // To break Case 3
case 4: dayName = "It's Thursday";
break; // To break Case 4
case 5: dayName = "It's Friday";
break; // To break Case 5
case 6: dayName = "It's Saturday";
break; // To break Case 6
case 7: dayName = "It's Sunday";
break; // To break Case 7
default:
dayName = "It's Invalid day range"; // this will appear if the input is invalid number
//was invalid
}
return dayName; // to return in dayname
}
}
==============================================
Activity no. 6
Java program that allows the user to enter a number aligned from the month finds the number of days in a month.
Code:
import java.util.Scanner;
public class Noofdaysinmonths {
public static void main(String[] strings) {
Scanner input = new Scanner(System.in);
int numofdaysinmonth = 0;
String MonthOfName = "Unknown";
System.out.print("Input a month number: ");
int month = input.nextInt();
System.out.print("Input a year: ");
int year = input.nextInt();
switch (month) {
case 1:
MonthOfName = "January";
numofdaysinmonth = 31;
break;
case 2:
MonthOfName = "February";
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
numofdaysinmonth = 29;
} else {
numofdaysinmonth = 28;
}
break;
case 3:
MonthOfName = "March";
numofdaysinmonth = 31;
break;
case 4:
MonthOfName = "April";
numofdaysinmonth = 30;
break;
case 5:
MonthOfName = "May";
numofdaysinmonth = 31;
break;
case 6:
MonthOfName = "June";
numofdaysinmonth = 30;
break;
case 7:
MonthOfName = "July";
numofdaysinmonth = 31;
break;
case 8:
MonthOfName = "August";
numofdaysinmonth = 31;
break;
case 9:
MonthOfName = "September";
numofdaysinmonth = 30;
break;
case 10:
MonthOfName = "October";
numofdaysinmonth = 31;
break;
case 11:
MonthOfName = "November";
numofdaysinmonth = 30;
break;
case 12:
MonthOfName = "December";
numofdaysinmonth = 31;
}
System.out.print(MonthOfName + " " + year + " has " + numofdaysinmonth + " days\n");
}
}
=========================================
Activity no. 7
Java program to display the 10 generated numbers .
Code:
public class LoopCondition {
public static void main(String[] args)
{
int i;
System.out.println ("The ten generated numbers are: ");
for (i=1;i<=10;i++)
{
System.out.println (i);
}
System.out.println ("\n");
}
}
==========================================
Activity no. 7
Java program that will ask the user to enter a number(terms) and the sum will appear on the screen.
Code:
import java.util.Scanner;
public class naturalnumberenter {
public static void main(String[] args)
{
int i, n, sum=0;
{
Scanner in = new Scanner(System.in);
System.out.print("Input number: ");
n = in.nextInt();
}
System.out.println("The first n natural numbers are : "+n);
for(i=1;i<=n;i++)
{
System.out.println(i);
sum+=i;
}
System.out.println("The Sum of Natural Number upto "+n+ " terms : " +sum);
}
}
============================
Activity no. 8
Java program that allows the user to enter a number(terms) and get the cube of the number entered by the user.
Code:
import java.util.Scanner;
public class LoopFindtheCube {
public static void main(String[] args)
{
int i,n;
System.out.print("Input number of terms : ");
Scanner in = new Scanner(System.in);
n = in.nextInt();
for(i=1;i<=n;i++)
{
System.out.println("Number is : " +i+" and cube of " +i+" is : "+(i*i*i));
}
}
}
===========================
Activity 9
Java program that displays the multiplication table of a given integer
Code:
import java.util.Scanner;
public class MultiplicationLoop {
public static void main(String[] args)
{
int n1,n2;
{
System.out.print("Input number (terms): ");
Scanner in = new Scanner(System.in);
n1 = in.nextInt();
System.out.println ("\n");
for(n2=0;n2<=n1;n2++)
System.out.println(n1+" X "+n2+" = " +n1*n2);
}
}
}
======================================
Activity no. 10
Java program that makes such a pattern like a pyramid with a number which will repeat the number in the same row.
Code:
import java.util.Scanner;
public class PyramidLoop {
public static void main(String[] args)
{
int i,j,n,s,x;
System.out.print ("Enter a number : ");
Scanner in = new Scanner(System.in);
n = in.nextInt();
s=n+4-1;
for(i=1;i<=n;i++)
{
for(x=s;x!=0;x--)
{
System.out.print(" ");
}
for(j=1;j<=i;j++)
{
System.out.print(i+" ");
}
System.out.println();
s--;
}
}
}
==========================================
Activity 11
Java program to allows the user to enter his age. If age is greater than or equal to 18 , the message " You are allowed to vote!" otherwise "Not allowed to Vote "will appear on the screen
Code:
import java.util.Scanner;
public class voteage {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int age;
System.out.println("Enter your age: ");
age = input.nextInt();
if (age >=18) {
System.out.println("You are allowed to vote!");
}
else {
System.out.println("Allowed to vote!");
}
}
}
============================================
Walang komento:
Mag-post ng isang Komento