0% found this document useful (0 votes)
2 views6 pages

Practical 2

The document contains multiple Java programs demonstrating various programming concepts. It includes a program to check for special numbers, automatic and explicit type casting, odd/even checks, character categorization, prime number verification, palindrome checking, and pattern printing using nested loops. Each program is structured with a main class and methods to perform specific tasks based on user input.

Uploaded by

khushishah5906
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)
2 views6 pages

Practical 2

The document contains multiple Java programs demonstrating various programming concepts. It includes a program to check for special numbers, automatic and explicit type casting, odd/even checks, character categorization, prime number verification, palindrome checking, and pattern printing using nested loops. Each program is structured with a main class and methods to perform specific tasks based on user input.

Uploaded by

khushishah5906
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

2 (Operators)

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.

import [Link].*;
public class special2{
public static void main(String args[]){
solution s = new solution();
[Link]();
}
}
class solution{
void sol(){
Scanner sc = new Scanner([Link]);
[Link]("Enter Number : ");
int num = [Link]();
int sum = 0, mul = 1, r = 0, n=num;
while(n != 0){
r = n%10;
sum = sum + r;
mul = mul*r;
n = n/10;

}
if(sum + mul == num){
[Link]("Yes!");
}
else{
[Link]("No!");
}
}
}
b. Write a Java program that demonstrate the concepts of automatic and explicit type casting.
import [Link].*;
public class type{
public static void main(String args[]){
Solution s = new Solution();
[Link]();
}
}
class Solution{
void sol(){
int a = 9;
double b = a;
[Link]("Implicit type Conversion : ");
[Link](b);
double c = 10.5;
int d = (int) c;
[Link]("Explicit type Conversion : ");
[Link](d);
}
}

c. Write a Java program to: i. check whether a number is odd or even (using if – else statement)
ii. check the category of a given character. (Using if…else…if ladder) ii. check whether a number
is prime or not. (Using for loop) iii. display reverse of a number and check whether it is
palindrome or not. (Using while/do while loop) import [Link];

public class menu {


public static void main(String[] args) {
Solution s = new Solution();
Scanner sc = new Scanner([Link]);

[Link]("1. Odd/Even\n2. Category of Character\n3.


Prime or Non-Prime\n4. Palindrome");
[Link]("Enter Choice : ");
int choice = [Link]();

switch (choice) {
case 1:
s.sol1(sc);
break;
case 2:
s.sol2(sc);
break;
case 3:
s.sol3(sc);
break;
case 4:
s.sol4(sc);
break;
default:
[Link]("Invalid choice!");
}
[Link]();
}
}

class Solution {
void sol1(Scanner sc) {
[Link]("Enter Number : ");
int num = [Link]();
if (num % 2 == 0) {
[Link]("Even");
} else {
[Link]("Odd");
}
}
void sol2(Scanner sc) {
[Link]("Enter Character : ");
char ch = [Link]().charAt(0);
if (ch >= 'a' && ch <= 'z') {
[Link]("Small Letters");
} else if (ch >= 'A' && ch <= 'Z') {
[Link]("Capitals");
} else if (ch >= '0' && ch <= '9') {
[Link]("Digits");
} else {
[Link]("Special Characters");
}
}

void sol3(Scanner sc) {


[Link]("Enter Number : ");
int num2 = [Link]();
boolean isComposite = false;

if (num2 <= 1) {
[Link]("Neither Prime nor Composite");
return;
}

for (int i = 2; i * i <= num2; i++) {


if (num2 % i == 0) {
isComposite = true;
break;
}
}

if (isComposite) {
[Link]("Composite");
} else {
[Link]("Prime");
}
}

void sol4(Scanner sc) {


[Link]("Enter Number : ");
int num3 = [Link]();
int new_num = num3;
int rev = 0;
int digit;

while (new_num > 0) {


digit = new_num % 10;
rev = rev * 10 + digit;
new_num = new_num / 10;
}

if (rev == num3) {
[Link]("Number is Palindrome!");
} else {
[Link]("Number is not Palindrome!");
}
}
}

d. Pattern printing. (Using nested loops)

public class pattern {


public static void main(String[] args) {
int n = 6;

for (int i = 1; i <= n; i++) {


for (int s = n - i; s > 0; s--) {
[Link](" ");
}
for (int j = 1; j <= i; j++) {
[Link](j + " ");
}

[Link]();
}
}
}

You might also like