22bce033 Prac 3 Oops
22bce033 Prac 3 Oops
PRINSHKUMAR
Roll No.:- 22BCE033
Course & Subject Code OOPS (2CS502)
Class & Batch A(A-2)
Department CSE
PRACTICAL-3(a):-
Write a Java Program that check whether user entered number is
special number or not. For example, Consider the number is 59. First,
find the sum of all digits (5+9=14). Second, find multiplication of all
digits (5*9=45). Then find addition of sum and multiplication of all
digits (14+45=59). If it is same as number itself, than it is a special
number.
Methodology Followed:-
➔ Scan number from user.
➔ Used While loop for separation of all digits of numbers
and store it in sum and prod name variables.
➔ Using if else condition if sum+prod==num print the
result.
Code:-
import java.util.Scanner;
class Prac_3_a {
public static void main(String[] args) {
if(sum+prod==temp)
{
System.out.println("This is Special Number");
}
else
{
System.out.println("This is not Special Number");
}
}
}
Output:-
PRACTICAL-3(b):-
Write a Java program using class that prints the numbers 1 to N (N
must be scan from the user). For all multiples of 3 print “Bizz” and for
all multiples of 5 print “Fizz”. For multiples of both 3 and 5 print
“BizzFizz”.
Methodology Followed:-
1. Input: The code prompts the user to enter a number 'n' using the Scanner
class.
2. Loop: It iterates through numbers from 1 to 'n'.
3. Conditions: For each number 'i', it checks:
- If 'i' is divisible by both 3 and 5, it prints "BizzFizz".
- If 'i' is divisible by 3, it prints "Bizz".
- If 'i' is divisible by 5, it prints "Fizz".
- Otherwise, it prints the value of 'i' itself.
4. Output: The code generates the BizzFizz sequence based on the
conditions and user input.
5. Termination:The loop ends after all values from 1 to 'n' are processed.
Code:-
import java.util.Scanner;
class Prac_3_b {
public static void main(String[] args) {
System.out.print("Enter value of n : ");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(int i=1;i<=n;i++)
{
if(i%3==0 && i%5==0)
{
System.out.println(i +"-" +"BizzFizz");
}
else if(i%3==0)
{
System.out.println(i + "-"+"Bizz");
}
else if(i%5==0)
{
System.out.println(i +"-"+ "Fizz");
}
else
{
System.out.println(i);
}
}
}
}
Output:-
PRACTICAL-3(c):-
Write a Java program that demonstrate the concepts of automatic
and explicit type casting.
Methodology Followed:-
1. Automatic conversions: Initializing variables and showcasing implicit
conversions from smaller to larger data types.
2. Explicit conversions: Using type casting to convert larger data types to
smaller ones, emphasizing potential data loss.
3. Printing results: Displaying outcomes of both automatic and explicit
conversions, illustrating the conversion processes and their impact.
Code:-
public class Prac_3_c {
public static void main(String[] args) {
// Ye wala Automatic type conversions ke liye
byte b = 10;
short s = b;
int i = s;
long l = i;
float f = l;
double d = f;
char c = 'A';
int ci = c;
long cl = c;
float cf = c;
double cd = c;
Output:-
PRACTICAL-3(d):-
Write a Java program to print the ASCII values for characters entered
by the user. ASCII value of A = 65 ASCII value of B = 66…………and So
on. Print all the ASCII values on screen. Hint: First 32 characters are
non-printing, where 32 itself is an ASCII code for space. Hence cover
the characters from 32 and onwards.
Methodology Followed:-
This Java code reads a character from the user and converts it to its ASCII value,
then prints the ASCII value to the console.
Code:-
import java.util.*;
public class Prac_3_d {
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter Character : ");
char a=sc.next().charAt(0);
int n=(int) a;
System.out.println("Ascii Value of given char : "+n);
}
}
Output:-
PRACTICAL-3(e):-
Write a Java program to get particulars of his/her birthday and
display it as shown below. Use 3 variables to hold date, month and
year. Your birthday is 01/01/2001.
Methodology Followed:-
1. Input Validation - Date: The program prompts the user to enter a date and
validates that it is within the range of 1 to 31. If the input is invalid, it prints an
error message and exits the program.
2.Input Validation - Month: The program prompts the user to enter a month
and validates that it is within the range of 1 to 12. If the input is invalid, it prints
an error message and exits the program.
3. Input Validation - Year: The program prompts the user to enter a year and
validates that it's a non-negative value. If the input is invalid, it prints an error
message and exits the program.
4. Date Validation: The program uses several conditional statements to further
validate the entered date based on the month and year. It handles different
cases for odd/even months and leap years.
5. Formatting and Printing: If all validations pass (`check` is `true`), the program
formats the date and month to ensure they have leading zeros if necessary.
Then it prints the formatted date in the "dd/mm/yyyy" format.
Code:-
import java.util.Scanner;
public class Prac_3_e {
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Date (provide date in number) : ");
int date=sc.nextInt();
if(date>31 | date < 0)
{
System.out.println("Enter Valid Date");
return;
}
System.out.println("Enter Month (Provide month between 1 to 12 )");
int month=sc.nextInt();
if(month>12 | month <0)
{
System.out.println("Enter Valid Month");
return;
}
System.out.println("Enter Year (Provide year in number): ");
int year=sc.nextInt();
boolean check=true;
if(month>0 & month<13)
{
if(month%2==1 & month<8 )
{
if(date<0 | date>31)
{
System.out.println("Enter valid date");
check=false;
}
}
if(month%2==0 & month<8 & month!=2)
{
if(date<0 | date>30)
{
System.out.println("Enter valid date");
check=false;
}
}
if(month%2==0 & month>7) {
if (date < 0 | date > 31) {
System.out.println("Enter valid date");
check=false;
}
}
if(month%2==1 & month>7) {
if (date < 0 | date > 30) {
System.out.println("Enter valid date");
check=false;
}
}
if(month==2 & year%4==0)
{
if(date<0 |date>29)
{
System.out.println("Enter Valid date");
check=false;
}
}
if(month==2 & year%4!=0)
{
if(date<0|date>28)
{
System.out.println("Enter valid date");
check=false;
}
}
}
else {
System.out.println("Enter valid year");
check=false;
}
if(year<0) {
System.out.println("Enter valid year");
check=false;
}
if(check) {
if (date < 10 & month<10) {
System.out.println("0"+date+"/"+"0"+month+"/"+year);
}
else if(date>9 & month<10)
{
System.out.println(date+"/"+"0"+month+"/"+year);
}
else if(date<10 & month>10)
{
System.out.println("0"+date+"/"+month+"/"+year);
}
else
{
System.out.println(date+"/"+month+"/"+year);
}
}
}
}
Output:-