0% found this document useful (0 votes)
60 views11 pages

Pratin

The document contains 10 Java programs that perform various numeric and prime number calculations and manipulations. Each program contains comments describing the expected output. The programs include loops, conditionals, methods to check for prime numbers, sorting digits of a number, and calculating series based on user input.

Uploaded by

Prashant Choudri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views11 pages

Pratin

The document contains 10 Java programs that perform various numeric and prime number calculations and manipulations. Each program contains comments describing the expected output. The programs include loops, conditionals, methods to check for prime numbers, sorting digits of a number, and calculating series based on user input.

Uploaded by

Prashant Choudri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 11

1)////////////////////////////////////////////////////////////////////

import java.util.Scanner;
class Pratin1
{
public static void main(String[] args)
{

//2 3 5 11 23 29 41 53 83 89

//(2*x)+1=prime number print x x is also prime


Scanner s=new Scanner(System.in);
System.out.println("enter the nth value");
int n=s.nextInt();
int count=0;
for(int i=2;count<n;i++)
{
if(is_prime(i)==true)
{
if((is_prime((2*i)+1))==true)
{
System.out.println(i);
++count;
}
}
}
}

public static boolean is_prime(int num)


{ int c=0;

for(int i=1;i<=num;i++)
{
if(num%i==0)
{
++c;
}
}
if(c==2)
{
return true;
}
else
{
return false;
}
}

2)/////////////////////////////////////////////////////////////////

import java.util.Scanner;
class Pratin2
{
public static void main(String[] args)
{
//1 -2 6 -15 31 -56 92 -141 205 -286

Scanner s=new Scanner(System.in);


System.out.println("entr the n value");

int n=s.nextInt();
int k=1;
for(int i=0;i<n;i++)
{
k=k+(i*i);
if(i%2==0)
{
System.out.print(k+" ");
}
else
{
System.out.print("-"+k+" ");

}
}
}
}

3)/////////////////////////////////////////////////////////////////////////////

import java.util.Scanner;
class Pratin3
{
public static void main(String[] args)
{
//1 1 2 12 14 26 40 264 304 568
Scanner s=new Scanner(System.in);
System.out.println("enter the n");
int n=s.nextInt();
int a=1;
int b=1;
int c=1;
for(int i=0;i<n;i++)
{
if(i<2)
{
System.out.print(a+" ");
}
else
{
c=a+b;

if((i+1)%4==0)
{
c=c*4;
}
System.out.print(" "+c+" ");
a=b;
b=c;
}
}

}
}

4)/////////////////////////////////////////////////////////////

import java.util.Scanner;
class Pratin5
{
public static void main(String[] args)
{
//4 6 12 18 30 42 60 72
System.out.println("Enter limit:");
Scanner inp=new Scanner(System.in);
int n=inp.nextInt();
int count;
for(int i=3;i!=n;i++)
{
count=0;

for(int j=2;j<=(i-1)/2;j++)
{
if((i-1)%j==0)
{
count++;
break;
}
}
for(int j=2;j<=(i+1)/2;j++)
{
if((i+1)%j==0)
{
count++;
break;
}
}
if(count==0)
{

System.out.println(i);
}
}

}
}

5)////////////////////////////////////////////////////////////////

import java.util.Scanner;
class Pratin6
{
public static void main(String[] args)
{
//11 41 102 196 325
Scanner s=new Scanner(System.in);
System.out.println("enter the num");
int n=s.nextInt();
int ar[]=new int[n];
int k=11;
int j=1;

for(int i=0;i<n;i++)
{
System.out.println(k);
k=(k+30*j)+i*i;
j=j+1;
}

}
}

6)////////////////////////////////////////////////////////////////

import java.util.Scanner;
class Pratin4
{
public static void main(String[] args)
{
//2 3 5 7
Scanner s=new Scanner(System.in);
System.out.println("enter the n");
int n=s.nextInt();
for(int i=2;i<=n;i++)
{
if(is_prime(i)==true)
{
System.out.println(i);
}
}

}
static boolean is_prime(int num)
{
for(int i=2;i<=num/2;i++)
{
if(num%i==0)
{
return false;
}
}
return true;
}
}
7)////////////////////////////////////////////////////////////////////////

import java.util.Scanner;
class Pratin7
{
public static void main(String[] args)
{

//1 3 7 13 21 43 57 73 91 111
int i=2;
int j=1;
Scanner s=new Scanner(System.in);
System.out.println("enter the range");
int n=s.nextInt();
for(int l=1;l<=n;l++)
{
System.out.println(j);
if(l>=5&&l%5==0)
{
j=j+i;
i=i+2;
}
j=j+i;
i=i+2;
}
}
}

8)///////////////////////////////////////////////////////////

class Pratin8
{
public static void main(String[] args)
{

// sWrite a program to display the 1st,2nd and


// 4th multiples of 7 which when divided by 2,3,4,5 &6 gives the
remainder as 1.

int a = 7;
int count = 0;
int cal = 0;

while(true) {
cal = cal + a;
if(cal%2==1 && cal%3==1 && cal%4==1 && cal%5==1 && cal%6==1) {
count++;
if(count==1 || count==2 || count==4||count==5)
System.out.print(cal + " ");
}
}

}
}

9)/////////////////////////////////////////////////////////////////////

