Vous êtes sur la page 1sur 10

2) CO-PRIME or NOT

import java.util.Scanner;
import java.lang.Math ;
public class co_prime
{
public static void main(String args[])
{
int n1,n2,flage=0,i,min;
Scanner in = new Scanner(System.in);
System.out.print("enter two numbers ");
n1 = in.nextInt();
n2 = in.nextInt();
min=Math.min(n1,n2);
if((n1-n2==2)||(n2-n1== 2))
{
for(i=2;i<=min;i++)
{
if( (n1%i==0) && (n2%i== 0))
{
flage = 1;
break;
}
}
}

if(flage == 1)
System.out.println("\n the numbers are not co prime numbers ");
else
System.out.println("\n the numbers are co prime numbers "):
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
6) Write a program to accept a no. & display the new number after removing all
zeros.
import java.util.Scanner;
public class remove_zero_from_number
{
public static void main(String args[])
{
int num ,rev_num=0,rem=0,i,n;
Scanner in = new Scanner(System.in);
System.out.println("\n Enter an Integer ");
num = in.nextInt();
System.out.println("You entered "+num);
n=num;
while(n>0)
{
rem= n%10;
if(rem != 0)
rev_num= rev_num*10+rem;
n=n/10;
}
System.out.println("The reverse of the number is after deleting zeros "+rev_num);
while(rev_num>0)
{
rem=rev_num%10;
n= n*10+rem;
rev_num=rev_num/10;
}
System.out.println("The number after deleting zeros is "+n);
}}
PRINT THE FOLLOWING PATTERN :
A

A a

A a a

A a a a

A a a a a
public class pyramid_pattern
{
public static void main(String args[])
{
int i,j,k,space=5;
for(i=1;i<=5;i++ )
{ for(j=1;j<=space-i;j++)
System.out.print(" ");
for(j=1;j<=i;j++)
System.out.print(" a");
System.out.println("");
}
}
}
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

1 2 3 4 5 5 4 3 2 1

1 2 3 4 4 3 2 1

1 2 3 3 2 1

1 2 2 1

1 1

@@@@@@@@@@@@@@@@@@@@@@@@@@@@
DUCK NUMBER
A Duck number is a number which has 0 in its digits.

import java.util.*;
public class DuckNumber
{
public static void main(String args[])
{
Scanner ob=new Scanner(System.in);
System.out.println("Enter the number to be checked.");
int num=ob.nextInt();

boolean duck=false;
int temp=num;
while(temp!=0)
{
int lastdigit=temp%10;
if(lastdigit==0)
{
duck=true;
break;
}
temp=temp/10;
}
if(duck==true)
{
System.out.println(num+" is a Duck Number.");
}
else
{
System.out.println(num+" is not a Duck Number.");
}
}
}
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

display the frequency of each digit present in the number.

import java.util.Scanner;

public class Q10_


{

public static void main(String args[])

Scanner in = new Scanner(System.in);

System.out.println(“Enter a number”);

long n=in.nextLong(), n1=n, f=0, d=0;

for(int i=0;i<=9;i++)

while(n!=0)

d=n%10;

if(d==i)

f++;

n=n/10;

n=n1;

if(f==0)

continue;

System.out.println(“Frequency of “+i+” = “+f);

f=0;

@@@@@@@@@@@@@@@@@@@@@@
Write a program to accept a number and check whether the number is present in the
Fibonacci series or not. The program displays the message accordingly.
import java.util.Scanner;

public class Q11

public static void main(String args[])

Scanner in = new Scanner(System.in);

System.out.println(“Enter a number”);

int a=0, b=1, c=a+b, n=in.nextInt();

while(c<n)

a=b;

b=c;

c=a+b;

if(c==n)

System.out.println(“Number is present in fibonacci series”);

break;

if(c>n)

System.out.println(“Number is not present in fibonacci series”);

Sample Input: 58

Sample Output: Number is not present in fibonacci series


A prime number is said to be ‘Twisted Prime’, if the new number obtained after
reversing the digits is also a prime number. Write a program to accept a number and
check whether the number is ‘Twisted Prime’ or not.

import java.util.Scanner;

public class Q12

public static void main(String args[])

Scanner in = new Scanner(System.in);

System.out.println(“Enter a number”);

int n=in.nextInt(), c=0, n1=0;

for(int i=1;i<=n;i++)

if(n%i==0)

c++;

if(c!=2)

System.out.println(“Not a twisted prime”);

else

while(n!=0)

n1=n1*10+(n%10);

n=n/10;

c=0;
for(int j=1;j<=n1;j++)

if(n1%j==0)

c++;

if(c==2)

System.out.println(“A twisted prime”);

else

System.out.println(“Not a twisted prime”);

Sample Input: 167

Sample Output: A twisted prime

A number is said to be Unique if digits of a number are not repeated in it. Write a
program to accept a number and check whether the number is Unique or not. The
program displays the message accordingly.

public class Q13

public static void main(String args[])

Scanner in = new Scanner(System.in);

System.out.println(“Enter a number”);

int n=in.nextInt(), f[]= new int[10], d, i;

while(n!=0)

d=n%10;
f[d]++;

n=n/10;

for(i=0; i<=9;i++)

if(f[i]>1)

break;

if(i==9)

break;

if(f[i]>1)

System.out.println(“Not a unique number”);

else

System.out.println(“A unique number”);

Sample Input: 547

Sample Output: A unique number

.Write a program to accept two numbers and check whether they are twin prime or
not.

import java.util.Scanner;

public class Q15

public static void main(String args[])

Scanner in = new Scanner(System.in);

System.out.println(“Enter two numbers”);


int a=in.nextInt(), b=in.nextInt(), c=0, d=0;

if((a-b)!=2 && (a-b)!=(-2))

System.out.println(“Numbers are not twin primes”);

else

for(int i=1; i<=a; i++)

if(a%i==0)

c++;

for(int j=1; j<=b; j++)

if(b%j==0)

d++;

if(c==2 && d==2)

System.out.println(“Numbers are twin prime”);

else

System.out.println(“Numbers are not twin prime”);

Sample Input: 12, 16

Sample Output: Numbers are not twin primes

Vous aimerez peut-être aussi