Project Main
Project Main
INTRODUCTION TO JAVA
Page 1
Java is an object-oriented, class-based programming language. The
language is designed to have as few dependencies implementations as
possible. The intention of using this language is to give relief to the
developers from writing codes for every platform. The term WORA,
write once and run everywhere is often associated with this language. It
means whenever we compile a Java code, we get the byte code (.class
file), and that can be executed (without compiling it again) on different
platforms provided they support Java. In the year 1995, Java language
was developed. It is mainly used to develop web, desktop, and mobile
devices. The Java language is known for its robustness, security, and
simplicity features. That is designed to have as few implementation
dependencies as possible.
PROGRAMS
CLASS BASED:-
Page 2
1. Write a program in Java to print all Prime Palindrome
numbers between ‘m’ and ‘n’.
Sample Execution:
Enter the value of m: 10
Enter the value of n: 760
Prime Palindrome numbers between 10 and 100 are:
11, 101, 131, 151, 191, 313, 353, 373, 383, 727, 757
import java.util.*;
public class Prime_Palin
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int d,i,k,r,n,p,m,a,j;
System.out.println("Enter the value of m");
m=sc.nextInt();
System.out.println("Enter the value of n");
n=sc.nextInt();
System.out.println("Prime Palindrome numbers between "
+m+ " and " +n+ " are:");
for(i=m;i<=n;i++)
{
a=i;r=0;p=i;k=0;
do
{
d=a%10;
r=r*10+d;
a=a/10;
Page 3
}
while(a!=0);
if(r==p)
{
for(j=1;j<=p;j++)
{
if(p%j==0)
k=k+1;
}
if(k==2)
System.out.print(p + ",");
k=0;
}
}
}
OUTPUT OF THE PROGRAM:
Page 4
import java.util.*;
public class Automorphic
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int n,i=1,j=0,i2,d=0,p;
double a,s;
System.out.println("Enter the number of automorphic
numbers to be displayed: ");
n=sc.nextInt();
System.out.println("Automorphic numbers are: ");
while(j<n)
{
s=(int)(Math.pow(i,2));
i2=i;
d=0;
while(i2>0)
{
i2/=10;
d++;
}
a=(Math.pow(10,d));
p=(int)(s%a);
if(i==p)
{
System.out.println(i);
j++;
}
Page 5
i++;
}
}
}
OUTPUT OF THE PROGRAM:
Page 6
Example:
M (78) = -1: for 78 = 2*3*13 so, M(78) = (−1) = -1.3
prime number.
Write a program to input a positive natural number N and
output M(N). The program should continue till the user
wants.
import java.util.*;
public class Mobius
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int j,p,n,c;
c=0;
double m;
m=0.0;
System.out.println("Enter a number :");
n=sc.nextInt();
if(n==1)
System.exit(1);
j=2;p=0;
while(j<=n)
{
if(n%j==0)
Page 7
{
c++;
n=n/j;
if(p>1)
{
m=0;
break;
}
}
else
{
j++;
p=0;
}
}
if(p<=1)
m=(Math.pow(-1,c));
System.out.println("The output of function is " +(int)m);
}
}
Page 8
4. Write a program in java to enter any number from 2 to 5
(both inclusive) and print all the combinations of digits
starting from 1 to that number. There should be one blank
space between each digit and each new combination should
appear on a new line. Also display the total number of
combinations formed for the given input.
Sample input:
Enter any number from 2 to 5 (both inclusive): 3
Sample Output:
123, 132, 213, 231, 312, 321.
Total number of combinations: 6.
import java.util.*;
public class Num_Combi
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int n,c=0;
Page 9
char ch;
System.out.println("Enter a number between 2 to 5 (Both
inclusve) : ");
n=sc.nextInt();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==j)
continue;
for(int k=1;k<=n;k++)
{
if(i==j || i==k || j==k)
continue;
for(int l=1;l<=n;l++)
{
if(i==j || i==k || i==l || j==k || j==l || k==l)
continue;
for(int m=1;m<=n;m++)
{
if(i==j || i==k || i==l || i==m || j==k || j==l || j==m
|| k==l || k==m || l==m)
continue;
System.out.println(i+ " " +j+ " " +k+ " " +l+ " " +m);
c++;
}
if(n==4)
{
System.out.println(i+ " " +j+ " " +k+ " " +l);
c++;
Page
10
}
}
if(n==3)
{
System.out.println(i+ " " +j+ " " +k);
c++;
}
}
if(n==2)
{
System.out.println(i+ " " +j);
c++;
}
}
}
System.out.println("Total number of combinations: "+c);
}
}
OUTPUT OF THE PROGRAM:
Page
11
Page
12
Page
13