×
FREE ASSISTANCE FOR THE INQUISITIVE PEOPLE
Tutorial Topics
X
softetechnologies
Accept a name and print only the initials with surname. Write a program in java to display the following pattern.
Write a programme in java to accept a number and print the sum of its prime digits. - Java
14661    Arnab De    11/06/2018

Write a programme in java to accept a number and print the sum of its prime digits.

First of all, create a method called IsPrime() to check a number is prime or not. Now in main method we accept a number and cut the digit from last and check it is prime or not using IsPrime() method. As IsPrime() is a non-static method, we have to create a object of the said class. because we can not call any non-static method from static method.

softetechnologies
import java.util.*;
public class PrimeDigit
{
    public boolean IsPrime(int i)
    {
        boolean b=true;
        int d=2;
        while(d<i/2)
        {
            if (i%d==0)
            {
                b=false;
                break;
            }
            d++;
        }
        return b;
    }
    public static void main(String []args)
    {
       Scanner sc=new Scanner(System.in);
       System.out.print("Enter A Number : ");
       int i=sc.nextInt();
       int s=0,r;
       PrimeDigit pd=new PrimeDigit();
       while(i>0)
       {
            r=i%10;
            if(pd.IsPrime(r))
			{
                s = s + r;
			}
            i = i / 10;
       }
       System.out.print("Sum Of The Prime Digits : " + s );
    }
}
softetechnologies

Output

Enter A Number : 7865
Sum Of The Prime Digits : 12

Accept a name and print only the initials with surname. Write a program in java to display the following pattern.
softetechnologies
Author Details
Arnab De
I have over 16 years of experience working as an IT professional, ranging from teaching at my own institute to being a computer faculty at different leading institute across Kolkata. I also work as a web developer and designer, having worked for renowned companies and brand. Through tutorialathome, I wish to share my years of knowledge with the readers.
Enter New Comment
Comment History
No Comment Found Yet.
Swami Vivekananda
A nation is advanced in proportion to education and intelligence spread among the masses.
Swami Vivekananda
2087
63.45
Today So Far
Total View (Lakh)
softetechnologies
26/05/2018     46744
25/06/2018     38887
01/01/2018     38546
28/06/2017     36472
02/08/2017     35190
01/08/2017     29444
06/07/2017     29200
15/05/2017     28721
14/07/2017     24570
11/09/2018     23623
softetechnologies