0% found this document useful (0 votes)
12 views2 pages

Part1 Java Programs

Uploaded by

Maya Joshi
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)
12 views2 pages

Part1 Java Programs

Uploaded by

Maya Joshi
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

Part 1 - Java Programs

1(a) Sum of digits of a number


public class SumOfDigits {
public static void main(String[] args) {
int num = 1234, sum = 0;
int temp = num;
while (temp > 0) {
sum = sum + temp % 10;
temp = temp / 10;
}
[Link]("Sum of digits of " + num + " = " + sum);
}
}

1(b) Reverse a number


public class ReverseNumber {
public static void main(String[] args) {
int num = 1234, rev = 0;
int temp = num;
while (temp > 0) {
rev = rev * 10 + temp % 10;
temp = temp / 10;
}
[Link]("Reverse of " + num + " = " + rev);
}
}

1(c) Check palindrome number


public class PalindromeCheck {
public static void main(String[] args) {
int num = 121, rev = 0, temp = num;
while (temp > 0) {
rev = rev * 10 + temp % 10;
temp = temp / 10;
}
if (num == rev)
[Link](num + " is a Palindrome");
else
[Link](num + " is not a Palindrome");
}
}

1(d) Student details using class


class Student {
String name;
int rollNo;
int marks;

void display() {
[Link]("Name: " + name);
[Link]("Roll No: " + rollNo);
[Link]("Marks: " + marks);
}
}

public class StudentDetails {


public static void main(String[] args) {
Student s1 = new Student();
[Link] = "Aman";
[Link] = 1;
[Link] = 85;

Student s2 = new Student();


[Link] = "Riya";
[Link] = 2;
[Link] = 90;

[Link]();
[Link]();
}
}

1(e) Find nCr and nPr


public class NCRNPR {
static int fact(int n) {
int f = 1;
for (int i = 1; i <= n; i++) {
f = f * i;
}
return f;
}

public static void main(String[] args) {


int n = 5, r = 2;
int ncr = fact(n) / (fact(r) * fact(n - r));
int npr = fact(n) / fact(n - r);

[Link]("nCr = " + ncr);


[Link]("nPr = " + npr);
}
}

1(f) Check vowel or consonant


public class VowelCheck {
public static void main(String[] args) {
char ch = 'e';
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
[Link](ch + " is a Vowel");
} else {
[Link](ch + " is a Consonant");
}
}
}

You might also like