0% found this document useful (0 votes)
29 views

Programming 1 Workshop 3

This document contains 4 Java programs. The first program reverses a 2-digit number input by the user. The second program calculates the diameter, area, and circumference of a circle given its radius. The third program takes a 5-digit number as input and prints it with spaces between each digit. The fourth program takes a 6-character string as input, determines its length, and prints each character separately.

Uploaded by

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

Programming 1 Workshop 3

This document contains 4 Java programs. The first program reverses a 2-digit number input by the user. The second program calculates the diameter, area, and circumference of a circle given its radius. The third program takes a 5-digit number as input and prints it with spaces between each digit. The fourth program takes a 6-character string as input, determines its length, and prints each character separately.

Uploaded by

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

Okegbe Akpofure Kelvin-FAith

BU/17C/IT/2694

1)package ng.edu.baze.com.com3131_2021Q2;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

int num,rev=0,rem;
Scanner sc = new Scanner(System.in);
//Taking 2-digit number from User
System.out.print("Enter 2-digit Number: ");
num = sc.nextInt();
// Reversing The Entered Number
while(num != 0){
// Finding Last digit of Number
rem = num % 10;
// Storing The digits in reverse order in variable rev
rev = rev*10 + rem;
// Dividing num by 10
num = num/10; }
// Printing The Number Backwards
System.out.println("Output Is :: "+rev);
}
}

2)package ng.edu.baze.com.com3131_2021Q2;

import java.util.Scanner;
import java.lang.Math;

public class Main {

public static void main(String[] args) {


//Write a program that inputs the radius of a circle as integer, the pi as a constant of type float
//and prints the diameter, area and circumference of the circle. Use the following formulas:
//diameter = 2 * radius
//circumference = 2 * pi * radius
//area = pi * radius2

Scanner sc = new Scanner(System.in);


System.out.println("input the radius of the circle ::");
int radius = sc.nextInt();
float pi = (float) Math.PI;

float diameter = 2 * radius;


float area = 2 * pi * radius;
float circumference = pi * radius * radius;

System.out.println("The diameter of the circle is " + diameter);


System.out.println("The area of the circle is " + area);
System.out.println("The circumference of the circle is " + circumference);

}
}

3)package ng.edu.baze.com.com3131_2021Q2;

import java.util.Scanner;
import java.lang.Math;

public class Main {

public static void main(String[] args) {


//Write a program that takes a five digit number and prints the number with space between. For
//example: if the number is 23467,
//the program prints 2 3 4 6 7.
long number,digit;
//Array of size 5 to store the number in reverse order
long arr[] = new long[5];
int i=0;
Scanner sc = new Scanner(System.in);
// Taking 5-digit Number Input
System.out.print("Enter the 5-digit Number: ");
number = sc.nextInt();
// Reversing the Number And Storing it into array arr
while(number > 0) {
digit = number % 10;
arr[i] = digit;
number = number / 10;
i++; }
// Printing The Required Output
// Means, Printing The Array arr in reverse order with spaces between numbers
for (int j = arr.length - 1; j >= 0; j--)
System.out.print(arr[j]+" ");

}
}

4)package ng.edu.baze.com.com3131_2021Q2;

import java.util.Scanner;
import java.lang.Math;

public class Main {

public static void main(String[] args) {


//Write a program that reads a String of 6 characters, use the length() to determine the length of
//the string and charAt() to print each character separately.
String str;
int length;
Scanner sc = new Scanner(System.in);
//Taking String As Input From User
System.out.print("Enter the String: ");
str = sc.nextLine();
// Finding Length of String Using length()
length = str.length();
// In Case If user Enters String of Characters Greater than 6
// Then, Fetching First 6 characters only using substring method
if (length > 6) {
str = str.substring(0, 6);
}
System.out.println("Output ::");
// Printing Each Character Separately Using charAt()
for (int i = 0; i < str.length(); i++) {
System.out.println(str.charAt(i));

}
}
}

You might also like