Skip to content

Instantly share code, notes, and snippets.

@luchtech
Created August 21, 2018 07:59
Show Gist options
  • Save luchtech/ad580d349d7d40ebe60e61160505cfc9 to your computer and use it in GitHub Desktop.
Save luchtech/ad580d349d7d40ebe60e61160505cfc9 to your computer and use it in GitHub Desktop.
Laboratory Activity: Loop Structure
import java.util.Scanner;
public class EvenNum {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
System.out.println("Program: EVEN NUMBERS FROM 1-50\n");
System.out.println("Even Number from 1 to 50...");
System.out.println("----------using for loop----------");
for(int i = 1; i<=50; i++) {
if(i%2 == 0) System.out.print(i + " ");
}
System.out.println("\n----------using while loop----------");
int i = 1;
while (i <= 50) {
if (i%2 == 0) System.out.print(i + " ");
i++;
}
System.out.println("\n----------using do...while loop----------");
i = 1;
do {
if (i%2 == 0) System.out.print(i + " ");
i++;
}
while(i <= 50);
}
}
import java.util.*;
public class InterestRate
{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
double balance = 0, rate = 0.025;
boolean loop = true;
char option;
System.out.println("Program: Bank Interest Rate\n");
while (loop){
System.out.print("Enter d for deposit, w for withdrawal or x to exit: ");
option = sc.nextLine().charAt(0);
balance+=balance*(0.025/12);
switch(option){
case 'e':
loop = false;
System.out.println("Balance after transaction: "+balance+"\n");
break;
case 'd':
System.out.println("Beginning of Month Balance: "+balance);
System.out.print("Enter deposit amount: ");
double deposit = sc.nextDouble();
balance+=deposit;
System.out.println("Balance after transaction: "+balance+"\n");
break;
case 'w':
System.out.println("Beginning of Month Balance: "+balance);
System.out.print("Enter withdrawal amount: ");
double withdraw = sc.nextDouble();
balance-=withdraw;
System.out.println("Balance after transaction: "+balance+"\n");
break;
default:
System.err.println("Error: Invalid choice.");
}
sc.nextLine();
}
}
}
import java.util.Scanner;
public class NameValidation {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
System.out.println("Program: NAME VALIDATION\n");
System.out.println("Using the while loop...");
boolean loop = true;
while(loop) { // flag-controlled
System.out.print("Enter a word: ");
String str = sc.next();
if (str.equalsIgnoreCase("exit")) {
System.out.println("Program is now terminating…");
loop = false; // or just break; will do
}
else if (str.charAt(0) == str.charAt(str.length()-1))
System.out.println("The first character is equal to its last character: "+str+"\n");
else System.out.println("The first character is not equal to its last character: "+str+"\n");
}
System.out.println("\nUsing the do-while loop...");
loop = true;
do{
System.out.print("Enter a word: ");
String str = sc.next();
if (str.equalsIgnoreCase("exit")) {
System.out.println("Program is now terminating…");
loop = false; // or just break; will do
}
else if (str.charAt(0) == str.charAt(str.length()-1))
System.out.println("The first character is equal to its last character: "+str+"\n");
else System.out.println("The first character is not equal to its last character: "+str+"\n");
} while (loop); // flag-controlled
}
}
import java.util.Scanner;
public class Prime {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
System.out.println("Program: PRIME NUMBERS\n");
System.out.print("Enter number: ");
int n = sc.nextInt();
int i = 2;
boolean loop = true;
while (loop) {
if (i < n)
if (n%i == 0) {
System.out.println("Not Prime");
loop = false;
}
else i++;
else {
System.out.println("Prime");
loop = false;
}
}
}
}
import javax.swing.JOptionPane;
public class StringManipulation {
public static void main(String[] args) {
boolean loop = true;
while(loop) {
String str = JOptionPane.showInputDialog("Enter string:");
if (str == null || str.equals("DONE")) {
int response = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit?", "Exit", JOptionPane.YES_NO_OPTION);
if (response == 0) loop = false;
}
else if (str.equals(""))
JOptionPane.showMessageDialog(null, "You typed a blank input. Please try again.", "Error", JOptionPane.ERROR_MESSAGE);
else {
str.toLowerCase();
int special = 0, consonants = 0, vowels = 0, odds = 0, evens = 0;
for (int i = 0, len = str.length()-1; i <= len;i++) {
char x = str.charAt(i);
if(Character.isLetter(x)) {
if (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u')
vowels++;
else consonants++;
}
else if (Character.isDigit(x)) {
if (x%2 == 0) evens++;
else odds++;
}
else special++;
if(i == len)
JOptionPane.showMessageDialog(null, String.format("String: %s\n\n"
+ "Special Characters: %d\nConsonants: %d"
+ "\nVowels: %d\nOdd Numbers: %d\nEven Numbers: %d", str, special, consonants, vowels, odds, evens), "Result", JOptionPane.INFORMATION_MESSAGE);
}
}
}
}
}
import java.util.Scanner;
public class Sum50 {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
System.out.println("Program: SUM OF NUMBERS FROM 1-50\n");
int sum = 0;
for(int i = 50; i > 0; i--) //decrement
sum+=i;
System.out.println("Sum of numbers from 1 to 50: "+sum);
}
}
import java.util.Scanner;
public class SumofNPositiveOdds {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
System.out.println("Program: SUM OF POSITIVE ODD INTEGERS FROM 1 TO N\n");
System.out.print("Enter n: ");
int n = sc.nextInt();
int sum = 0;
for (int i = 1, j = 1; i <= n; i++) {
sum+=j;
j+=2;
}
System.out.println("Result: "+sum);
}
}
import java.util.Scanner;
public class SumOfNSquares {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
System.out.println("Program: SUM OF SQUARES FROM 1 TO N\n");
System.out.print("Enter n: ");
int n = sc.nextInt();
int sum = 0;
for (int i = 1; i <= n; i++) {
sum+=(i*i);
}
System.out.println("Result: "+sum);
}
}
import java.util.Scanner;
public class TableOfSquares {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
System.out.println("Program: TABLE OF SQUARES\n");
System.out.println("-------Using for loop----------");
for (int i = 0; i <= 20; i++)
System.out.printf("Number : %d\tSquare : %d\n", i, i*i); // Using String Formatter
System.out.println("\n-------Using while loop--------");
int i = 1;
while(i <= 20) {
System.out.printf("Number : %d\tSquare : %d\n", i, i*i); // Using String Formatter
i++;
}
System.out.println("\n-------Using do...while loop------");
i = 1;
do {
System.out.printf("Number : %d\tSquare : %d\n", i, i*i); // Using String Formatter
i++;
}while(i <= 20);
}
}
import java.util.Scanner;
public class WhiteSpaceCounter {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
System.out.println("Program: WHITESPACES COUNTER\n");
System.out.print("Enter string: ");
String str = sc.nextLine();
int whitespaces = 0;
for (int i = 0, len = str.length()-1; i <= len;i++) {
char x = str.charAt(i);
if (Character.isWhitespace(x)) {
whitespaces++;
}
}
System.out.println("Result: "+whitespaces);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment