100% found this document useful (1 vote)
5K views42 pages

Java Coding Interview Questions Guide

The document contains 23 code snippets demonstrating various Java programming concepts and examples, including: 1) Calculating the factorial of a number 2) Reversing a number 3) Checking if a number is a palindrome 4) Finding Armstrong numbers 5) Printing Armstrong numbers between 0-1000 6) Printing palindromes between 0-100 7) Counting digits in a number 8) Finding the sum of digits in a number 9) Swapping two numbers with and without a third variable 10) Checking if a number is even or odd 11) Counting even and odd numbers between 1-100 12) Printing the Fibonacci series 13) Printing Fibonacci series terms up to

Uploaded by

Binod Yadav
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
100% found this document useful (1 vote)
5K views42 pages

Java Coding Interview Questions Guide

The document contains 23 code snippets demonstrating various Java programming concepts and examples, including: 1) Calculating the factorial of a number 2) Reversing a number 3) Checking if a number is a palindrome 4) Finding Armstrong numbers 5) Printing Armstrong numbers between 0-1000 6) Printing palindromes between 0-100 7) Counting digits in a number 8) Finding the sum of digits in a number 9) Swapping two numbers with and without a third variable 10) Checking if a number is even or odd 11) Counting even and odd numbers between 1-100 12) Printing the Fibonacci series 13) Printing Fibonacci series terms up to

Uploaded by

Binod Yadav
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
  • Find the Reverse of a Number
  • Find the Factorial of a Given Number
  • Check Whether the Number is Armstrong or Not
  • Check Whether the Number is Palindrome or Not
  • Print Palindrome Numbers Available Between 0 to 100
  • Print Armstrong Numbers Available Between 0 to 1000
  • Print the Count of the Given Number
  • Swap Two Numbers Using Third Variable
  • Find the Sum of the Digits
  • Swap Two Variables Without Third Variable
  • Find Even/Odd Number
  • Count of Even and Odd Numbers
  • Fibonacci Series
  • Print Values in Fibonacci Series Up to 100
  • Reverse a String
  • Check If the String is Palindrome
  • Count Each Character in the String
  • Count Each Word
  • Print Numbers in Ascending Order
  • Print Numbers in Descending Order
  • Print Triangle with Stars
  • Find Special Characters, Uppercase, Lowercase, Digits
  • Replace String Characters
  • Print Reverse Triangle Without Space
  • Print Prime Numbers Count Between 1 to 100
  • Check Whether the Given Number is Prime
  • Multiplication of the Given Number
  • Biggest of 4 Numbers
  • Find the 3rd Maximum Number in an Array
  • Separate Reverse of Each Word in the String

1.

Find the Factorial of the given number

Program:
public class Factorial {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
[Link]("Enter the number");
int n = [Link]();
int factorial=1;
for(int i=1;i<=n;i++)
{
factorial= factorial*i;
}
[Link](factorial);
}
}

Output:
Enter the number
5
120

2. Find the reverse of the number

Program:
public class ReverseTheNumber {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
[Link]("Enter the number");
int n = [Link]();
int a,i=0,j=0;
a=n;
while(a>0) {
i=a%10;
j=(j*10)+i;
a=a/10;
}
[Link]("The reverse number is "+j);
}
}

Output:
Enter the number
12345
The reverse number is 54321

3. Check whether the number is palindrome or not

Program:
public class Palindrome {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
[Link]("Enter the number");
int n = [Link]();
int a,i=0,j=0;
a=n;
while(a>0) {
i=a%10;
j=(j*10)+i;
a=a/10;
}
if(n==j) {
[Link]("It is panlidrome");
}
else {
[Link]("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([Link]);
[Link]("Enter the number");
int n = [Link]();
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) {
[Link]("It is amstrong");
}
else {
[Link]("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) {
[Link](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) {
[Link](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;
[Link]("Enter a number");
Scanner get=new Scanner([Link]);
n=[Link]();
while(n>0)
{
n=n/10;
i++;
}
[Link]("No of digits present: "+i);
}
}

Output:
Enter a number
12345
No of digits present: 5

8. Find the Sum of the digit

Program:
public class SumOfDigits{
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
[Link]("Enter the number");
int n = [Link]();
int a,i=0,j=0;
a=n;
while(a>0) {
i=a%10;
j=j+i;
a=a/10;
}
[Link]("Sum of the digits "+j);
}
}

Output:
Enter the number
123
Sum of the digits 6

[Link] of two number using third variable

Program:
public class Swap{
public static void main(String[] args) {
int a, b, c;
Scanner sw = new Scanner([Link]);
[Link]("The numbers are");
a = [Link]();
b = [Link]();
c = a;
a = b;
b = c;
[Link]("Swapping numbers are");
[Link](a);
[Link](b);
}
}

Output:
The numbers are
12
24
Swapping numbers are
24
12

[Link] of two variable without using third variable

Program:
public class SwapTwoNumber{
public static void main(String[] args) {
int a, b;
Scanner sw = new Scanner([Link]);
[Link]("The numbers are");
a = [Link]();
b = [Link]();
a = a + b;
b = a - b;
a = a - b;
[Link]("Swapping numbers are");
[Link](a);
[Link](b);
}
}

Output:
The numbers are
12
24
Swapping numbers are
24
12

11. To find even/odd number:


Program:
public class EvenOrOdd{
public static void main(String[] args) {
Scanner e = new Scanner([Link]);
[Link]("Enter a Number");
int n = [Link]();
if (n % 2 == 0) {
[Link]("Even number");
} else {
[Link]("Odd number");
}
}
}
Output:
Enter a Number
23
Odd

12. Count of even and odd count


Program:
public class OddEvenCount{
public static void main(String[] args) {
int evencount = 0, oddCount=0;
for (int i = 1; i <= 100; i++) {
if (i % 2 == 0) {
evencount++;
}
else {
oddCount++;
}
}
[Link]("Even count is "+evencount);
[Link]("Odd count is "+oddCount); }
}

Output:
Even count is 50
Odd count is 50
13. Fibonacci series:
Program:
public class Fibonacci {
public static void main(String[] args) {
int a = 0, b = 1;
[Link](a);
[Link](b);
for (int i = 2; i <= 10; i++) {
int c = a + b;
[Link](c);
a = b;
b = c;
}
}
}

Output:
0
1
1
2
3
5
8
13
21
34
55

14. Print the value in Fibonacci series up to 100


Program:
public class Fibonacci{
public static void main(String[] args) {
int a = 0, b = 1;
[Link](a);
[Link](b);
for (int i = 1; i <= 10; i++) {
int c = a + b;
if(c<=100)
a = b;
b = c;
[Link](c);
}
}
}

Output:
0
1
1
2
3
5
8
13
21
34
55
89

15. Reverse the String


Program:
public class ReverseString{
public static void main(String args[]) {
String original, reverse = "";
Scanner in = new Scanner([Link]);
[Link]("Enter a string to reverse");
original = [Link]();
int length = [Link]();
for (int i = length - 1; i >= 0; i--)
reverse = reverse + [Link](i);
[Link]("Reverse of entered string is: " + reverse);
}
}

Output:
Enter a string to reverse
nishathi
Reverse of entered string is: ihtahsin

[Link] Check the String is palindrome or not.

Program:
public class Palindrome {
public static void main(String args[])
{
String original, reverse = "";
Scanner in = new Scanner([Link]);
[Link]("Enter a string to check if it is a palindrome");
original = [Link]();
int length = [Link]();
for ( int i = length - 1; i >= 0; i-- )
reverse = reverse + [Link](i);
if ([Link](reverse))
[Link]("Entered string is a palindrome.");
else
[Link]("Entered string is not a palindrome.");
}
}

Output:
Enter a string to check if it is a palindrome
madam
Entered string is a palindrome.

[Link] of each Character in the String


Program:
public class CountOfCharacter {
public static void main(String args[]) {
{
String s = "vengatram";
HashMap<Character, Integer> emp = new HashMap<Character,
Integer>();
char[] ch = [Link]();
for (char c : ch) {
if ([Link](c)) {
int x = [Link](c);
[Link](c, x + 1);
} else {
[Link](c, 1);
}
}
[Link](emp);
}
}
}
Output:
{a=2, r=1, t=1, e=1, v=1, g=1, m=1, n=1}
[Link] of each Word
Program:
public class CountOfWord{
public static void main(String args[]) {
{
String s = "vengat ram";
String[] s1 = [Link](" ");
HashMap<String, Integer> emp = new HashMap<String,
Integer>();
for (String c : s1) {
if ([Link](c)) {
int x = [Link](c);
[Link](c, x + 1);
} else {
[Link](c, 1);
}
}
[Link](emp);
}}
}
Output:
{vengat=1, ram=1}

19. Print the numbers in ascending order


Program:
public class AscendingOrder {
public static void main(String[] args)
{
int n, temp;
Scanner s = new Scanner([Link]);
[Link]("Enter no. of elements you want in array:");
n = [Link]();
int a[] = new int[n];
[Link]("Enter all the numbers:");
for (int i = 0; i < n; i++)
{
a[i] = [Link]();
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
[Link]("Ascending Order:");
for (int i = 0; i < n - 1; i++)
{
[Link](a[i] + ",");
}
[Link](a[n - 1]);
}
}
Output:
Enter no. of elements you want in array: 10
Enter all the numbers:
20
30
40
50
60
70
80
90
100
120
Ascending Order:20,30,40,50,60,70,80,90,100,120

[Link] the numbers in descending order


Program:
public class DescendingOrder{
public static void main(String[] args) {
int n, temp;
Scanner s = new Scanner([Link]);
[Link]("Enter no. of elements you want in array:");
n = [Link]();
int a[] = new int[n];
[Link]("Enter all the elements:");
for (int i = 0; i < n; i++) {
a[i] = [Link]();
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (a[i] > a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
[Link]("Descending Order:");
for (int i = n - 1; i > 0; i--) {
[Link](a[i] + ",");
}
[Link](a[0]);
}
}
Output:
Enter no. of elements you want in array: 5
Enter all the elements:
90
50
35
48
12
Descending Order:90,50,48,35,12

[Link] Triangle with Stars


Program:
public class Triangle{
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5 - i; j++) {
[Link]("* ");
}
for (int k = 1; k <= i; k++) {
[Link]("* ");
}
[Link](" ");}}}
Output:
*
**
***
****
*****

22. Assume the string is he,xa,wa,re and give the output as


hexaware
Program:
public class Replace {
public static void main(String[] args) {
String s="he,xa,wa,re";
String x = [Link](",", "");
[Link](x);
}
}
Output:
Hexaware

[Link] the special character, uppercase, lowercase, Number of


digits in the given string
Program:
public class CharCount{
public static void main(String[] args) {
String s = "Hi Welcome To Java Classes Tommorow At 2.00
p.m!!";
int count = 0;
int count1 = 0;
int count2 = 0;
int count3 = 0;
for (int i = 0; i < [Link](); i++) {
if ([Link](i) >= 'a' && [Link](i) <= 'z') {
count++;
} else if ([Link](i) >= 'A' && [Link](i) <= 'Z') {
count1++;
} else if ([Link](i) >= '0' && [Link](i) <= '9') {
count2++;
} else {
count3++;
}
}
[Link]("total no of small letters: " + count);
[Link]("total no of capital letters: " + count1);
[Link]("total no of digits: " + count2);
[Link]("total no of special characters: " + count3);
}
}
Output:
total no of small letters: 27
total no of capital letters: 7
total no of digits : 3
total no of special characters: 12

24. Print Reverse triangle without Space


Program:
public class ReverseTriangle{
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 5; j >= i; j--) {
[Link]("* ");
}
[Link]();
}
}
}
Output:
*****
****
***
**
*

25 . Check Whether the given number is prime or not


Program:
public class Prime{
public static void main(String[] args) {
int n;
Scanner input = new Scanner([Link]);
[Link]("Enter the number");
n = [Link]();
int count = 0;
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0) {
count = 1;
}
}
if (count == 0) {
[Link]("It is a prime number");
} else {
[Link]("It is not a prime number");
}
}
}
Output:
Enter the number
17
It is a prime number

26. Print the prime numbers counts available between 1 to 100


Program:
public class PrimeNumberCount{
public static void main(String[] args) {
int count, c = 0;
for (int i = 1; i <= 100; i++) {
count = 0;
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) {
count++;
}
}
if (count == 0) {
c++;
}
}
[Link](c);
}
}
Output:
26

27. Multiplication of the given number


Program:
public class Multiplication{
public static void main(String[] args) {
int n, j;
Scanner mt = new Scanner([Link]);
[Link]("Enter the Table");
n = [Link]();
[Link]("Table upto");
j = [Link]();
for (int i = 1; i <= j; i++) {
int c = n * i;
[Link](i + "*" + n + "=" + c);
}
}}
Output:
Enter the Table
7
Table upto
10
1*7=7
2*7=14
3*7=21
4*7=28
5*7=35
6*7=42
7*7=49
8*7=56
9*7=63
10*7=70

28. Biggest of 4 number


Program:
public class BiggestNumber{
public static void main(String[] args) {
int a, b, c, d;
Scanner bn = new Scanner([Link]);
[Link]("The four numbers are");
a = [Link]();
b = [Link]();
c = [Link]();
d = [Link]();
if (a > b && a > c && a > d) {
[Link]("The biggest number is= " + a);
} else if (b > a && b > c && b > d) {
[Link]("The biggest number is= " + b);
} else if (c > a && c > b && c > d) {
[Link]("The biggest number is= " + c);
} else {
[Link]("The biggest number is= " + d);
}
}
}
Output:
The four numbers are
10
20
30
40
The biggest number is=40

29. Find the 3


rd
maximum Number in an given array
Program:
public class ThirdMax{
public static void main(String[] args) {
int a[]={-12,45,-23,64,-100,24};
for(int i=0;i<[Link];i++){
for(int j=i+1;j<[Link];j++){
int temp=0;
if(a[i]<a[j]){
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
}
for(int k=0;k<[Link];k++){
[Link](a[k]);
}
[Link]("The Third maximum number is " +
a[a.length4]);
}
}
Output:
64
45
24
-12
-23
-100
The Third maximum number is 24

30. Separate reverse of each word in the string


Program:
public class Reverse{
public static void main(String[] args)
{
String name = "Greens Tech";
String [] s =[Link](" ");
String res = "";
for(int i=0;i<[Link];i++)
{
String t = s[i];
for(int j=[Link]()-1;j>=0;j--)
{
char ch=[Link](j);
res=res+ch;
}
res=res+ " ";
}
[Link](res);
}
}
Output:
sneerG hceT
31. Number triangle
Program:
public class Welcome {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5 - i; j++) {
[Link](" ");
}
for (int k = 1; k <= i; k++) {
[Link](i+" ");
}
[Link](" ");
}
}
}
Output:
1
22
333
4444
55555

32. Find the duplicate count in an array


Program:
public class ArrayDuplicate {
public static void main(String[] args)
{
int n, count=0;
Scanner s = new Scanner([Link]);
[Link]("Enter no. of elements you want in array: ");
n = [Link]();
int a[] = new int[n];
[Link]("Enter all the numbers: ");
for (int i = 0; i < n; i++)
{
a[i] = [Link]();
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if(a[i]==a[j]) {
count++;
}
}}
[Link](count);
}
}
Output:
Enter no. of elements you want in array: 5
Enter all the numbers:
10
20
10
30
10
3

[Link] the duplicate count in the string


Program:
public class ListDuplicate {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
[Link]("a");
[Link]("b");
[Link]("c");
[Link]("d");
[Link]("b");
[Link]("c");
[Link]("a");
[Link]("a");
[Link]("a");
[Link]("Count all with frequency");
Set<String> uniqueSet = new HashSet<String>(list);
for (String temp : uniqueSet) {
[Link](temp + ": " +
[Link](list, temp));
}
}
}
Output:
Count all with frequency
a: 4
b: 2
c: 2
d: 1

[Link] of the palindrome number


Program:
public class PalindromeCount{
public static void main(String[] args) {
int c = 0;
for (int n = 1; n <= 1000; 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) {
c++;
}
}
[Link](c);
}
}
Output:
106

35. Count of the amstrong number


Program:
public class AmstrongCount {
public static void main(String[] args) {
int c = 0;
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) {
c++;
}
}
[Link](c);
}
}
Output:
5

[Link] the triangle pyramid


Program:
public class TrianglePyramid{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("How Many Rows You Want In Your Pyramid?");
int noOfRows = [Link]();
int rowCount = 1;
[Link]("Here Is Your Pyramid");
for (int i = noOfRows; i >= 1; i--)
{
//Printing i*2 spaces at the beginning of each row
for (int j = 1; j <= i*2; j++)
{
[Link](" ");
}
//Printing j where j value will be from i to noOfRows
for (int j = i; j <= noOfRows; j++)
{
[Link](j+" ");
}
for (int j = noOfRows-1; j >= i; j--)
{
[Link](j+" ");}
[Link]();
//Incrementing the rowCount
rowCount++;
}
}
}
Output:
How Many Rows You Want In Your Pyramid?
5
Here Is Your Pyramid
5
454
34543
2345432
123454321

37. Count of vowels and non vowels


Program:
public class VowelsCount {
public static void main(String[] args) {
String a = "welcome";
int vowels = 0;
int nonVowels = 0;
for (int i = 0; i < [Link](); i++) {
char ch = [Link](i);
if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i'
|| ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u'
|| ch == 'U') {
vowels++;
} else {
nonVowels++;
}
}
[Link]("Count of vowels is "+vowels);
[Link]("Count of Non Vowels is "+nonVowels);
}
}
Output:
Count of vowels is 3
Count of Non Vowels is 4

[Link] duplicates from stored array


Program:
public class RemoveDuplicate {
public static int[] removeDuplicates(int[] input){
int j = 0;
int i = 1;
//return if the array length is less than 2
if([Link] < 2){
return input;
}
while(i < [Link]){
if(input[i] == input[j]){
i++;
}else{
input[++j] = input[i++];
}
}
int[] output = new int[j+1];
for(int k=0; k<[Link]; k++){
output[k] = input[k];
}
return output;
}
public static void main(String a[]){
int[] input1 = {2,3,6,6,8,9,10,10,10,12,12};
int[] output = removeDuplicates(input1);
for(int i:output){
[Link](i+" ");
}
}
}
Output:
{2 3 6 8 9 10 12}

[Link] of the odd and even number


Program:
public class SumOfOdd{
public static void main(String[] args) {
int oddCount = 0,evenCount=0;
for (int i = 1; i <= 100; i++) {
if (i % 2 == 1) {
oddCount= oddCount + i;
}
else {
evenCount=evenCount+i;
}
}
[Link]("Count of odd number is "+oddCount);
[Link]("Count of even number is "+evenCount);
}
}
Output:
Count of odd number is 2500
Count of even number is 2550

[Link] of Uppercase, lowercase, digits, special character


Program:
public class Test {
public static void main(String[] args) {
int lCaseCount = 0, uCaseCount = 0, numbersCount = 0,
sCharCount = 0;
String s = "Welcome To JAVA Clas @ 12345";
for (int i = 0; i < [Link](); i++) {
char ch = [Link](i);
if ([Link](ch)) {
lCaseCount++;
} else if ([Link](ch)) {
uCaseCount++;
} else if ([Link](ch)) {
numbersCount++;
} else {
sCharCount++;
}
}
[Link]("Upper Case Count: " + uCaseCount);
[Link]("Lower Case Count: " + lCaseCount);
[Link]("Numbers Count: " + numbersCount);
[Link]("Special Characters Count: " + sCharCount);
}
}
Output:
Upper Case Count: 7
Lower Case Count: 10
Numbers Count: 5
Special Characters Count: 6