import java.lang.Math;
import java.util.Scanner;
class Pratin9
{
public static void main(String[] args)
{
//Write the program to generate the following series
//where N which is upper limit should be taken as an input.
//Also, display the count of the numbers which have the digit '9' in
them.
//For example, if the value of N is 2800 the output should be as
follows

//1 4 9 49 121 169 361 529 841 1369 1681 1849

//Count of the numbers which have the digit 9 in them : 6


Scanner s=new Scanner(System.in);
System.out.println("enter the num");
int n=s.nextInt();
System.out.println("enter the occur");
int d=s.nextInt();
int count =1;
int occur=0;
int number=0;
int rem=0;
System.out.println("1");
for(int i=2;(int)Math.pow(i,2)<n;i++)
{
if(is_prime(i)==true)
{
if(count<3)
{ count++;
number=(int)Math.pow(i,2);
System.out.println(number);
while(number>0)
{
if(number%10==d)
{
++occur;

}
number=number/10;
}

}
else
{
count=0;
continue;
}
}
}
System.out.println(occur);
}
public static boolean is_prime(int num)
{
for(int i=2;i<=num/2;i++)
{
if(num%i==0)
{
return false;
}
}
return true;
}

10)//////////////////////////////////////////////////////////////////////////

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
class Pratin10
{
public static void main(String[] args)
{
//Write a program to rearrange a 4 digit number in ascending
//order of digits. Eg: 3921 to 1239
Scanner s=new Scanner(System.in);
System.out.println("enter the num");
long num=s.nextLong();

System.out.println(smallest(num));

static long smallest(long num)


{
// initialize frequency of each digit to Zero
int[] freq = new int[10];

// count frequency of each digit in the number


while (num > 0)
{
long d = num % 10; // extract last digit
freq[(int)d]++; // increment counting
num = num / 10; //remove last digit
}
for(int i=0;i<freq.length;i++)
{
System.out.println(freq[i]);
}

// Set the LEFTMOST digit to minimum expect 0


long result = 0;
for (int i = 1 ; i <= 9 ; i++)
{
if (freq[i] != 0)
{
result = i;
System.out.println("result"+result);
System.out.println(" freq i"+freq[i]);
freq[i]--;
System.out.println(" freq i"+freq[i]);
break;
}
}

// arrange all remaining digits


// in ascending order
for (int i = 0 ; i <= 9 ; i++)
while (freq[i]-- != 0)
result = result * 10 + i;

return result;
}
}

11)//////////////////////////////////////////////////////////////////

import java.util.Scanner;
class Pratin11
{
public static void main(String[] args)
{ /// 6,15,35,77,143,221,323,437
//2*3 3*5 5*7 7*11 ......
Scanner s=new Scanner(System.in);
System.out.println("enter the num");
int n=s.nextInt();
int c=0;
int j=0;
int ar[]=new int[n];

for(int i=2;c<n;i++)
{
if(is_prime(i)==true)
{
ar[j++]=i;
++c;
}
}
for(int i=0;i<ar.length;i++)
{
System.out.println(ar[i]);
}

for(int i=0;i<ar.length-1;i++)
{
System.out.println(ar[i]*ar[i+1]);
}

}
public static boolean is_prime(int n)
{
for(int i=2;i<=n/2;i++)
{
if(n%i==0)
{
return false;
}
}
return true;
}

12)////////////////////////////////////////////////

import java.util.Scanner;
import java.lang.Math;
class Pratin12
{
public static void main(String[] args)
{

//3, 11, 31, 69, 131

//1^3+2=3.
//2^3+3=11.
//3^3 +4=31.
//4^3+5=69.
//5^3+6=13
Scanner s=new Scanner(System.in);
System.out.println("enter the number");
int n=s.nextInt();
int j=2;
for(int i=1;i<=n;i++)
{
System.out.println(((int)Math.pow(i,3))+j);

j++;
}

}
}

13)//////////////////////////////////////////////////////
import java.util.Scanner;
import java.lang.Math;
class Pratin13
{
public static void main(String[] args)
{

////23, 21, 30, 25, 74, 63?

//23–2=21
//21+3^2=30
//30–5=25
//25+7^2=74
//74–11=63
//63+13^2=232..
Scanner s=new Scanner(System.in);
System.out.println("enter the number");
int n=s.nextInt();
int sum=23;
int c=0;
int j=1;
System.out.println(sum);
for(int i=2;c<n-1;i++)
{
if(is_prime(i)==true&&j%2==1)
{
sum=sum-i;
System.out.println(sum);
j=j+1;
++c;
}
else if(is_prime(i)==true&&j%2==0)
{
sum=sum+(int)Math.pow(i,2);
System.out.println(sum);
j=j+1;
++c;
}
}
}
static boolean is_prime(int n)
{
for(int i=2;i<=n/2;i++)
{
if(n%i==0)
{
return false;
}
}
return true;
}
}

14)//////////////////////////////////////////////////////////

import java.util.Scanner;
class Pratin
{
public static void main(String[] args)
{
////4 6 12 18 30 42

//3_5 5_7 11_13 3 and 5 alternate prime number


Scanner s=new Scanner(System.in);
System.out.println("enter the num");
int n=s.nextInt();
int c=0;
for(int i=2;c<n;i++)
{
if(is_prime(i)==true&&is_prime(i+1)==false&&is_prime(i+2)==true)
{
System.out.println(i+1);
c++;
}
}

}
static boolean is_prime(int n)
{
for(int i=2;i<=n/2;i++)
{
if(n%i==0)
{
return false;
}
}
return true;
}
}

15)///////////////////////////////////////////

What comes next in 6, 14, 36, 98, 276?

6×3−4=14

14×3−6=36

36×3−10=98

98×3−18=276

16)////////////////////////////////////////////////////

//8 6 9 23 27 429

//(8*1)-2=6,
//(6*2)-3=9,
//(9*3)-4=23,
//(23*4)-5=87,
//(87*5)-6=429,

You might also like