0% found this document useful (0 votes)
23 views11 pages

22bce033 Prac 3 Oops

Uploaded by

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

22bce033 Prac 3 Oops

Uploaded by

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

Name:- BHANDERI

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) {

Scanner sc=new Scanner(System.in);


System.out.print("Enter Number : ");
int num=sc.nextInt();
int sum=0;
int prod=1;
int temp=num;
while(num>0)
{
int digit=num%10;
sum+=digit;
prod*=digit;
num/=10;
}

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;

//ye variables explicit type conversions ke liye


double d2 = 123.456;
float fd = (float) d2;
long ld = (long) d2;
int id = (int) d2;
char cd2 = (char) d2;
short sd = (short) d2;
byte bd = (byte) d2;

//Automatic Type conversion ko print karane ke liye

System.out.println("Automatic Conversion : ");


System.out.println("b: " + b);
System.out.println("s: " + s);
System.out.println("i: " + i);
System.out.println("l: " + l);
System.out.println("f: " + f);
System.out.println("d: " + d);
System.out.println("c: " + c);
System.out.println("ci: " + ci);
System.out.println("cl: " + cl);
System.out.println("cf: " + cf);
System.out.println("cd: " + cd);

//explicit tpe conversion ko print karane ke liye


System.out.println("\n Explicit Conversion : ");
System.out.println("d2: " + d2);
System.out.println("fd: " + fd);
System.out.println("ld: " + ld);
System.out.println("id: " + id);
System.out.println("cd2: " + cd2);
System.out.println("sd: " + sd);
System.out.println("bd: " + bd);
}
}

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:-

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy