### 1. Write a Java program to find out the sum of digits .

// Java program to compute

// sum of digits in number.

import java.io.*;

class GFG {

/* Function to get sum of digits */

static int getSum(int n)

{

int sum = 0;

while (n != 0)

{

sum = sum + n % 10;

n = n/10;

}

return sum;

}

// Driver program

public static void main(String[] args)

{

int n = 687;

System.out.println(getSum(n));

}

### 2. Write a program to find out the prime Number in Java.

Prime number in Java: Prime number is a number that is greater than 1 and divided by 1 or itself only. In other words, prime numbers can’t be divided by other numbers than itself or 1. For example 2, 3, 5, 7, 11, 13, 17…. are the prime numbers.

Let’s see the prime number program in java. In this java program, we will take a number variable and check whether the number is prime or not.

1. publicclass PrimeExample{
2. public static void main(String args[]){
3. int i,m=0,flag=0;
4. int n=3;//it is the number to be checked
5. m=n/2;
6. if(n==0||n==1){
7. out.println(n+” is not prime number”);
8. }else{
9. for(i=2;i<=m;i++){
10. if(n%i==0){
11. out.println(n+” is not prime number”);
12. flag=1;
13. break;
14. }
15. }
16. if(flag==0)  { System.out.println(n+” is prime number”); }
17. }//end of else
18. }
19. }

Output:

### 3. Write a factorial Program using recursion in java

Let’s see the factorial program in java using recursion.

1. classFactorialExample2{
2. static int factorial(int n){
3. if (n == 0)
4. return 1;
5. else
6. return(n * factorial(n-1));
7. }
8. public static void main(String args[]){
9. int i,fact=1;
10. int number=4;//It is the number to calculate factorial
11. fact = factorial(number);
12. out.println(“Factorial of “+number+” is: “+fact);
13. }
14. }

Output:

Factorial of 4 is: 24

### 4. Write a Java Program to find Largest Number in an Array

We can find the largest number in an array in java by sorting the array and returning the largest number. Let’s see the full example to find the largest number in java array.

1. publicclass LargestInArrayExample{
2. publicstatic int getLargest(int[] a, int total){
3. inttemp;
4. for(int i = 0; i < total; i++)
5. {
6. for (int j = i + 1; j < total; j++)
7. {
8. if (a[i] > a[j])
9. {
10. temp = a[i];
11. a[i] = a[j];
12. a[j] = temp;
13. }
14. }
15. }
16. return a[total-1];
17. }
18. publicstatic void main(String args[]){
19. inta[]={1,2,5,6,3,2};
20. intb[]={44,66,99,77,33,22,55};
21. out.println(“Largest: “+getLargest(a,6));
22. out.println(“Largest: “+getLargest(b,7));
23. }}

Output:

### 5. Write a Java Program to find Third Largest Number in an Array

We can find the third largest number in an array in java by sorting the array and returning the 3nd largest number. Let’s see the full example to find the third largest number in java array.

1. publicclass ThirdLargestInArrayExample{
2. publicstatic int getThirdLargest(int[] a, int total){
3. inttemp;
4. for(int i = 0; i < total; i++)
5. {
6. for (int j = i + 1; j < total; j++)
7. {
8. if (a[i] > a[j])
9. {
10. temp = a[i];
11. a[i] = a[j];
12. a[j] = temp;
13. }
14. }
15. }
16. returna[total-3];
17. }
18. publicstaticvoid main(String args[]){
19. inta[]={1,2,5,6,3,2};
20. intb[]={44,66,99,77,33,22,55};
21. out.println(“Third Largest: “+getThirdLargest(a,6));
22. out.println(“Third Largest: “+getThirdLargest(b,7));
23. }}

Output:

Third Largest:3

Third Largest:66

### 6. Write a Java program to find out the number of characters , lines and words in a paragraph .

Java Program :

3. importio.IOException;
4. public class WordCountInFile
5. {
6. public static void main(String[] args)
7. {
• //Initializing charCount, wordCount and lineCount to 0
• int charCount = 0;
• int wordCount = 0;
• int lineCount = 0;
• try
• {
• //Creating BufferedReader object
• //Reading the first line into currentLine
• while (currentLine != null)
• {
• //Updating the lineCount
• lineCount++;
• //Getting number of words in currentLine
• String[] words = currentLine.split(” “);
• //Updating the wordCount
• wordCount = wordCount + words.length;
• //Iterating each word
• for (String word : words)
• {
• //Updating the charCount
• charCount = charCount + word.length();
• }
• //Reading next line into currentLine
• }
• //Printing charCount, wordCount and lineCount
• System.out.println(“Number Of Chars In A File : “+charCount);
• System.out.println(“Number Of Words In A File : “+wordCount);
• System.out.println(“Number Of Lines In A File : “+lineCount);
• }
• catch (IOException e)
• {
• printStackTrace();
• }
• finally
• {
• try
• {
• close(); //Closing the reader
• }
• catch (IOException e)
• {
• printStackTrace();
• }
• }
• }
• }

Output :

Number Of Chars In A File : 86
Number Of Words In A File : 14
Number Of Lines In A File : 4

### 7. Write a Java program to reverse a string in a paragraph.

 // Java program to Reverse String using Byte Array. import java.lang.*; import java.io.*; import java.util.*;   // Class of ReverseString class ReverseString {     public static void main(String[] args)     {         String input = “GeeksforGeeks”;           // getBytes() method to convert string          // into bytes[].         byte [] strAsByteArray = input.getBytes();           byte [] result =                     new byte [strAsByteArray.length];           // Store result in reverse order into the         // result byte[]         for (int i = 0; i
1. Output:
2. skeeGrofskeeG

### References :

https://www.geeksforgeeks.org/java-program-for-sum-the-digits-of-a-given-number/

https://www.javatpoint.com/prime-number-program-in-java

https://www.javatpoint.com/factorial-program-in-java

https://www.javatpoint.com/java-program-to-find-largest-number-in-an-array

https://www.javatpoint.com/java-program-to-find-third-largest-number-in-an-array

https://javaconceptoftheday.com/find-number-of-characters-words-and-lines-in-file-in-java/

https://www.geeksforgeeks.org/reverse-a-string-in-java/