Java programs

Write a Program in Java to input a number and check whether it is a Pronic Number or Heteromecic Number or not.
Pronic Number : A pronic number, oblong number, rectangular number or heteromecic number, is a number which is the product of two consecutive integers, that is, n (n + 1).
The first few pronic numbers are:
0, 2, 6, 12, 20, 30, 42, 56, 72, 90, 110, 132, 156, 182, 210, 240, 272, 306, 342, 380, 420, 462 … etc.o edit text

import java.util.*;
class PronicNumber
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
         
        System.out.print("Enter a number : ");
        int n = sc.nextInt();
        int k = (int)(Math.sqrt(n));
        if(k*(k+1)==n)
            System.out.println(n+" is a Pronic Number.");
        else
            System.out.println(n+" is not a Pronic Number.");     
    }
}

Write a program to input a word from the user and remove the duplicate characters present in it.
Example:
INPUT – abcabcabc
OUTPUT – abc
INPUT – javaforschool
OUTPUT – javforschl
INPUT – Mississippi
OUTPUT – Misp

import java.io.*;
class RemoveDupChar
{
    public static void main(String args[])throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter any word : ");
        String s = br.readLine();
        int l = s.length();
        char ch;
        String ans="";
         
        for(int i=0; i<l; i++)
        {
            ch = s.charAt(i);
            if(ch!=' ')
                ans = ans + ch;
            s = s.replace(ch,' '); //Replacing all occurrence of the current character by a space
        }
 
       System.out.println("Word after removing duplicate characters : " + ans);
    }
}

Write a program to input a word from the user and remove the consecutive repeated characters by replacing the sequence of repeated characters by its single occurrence.
Example:
INPUT – Jaaavvvvvvvvaaaaaaaaaaa
OUTPUT – Java
INPUT – Heeeiiiissggoiinggg
OUTPUT – Heisgoing

import java.io.*;
class RemoveRepChar
{
    public static void main(String args[])throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter any word: ");
        String s = br.readLine();
         s = s + " ";
        int l=s.length();
        String ans="";
        char ch1,ch2;
 
        for(int i=0; i<l-1; i++)
        {
            ch1=s.charAt(i);
            ch2=s.charAt(i+1);
             // Adding the first extracted character to the result if the current and the next characters are different
             if(ch1!=ch2)
            {
            ans = ans + ch1;
            }
        }
        ns=ns+s.charAt(l-1);
        System.out.println("Word after removing repeated characters = "+ans);
    }
}
Question
Write a program to enter an integer and check whether it is a Harshad Number or not. 
[Harshad number or Niven number is an integer which is divisible by sum of its digits. Example-18]


public class Harshad_no_459
{
    public static void main(int n)
    {
        int a=n,s=0;
        while(a>0)
        {
            int d=a%10;
            s=s+d;
            a=a/10;
        }
        if(n%s==0)
        {
            System.out.println("Harshad No.");
        }
        else
        {
            System.out.println("Not a Harshad No.");
        }
    }
}


Question
54321+4321+321+21+1

public class sum_series
{
    public static void main()
    {
        int n=54321,s=0;
        for(int x=10000;x>0;x=x/10)
        {
            s=s+n;
            n=n%x;
        }
        System.out.println("Sum="+s);
    }
}


1. Pell_series
Write a program to display first 20 numbers of the Pell series.
1,2, 5, 12, 29, 70, ……
[Pell series is such a series which starts from 1 and 2 and subsequent numbers are the sum of twice the previous number and the number previous to the previous number.
Ex. 5=2*2+1, 12=5*2+2 and so on]

package loop;
public class pell_series_429
{
    public static void main()
    {
        int a=1,b=2;
        System.out.println(a);
        System.out.println(b);
        for(int x=1;x<=18;x++)
        {
            int c=b*2+a;
            System.out.println(c);
            a=b;
            b=c;
        }
    }
}

2.  sum_series
9+99+8+89+7+79+……….+0+9

package loop;
public class sum_series_430
{
    public static void main()
    {
        int s=0;
        for(int x=9;x>=0;x--)
        {
            s=s+x+(x*10+9);
        }
        System.out.println(s);
    }
}

(Stewart)
    1. Write a program to accept a number and check whether it is palprime or not.

    public class PalPrime
    {
        public static void main(int n)
        {
            int a=n,r=0;
            while(a>0)
            {
                int d=a%10;
                r=r*10+d;
                a=a/10;
            }
            int c=0;
            for(int f=1;f<=n;f++)
            {
                if(n%f==0)
                {
                    c++;
                }
            }
            if(c==2 && r==n)
            {
                System.out.println("Palprime");
            }
            else
            {
                System.out.println("not Palprime");
            }
        }
    }

    /*2.     Write a program to input 2 integers n and p, find and display n raised to power p. Do not use Math.pow.*/

    package stewart;
    public class power_calc
    {
        public static void main(int n,int p)
        {
            int v=1;
            while(p>0)
            {
                v=v*n;
                p--;
            }
            System.out.println(v);
        }
    }


    /*3.     1+a/3+2+a/5+3+a/7…..upto n terms*/
    package stewart;
    public class sum_series1
    {
        public static void main(int n,int a)
        {
            double s=0.0;
            for(int x=1;x<=n;x++)
            {
                s=s+x+a/(x*2+1);
            }
            System.out.println(s);
        }
    }


    /*4.     Write a program to evaluate a function fx=(x2+1.5x+5)/(x-3) where x is ranging from -10 to +10.*/
    package stewart;
    public class equation
    {
        public static void main()
        {
            for(int x=-10;x<=10;x++)
            {
                double fx=(x*x+1.5*x+5)/(x-3);
                System.out.println(x+"  "+fx);
            }
        }
    }


    /*5.       1/2-2/4+3/6-4/8……upto n terms*/

    package stewart;
    public class sum_series
    {  
        public static void main(int n)
        {
            double s=0.0;
            for(int a=1;a<=n;a++)
            {
                if(a%2==0)
                {
                    s=s-(double)a/(a*2);
                }
                else
                {
                    s=s+(double)a/(a*2);
                }
            }
            System.out.println(s);
        }
    }


    /*6.     1*1/5*2/10*3/15*……*5/25  */

    package stewart;
    public class sum_series_6
    {
       public static void main()
       {
           double p=1.0;
           for(int a=1;a<=5;a++)
           {
               p=p*(double)a/(a*5);
            }
            System.out.println(p);
        }
    }


    /*7.       1/1-(1*2)/2+(1*2*3)/3-(1*2*3*4)/4……upto n terms*/
    package stewart;
    public class sum_series7
    {
        public static void main(int n)
        {
            double s=0.0;
            for(int x=1;x<=n;x++)
            {
                int p=1;
                for(int y=1;y<=x;y++)
                {
                    p=p*y;
                }
                if(x%2==0)
                {
                    s=s-(double)p/x;
                }
                else
                {
                    s=s+(double)p/x;
                }
            }
            System.out.println("Sum="+s);
        }
    }       

    /*8.    Write a menu driven program to find the sum of the following series depending on the user choosing 1 or 2
    (i) 1/4+1/8+1/12…….upto n terms
    (ii)    1/1!-2/2!+3/3!.........upto n terms */
    package stewart;
    public class menu_8
    {
        public static void main(int n,int ch)
        {
            switch(ch)
            {
                case 1:
                    double s=0.0;
                    for(int a=1,b=4;a<=n;a++,b=b+4)
                    {
                        s=s+1.0/b;
                    }
                    System.out.println("Sum="+s);
                    break;
               case 2:
                    double sum=0.0;
                    for(int a=1;a<=n;a++)
                    {
                        int k=a,f=1;
                        while(k>0)
                        {
                            f=f*k;
                            k--;
                        }
                        if(a%2==0)
                        {
                            sum=sum-(double)a/f;
                        }
                        else
                        {
                            sum=sum+(double)a/f;
                        }
                    }
                    System.out.println("Sum="+sum);
                    break;
               default:
                    System.out.println("Invalid entry");
            }
        }
    }

     /* 9. Define a class menu to perform the following operations depending upon the user';s choice:
              a) find out the square root of the number
              b) rounded down value of a number
              c) rounded up value of a number
    */
    package stewart;
    public class menu9
    {
       public static void main(double n,int ch)
       {
           switch(ch)
           {
               case 1:
                    double s=Math.sqrt(n);
                    System.out.println("Square Root="+s);
                    break;
              case 2:
                    double rd=Math.floor(n);
                    System.out.println("Rounded down value="+rd);
                    break;
              case 3:
                    double ru=Math.ceil(n);
                    System.out.println("Rounded up value="+ru);
                    break;
              default:
                System.out.println("Invalid entry");
           }
        }
    }

      

    /*10. In an election,out of 1249 voters in a booth,only 869 voters chose their
          franchises correctly.If 5 candidates are contesting,write a program to find:
                    i)  number of valid and invalid votes
                    ii)  percentage of valid votes received by each candidate
    */
    package stewart;
    public class vote_10
    {
        public static void main(int a,int b,int c,int d,int e)
        {
            int tv=1249,vv=869;
            int iv=tv-vv;
            double pa,pb,pc,pd,pe;
            pa=(double)a/vv*100;
            pb=(double)b/vv*100;
            pc=(double)c/vv*100;
            pd=(double)d/vv*100;
            pe=(double)e/vv*100;
            System.out.println(pa+" "+pb+" "+pc+" "+pd+" "+pe);
        }
    }

    /*11.  Write a program to add two times given in hours and minutes. */

    package stewart;
    public class time_11
    {
        public static void main(int h1,int m1,int h2,int m2)
        {
            int h=h1+h2;
            int m=m1+m2;
            if(m>60)
            {
                m=m-60;
                h=h+1;
            }
            System.out.println("Sum="+h+" hours "+m+" minutes");
        }
    }       
       /*12. Write a program to enter an integer and convert it from decimal to binary. */

    package stewart;
    public class decimal_binary_12
    {
        public static void main(int n)
        {
            String b="";
            int a=n;
            while(a>0)
            {
                int d=a%2;
                b=d+b;
                a=a/2;
            }
            System.out.println(b);
        }
    }


    /* 13.  octal to decimal */
    package stewart;
    public class octal_to_decimal_13
    {
        public static void main(int octal)
        {
             int a=octal,num=0,v=1;
             while(a>0)
             {
                 int d=a%10;
                 num=num+d*v;
                 v=v*8;
                 a=a/10;
             }
             System.out.println(num);
           
        }
    }
    /* 14.  binary to octal*/
    package stewart;
    public class binary_to_octal_14
    {
        public static void main(int n)
        {
           
             int a=n,num=0,v=1;
             while(a>0)//loop to convert binary to decimal
             {
                 int d=a%10;
                 num=num+d*v;
                 v=v*2;
                 a=a/10;
             }
            String r="";
            a=num;
            while(a>0)//loop to convert decimal to octal
            {
                int d=a%8;
                r=d+r;
                a=a/8;
            }
            //int r=Integer.parseInt(r);
            System.out.println(r);
           
        }
    }
        
    /*15.  octal to binary */
    package stewart;
    public class octal_to_binary_15
    {
        public static void main(int octal)
        {
             int a=octal,num=0,v=1;
             while(a>0)//loop to convert octal to decimal
             {
                 int d=a%10;
                 num=num+d*v;
                 v=v*8;
                 a=a/10;
             }
             String b="";
             a=num;
             while(a>0)//loop to convert decimal to binary
             {
                int d=a%2;
                b=d+b;
                a=a/2;
             }
             System.out.println(b);
        }
    }  

    /*16.    write a program to enter an integer and convert it into fraction by placing product of all odd digits in the denominator and product of all even digits in the numerator. */

    package stewart;
    public class integer_to_fraction_16
    {
        public static void main(int n)
        {
            int a=n,pe=1,po=1;
            while(a>0)
            {
                int d=a%10;
                if(d%2==0)
                {
                    pe=pe*d;
                }
                else
                {
                    po=po*d;
                }
                        
                a=a/10;
            }
            double num=(double)pe/po;
            System.out.println(num);
        }
    }

    /*17.  Write a program to enter  a number and check whether it is
     divisible by minimum by three prime numbers.*/
    package stewart;
    public class divisible_by_prime_17
    {
        public static void main(int n)
        {
            int ctr=0;
            for(int m=1;m<=n;m++)
            {
                int c=0;
                for(int f=1;f<=m;f++)
                {
                    if(m%f==0)
                    {
                        c++;
                    }
                }
                if(c==2)
                {
                    if(n%m==0)
                    {
                       ctr++;
                    }
                }
            }
            if(ctr>=3)
            {
                System.out.println("div. by min. 3 prime nos.");
            }
            else
            {
                System.out.println("not div. by min. 3 prime nos.");
            }
        }

1 comment:

  1. sir do the program of converting decimal to hexadecimal

    ReplyDelete

any problem in any program comment:-

Second largest number in java using ternary operator

 //Largest second number using ternary operator public class Main { public static void main(String[] args) { int a=5,b=6,c=7; int ...