Professional Documents
Culture Documents
CODECHUM COMPILATION
JAVA BASICS
JAVA CONDITIONALS
JAVA LOOPING
OOPR211 CODECHUM COMPILATION
JAVA BASICS
1. Leftovers
We Filipinos are quite known for the habit of leaving at least one of
the many pieces of food on the plate as it can't be equally divided
among all at most times. Just like when 3 people decide to eat a
pizza of 10 slices, there will often be at least one slice left on the
box. To accurately show just how many pieces of things are left
when divided by a certain number, we code it using modulo.
Care to do it for me?
Input Output
JAVA BASICS
1. Leftovers PROGRAM
import java.util.Scanner;
class Main
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
int num = input.nextInt();
int num2 = input.nextInt();
int num3 = num%num2;
System.out.println(num3);
input.close();
}
}
OOPR211 CODECHUM COMPILATION
JAVA BASICS
Do you like reading books? If you do, then you must have
encountered texts that have quite a wide space with dots in
between them, just like pausing or thinking before proceeding the
narration. Let’s try doing that here in C, shall we?
Input Output
JAVA BASICS
import java.util.Scanner;
class Main
{
JAVA BASICS
3. Enumeration
Have you ever answered a test question that tells you to enumerate
a series of things, but the answer must be in a sentence? Quite
tiring to write those things separated by commas and a single
space, isn't it?
Input Output
JAVA BASICS
3. Enumeration PROGRAM
import java.util.Scanner;
class Main
{
JAVA BASICS
Input Output
JAVA BASICS
import java.util.Scanner;
class Main
{
JAVA BASICS
Input Output
JAVA BASICS
import java.util.Scanner;
class Main
{
public static void main(String args[])
{
JAVA CONDITIONALS
1. Positive or Negative?
When you're feeling good, they call you positive. But when you're
feeling down, they call you negative. Try to determine whether a
number is positive or negative!
Input Output
JAVA CONDITIONALS
import java.util.Scanner;
class Main {
public static void main(String args[]) {
JAVA CONDITIONALS
2. Sum Cubes
Input Output
JAVA CONDITIONALS
import java.util.Scanner;
class Main {
public static void main(String args[]) {
JAVA CONDITIONALS
3. Security Check
Woah there, what are you doing roaming around past the curfew?
I'm going to have to ask for some identification.
Input Output
JAVA CONDITIONALS
import java.util.Scanner;
class Main {
public static void main(String args[]) {
JAVA CONDITIONALS
4. Operation: Operation
Input Output
JAVA CONDITIONALS
4. Operations: Operation PROGRAM
import java.util.Scanner;
class Main {
public static void main(String args[]) {
Scanner input = new Scanner(System.in );
double num1, num2;
char operator;
num1= input.nextDouble();
operator = input.next().charAt(0);
num2= input.nextDouble();
switch(operator)
{
case '+' :
{
double addition = num1 + num2;
System.out.format("%.2f",addition);
break;
}
case '-' :
{
double subtraction = num1 - num2;
System.out.format("%.2f",subtraction);
break;
}
case '*' :
{
double multiplication = num1 * num2;
System.out.format("%.2f",multiplication);
break;
}
case '/' :
{
double division = num1 / num2;
System.out.format("%.2f",division);
break;
}
default :
{
System.out.println("Invalid");
return;
}
}
}
}
OOPR211 CODECHUM COMPILATION
JAVA CONDITIONALS
We've been giving positive numbers too much attention lately, it's
time to let negative numbers take the spotlight this time!
Input Output
JAVA CONDITIONALS
5. NEG DEC TALLY PROGRAM
import java.util.Scanner;
class Main{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
float num = input.nextFloat();
float num2 = input.nextFloat();
float num3 = input.nextFloat();
float num4 = input.nextFloat();
if (num <= 0 && num2 <= 0 && num3 <= 0 && num4 <= 0){
double equal = num + num2 + num3 + num4;
System.out.format("%.2f", equal);
}
else if (num >= 0 && num2 <= 0 && num3 <= 0 && num4 <= 0){
double equal = num2 + num3 + num4;
System.out.format("%.2f", equal);
}
else if (num <= 0 && num2 >= 0 && num3 <= 0 && num4 <= 0){
double equal = num + num3 + num4;
System.out.format("%.2f", equal);
}
else if (num <= 0 && num2 <= 0 && num3 >= 0 && num4 <= 0){
double equal = num + num2 + num4;
System.out.format("%.2f", equal);
}
else if (num <= 0 && num2 <= 0 && num3 <= 0 && num4 >= 0){
double equal = num + num2 + num3;
System.out.format("%.2f", equal);
}
else if (num <= 0 && num2 >= 0 && num3 >= 0 && num4 >= 0){
double equal = num;
System.out.format("%.2f", equal);
}
else if (num >= 0 && num2 <= 0 && num3 >= 0 && num4 >= 0){
double equal = num2;
System.out.format("%.2f", equal);
}
else if (num >= 0 && num2 >= 0 && num3 <= 0 && num4 >= 0){
double equal = num3;
System.out.format("%.2f", equal);
}
OOPR211 CODECHUM COMPILATION
JAVA CONDITIONALS
5. NEG DEC TALLY CONT. PROGRAM
else if (num >= 0 && num2 >= 0 && num3 >= 0 && num4 <= 0){
double equal = num4;
System.out.format("%.2f", equal);
}
else if (num <= 0 && num2 <= 0 && num3 >= 0 && num4 >= 0){
double equal = num + num2;
System.out.format("%.2f", equal);
}
else if (num <= 0 && num2 >= 0 && num3 <= 0 && num4 >= 0){
double equal = num + num3;
System.out.format("%.2f", equal);
}
else if (num <= 0 && num2 >= 0 && num3 >= 0 && num4 <= 0){
double equal = num + num4;
System.out.format("%.2f", equal);
}
else if (num >= 0 && num2 <= 0 && num3 <= 0 && num4 >= 0){
double equal = num2 + num3;
System.out.format("%.2f", equal);
}
else if (num >= 0 && num2 <= 0 && num3 >= 0 && num4 <= 0){
double equal = num2 + num4;
System.out.format("%.2f", equal);
}
else if (num >= 0 && num2 >= 0 && num3 <= 0 && num4 <= 0){
double equal = num3 + num4;
System.out.format("%.2f", equal);
}
else if (num >= 0 && num2 >= 0 && num3 >= 0 && num4 >= 0){
double equal = 00.00;
System.out.format("%.2f", equal);
}
}
}
OOPR211 CODECHUM COMPILATION
JAVA CONDITIONALS
6. Largest Digit
This one is a bit tricky. You're going to have to isolate each digit of
the integer to determine which one is the largest, so good luck!
Input Output
JAVA CONDITIONALS
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
int num = input.nextInt ();
int rem, Lar= 0;
while (num > 0)
{
rem = num % 10;
if (Lar< rem)
{
Lar= rem;
}
num = num/ 10;
}
System.out.println(Lar);
input.close();
}
}
OOPR211 CODECHUM COMPILATION
JAVA CONDITIONALS
Input Output
JAVA CONDITIONALS
import java.util.Scanner;
class Main {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
double weight, height, BMI;
weight= input.nextDouble();
height = input.nextDouble();
BMI = 703 * ( weight / (height * height));
if(BMI < 18.5){
BMI = 703 * ( weight / (height * height));
System.out.format("%.1f" +" underweight",BMI);
}
else if(BMI <= 24.9){
BMI = 703 * ( weight / (height * height));
System.out.format("%.1f" +" normal weight",BMI);
}
else if(BMI < 29.9){
BMI = 703 * ( weight / (height * height));
System.out.format("%.1f" +" overweight",BMI);
}
else if(BMI > 30){
BMI = 703 * ( weight / (height * height));
System.out.format("%.1f" +" obese",BMI);
}
}
}}
OOPR211 CODECHUM COMPILATION
JAVA CONDITIONALS
Input Output
JAVA CONDITIONALS
import java.util.Scanner;
class Main {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int road;
double speed, find, fint, dist, time;
road = input.nextInt();
dist = input.nextInt();
time = input.nextInt();
find = dist /1000;
fint = time / 60;
switch(road)
{
case 1 :
{
speed = (find / fint);
if (speed <= 60){
System.out.format("%.2f" + " within-speed-limit",speed);
break;}
}
case 2 :
{
speed = (find / fint);
if (speed <= 60){
System.out.format("%.2f" + " under-speeding",speed);
break;}
}
default :
{
System.out.println("Invalid");
return;
}
}
}
OOPR211 CODECHUM COMPILATION
JAVA CONDITIONALS
9. Visibly Divisible
Input Output
JAVA CONDITIONALS
import java.util.Scanner;
class Main{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
JAVA CONDITIONALS
10.Sort It Yourself
Input Output
JAVA CONDITIONALS
import java.io.*;
import java.util.Scanner;
import java.util.Arrays;
class Main {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int[] a = new int[3];
int temp=0;
for(int l=0;l<3;l++)
{
a[l]=input.nextInt();
}
for(int index=0;index<a.length;index++)
{
for(int z=index+1;z<a.length;z++)
{
if(a[index] > a[z] )
{
temp = a[index];
a[index] = a[z];
a[z] = temp;
}
}
}
for(int v=0;v<a.length;v++)
{
System.out.print(a[v]+" ");
}
}
}
OOPR211 CODECHUM COMPILATION
JAVA CONDITIONALS
Input Output
JAVA CONDITIONALS
import java.util.Scanner;
class Main{
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int a = input.nextInt();
if(a % 3 == 0 && a % 5 == 0) {
System.out.println("FizzBuzz");
} else if (a % 5 == 0) {
System.out.println("Buzz");
} else if (a % 3 == 0) {
System.out.println("Fizz");
}
}
}
OOPR211 CODECHUM COMPILATION
JAVA CONDITIONALS
Can you identify if Cody's name is spelled right? If so, then make a
program that accepts four one-letter strings separated by space
and print "Correct" if the inputted strings, combined in order, spell
the name Cody correctly. If not, print "Wrong".
Input Output
JAVA CONDITIONALS
12. Is it you, Cody? PROGRAM
import java.util.Scanner;
class Main{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
String a;
String b;
String c;
String d;
a = input.next();
b = input.next();
c = input.next();
d = input.next();
if(a.equals("c") || a.equals("C"))
{
if(b.equals("o") || b.equals("O"))
{
if(c.equals("d") || c.equals("D"))
{
if(d.equals("y") || d.equals("Y"))
{
System.out.println("Correct");
}
else
{
System.out.println("Wrong");
}
}
else
{
System.out.println("Wrong");
}
}
else
{
System.out.println("Wrong");
}
}
else
OOPR211 CODECHUM COMPILATION
JAVA LOOPINGS
1. Broken Record
Input Output
JAVA LOOPING
1. Broken Record PROGRAM
import java.util.Scanner;
class Main {
public static void main(String args[]) {
int n = input.nextInt();
int i = 1;
while(i <=n) {
System.out.print("CodeChum is awesome
\n");
i++;
}
}
}
OOPR211 CODECHUM COMPILATION