Material File
Material File
Factorial of a Number
public class Factorial {
public static void main(String[] args) {
int num = 5, factorial = 1;
for (int i = 1; i <= num; i++) {
factorial *= i;
}
System.out.println(factorial);
}
}
---Reverse a Number
public class ReverseNumber {
public static void main(String[] args) {
int num = 12345, reversed = 0;
while (num != 0) {
reversed = reversed * 10 + num % 10;
num /= 10;
}
System.out.println(reversed);
}
}
3. Check whether the number is palindrome or not
Program:
public class Palindrome {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number");
int n = sc.nextInt();
int a,i=0,j=0;
a=n;
while(a>0) {
i=a%10;
j=(j*10)+i;
a=a/10;
}
if(n==j) {
System.out.println("It is panlidrome");
}
else {
System.out.println("It is not a panlindrome");
}
}
}
Output:
Enter the number
11
It is panlidrome
4. Check whether the number is amstrong or not
Program:
public class Amstrong{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number");
int n = sc.nextInt();
int a,i=0,j=0;
a=n;
while(a>0) {
i=a%10;
j=(i*i*i)+j;
a=a/10;
}
if(n==j) {
System.out.println("It is amstrong");
}
else {
System.out.println("It is not a amstrong");
}
}
}
Output:
Enter the number
153
It is amstrong
}
5. Print the amstrong number available between 0 to 1000
Program:
public class Amstrong{
public static void main(String[] args) {
for (int n = 1; n <= 1000; n++) {
int a, i = 0, j = 0;
a = n;
while (a > 0) {
i = a % 10;
j = j + (i * i * i);
a = a / 10;
}
if (n == j) {
System.out.println(n);
}
}
}
}
Output:
1
153
370
371
407
6. To print the palindrome available between 0 to 100
Program:
public class Palindrome {
public static void main(String[] args) {
for (int n = 1; n <= 100; n++) {
int a, i = 0, j = 0;
a = n;
while (a > 0) {
i = a % 10;
j = (j * 10) + i;
a = a / 10;
}
if (n == j) {
System.out.println(n);
}
}
}
Output:
1
2
3
4
5
6
7
8
9
11
22
33
44
55
66
77
88
99
7. Print the count of the given number
Program:
public class CountOfNumber{
public static void main(String[] args) {
int n,i=0;
System.out.println("Enter a number");
Scanner get=new Scanner(System.in);
n=get.nextInt();
while(n>0)
{
n=n/10;
i++;
}
System.out.println("No of digits present: "+i);
}
}
Output:
Enter a number
12345
No of digits present: 5
Reverse a String
public class ReverseString {
public static void main(String[] args) {
String str = "Automation";
StringBuilder reversed = new StringBuilder(str).reverse();
System.out.println(reversed);
}
}
}
62.Character count in the string
public static void main(String[] args) {
// TODO Auto-generated method stub
String s="aaabbbcccc";
int countA=0,countB=0,countC=0;
for(int i=0;i<s.length();i++) {
char ch=s.charAt(i);
if(ch=='a') {
countA++;
}else if(ch=='b') {
countB++;
}else if(ch=='c') {
countC++;
}
}
System.out.println(countA);
System.out.println(countB);
System.out.println(countC);
}
63.Higher number print
public static void main(String[] args) {
// TODO Auto-generated method stub
Set<Integer> numbers = new HashSet<>();
Collections.addAll(numbers, 2, 4, 3, 15, 7, 1);
//numbers.add(2);
//numbers.add(4);
//numbers.add(3);
// numbers.add(5);
//numbers.add(7);
//numbers.add(1);