Common questions

Powered by AI

Detecting palindrome numbers between a range involves iterating through each number, reversing it, and comparing against the original. The time complexity is O(n*m), where n is the range limit and m is the average number length; each number requires reversal operations proportional to its digit count. Execution time could be significant for larger ranges unless optimizations or parallel processing are employed .

To optimize the Armstrong number check algorithm, I suggest removing redundant calculations and clarifying the logic by using consistent variable naming. Instead of recomputing the modulus and cube of digits in every iteration, store the operation results in variables and utilize them for comparison after summing. This simplification could enhance readability and potentially improve performance due to reduced operations .

Counting even and odd numbers within a set range demonstrates iteration efficiency by leveraging a simple modulus operation within a loop restricted to a defined iteration limit, i.e., from 1 to 100. The program efficiently maintains counters for even and odd numbers separately, requiring minimal computational overhead, thus showcasing streamlined iteration with clear partitioning logic .

Both palindrome detection and number reversal utilize the logic of iterating through the number's digits in reverse order using modulus and division operations. However, to differentiate, the palindrome check compares the reversed number with the original, while reversal simply outputs the constructed reverse without comparison. The logic involves checking if the original number is equal to the reversed number after reconstruction to confirm palindrome status .

The program calculates the factorial of a number by iterating from 1 to the number 'n' and multiplying the result by each integer in the iteration loop. The computational consideration for larger numbers should include managing integer overflow, as factorial values grow exponentially, potentially exceeding the storage capacity of data types like int. Utilizing data types that support larger number ranges or implementing a method to handle large integers could be essential .

The algorithm determines the third maximum number by sorting the array in descending order through nested loops and then selecting the third element. It presumes no less than three unique elements; otherwise, results may be inaccurate. Consideration of unique element existence and preventing out-of-bound errors are vital for accuracy .

Swapping numbers without a third variable is based on arithmetic operations: adding both numbers, then reassigning by subtraction to isolate original values into new variables. Its computational complexity is O(1) in terms of space and time, as it involves limited arithmetic operations that remain constant irrespective of the values used .

Handling character count diversity in strings holds importance in tasks like data validation, formatting checks, and cryptographic processes. Differentiating types assists in enforcing specific input formats, enhancing security by managing potential vulnerabilities through character analysis, and improving user experience by ensuring clear, expected output .

Reversing each word in a string involves splitting the string into separate words, individually reversing them, and then recombining. This differs from reversing the string's character order entirely. Computational complexity for word-based reversal tends to be greater due to additional splitting and concatenation operations compared to a straightforward character scan for full reversal .

Integer overflow can occur when extending the Fibonacci sequence beyond 100 due to exponential growth in sequence numbers. Once numbers surpass the maximum value storable in the chosen data type (e.g., int), overflow occurs, causing incorrect results. Recognizing this, using data types like long for larger sequences or libraries supporting arbitrary-precision arithmetic is advised .

1.Find the Factorial of the given number 
 
Program: 
public class Factorial { 
public static void main(String[] args) { 
Sca
public static void main(String[] args) { 
Scanner sc=new Scanner(System.in); 
System.out.println("Enter the number"); 
int n
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
public class Amstrong{ 
public static void main(String[] args) { 
Scanner sc=new Scanner(System.in); 
System.out.println("Ent
5. Print the amstrong number available between 0 to 1000 
 
Program: 
public class Amstrong{ 
public static void main(String[
6. To print the palindrome available between 0 to 100 
 
Program: 
public class Palindrome { 
public static void main(Strin
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 CountOf
n=n/10; 
i++; 
} 
System.out.println("No of digits present: "+i); 
} 
} 
 
Output: 
Enter a number 
12345 
No of digits prese
} 
System.out.println("Sum of the digits "+j); 
} 
} 
 
Output: 
Enter the number 
123 
Sum of the digits 6 
 
9.Swap of two
} 
} 
 
Output: 
The numbers are 
12 
24 
Swapping numbers are 
24 
12 
 
10.Swap of two variable without using third variabl

You might also like