0% found this document useful (0 votes)
21 views

Finalpaperspastpf

Uploaded by

basit.ul.ibad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Finalpaperspastpf

Uploaded by

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

Perfect num

package pastpapers;

import java.util.Scanner;

public class Pastpapers {

public static void main(String[] args) {

Scanner input=new Scanner(System.in);

System.out.println("enter a num ");

int num=input.nextInt();

int count=0;

for(int i=1;i<num;i++){

if(num%i==0){

count=count+i;

if(count==num){

System.out.println("perfect num ");

}else{

System.out.println("no perfect num");

}
Qno 2 find number is substring or not
import java.util.Scanner;

public class SubstringCheck {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// Ask the user to enter the first string

System.out.print("Enter the first string: ");

String firstString = scanner.nextLine();

// Ask the user to enter the second string

System.out.print("Enter the second string: ");

String secondString = scanner.nextLine();

// Check if the second string is a substring of the first string using contains method

boolean isSubstring = firstString.contains(secondString);

// Print the result

if (isSubstring) {

System.out.println("The second string is a substring of the first string.");

} else {

System.out.println("The second string is not a substring of the first string.");

}
// Close the scanner

scanner.close();

Palindrome
package pastpapers;

import java.util.Scanner;

public class Pastpapers {

public static void main(String[] args) {

Scanner input=new Scanner(System.in);

System.out.println("enter a word");

String character=input.next();

boolean palindrom=true;

for(int i=0;i<=character.length()/2;i++){

if( character.charAt(i)!= character.charAt( character.length()-1-i)){

palindrom=false;

break;

if(palindrom){

System.out.println("yes palindrome ");

else{

System.out.println("bye wth it is not palindrome");

}
import java.util.Scanner;

public class Pastpapers {

public static void main(String[] args) {

Scanner input=new Scanner(System.in);

int largest=0;

int seclargest=0;

int thirdlar=0;

while(true){

System.out.println("enter 0 to leave");

System.out.println("enter a word");

int word=input.nextInt();

if(word==0){

break;

else if(word>largest){
thirdlar=seclargest;

seclargest=largest;

largest=word;

else if(word<largest && word>thirdlar){

thirdlar=seclargest;

seclargest=word;

else if(word<largest && word<seclargest){

thirdlar=word;

System.out.println("largest "+largest+"sec "+seclargest+" third "+thirdlar);

}
Password

package pastpapers;

import java.util.Scanner;

public class Pastpapers {

public static void main(String[] args) {

Scanner scanner=new Scanner(System.in);

while (true) {

System.out.println("Enter a password:");

String password = scanner.nextLine();

if (isValidPassword(password)) {

System.out.println("Valid password!");

break; // Exit the loop if the password is valid

} else {

System.out.println("Invalid password. Please try again.");

scanner.close();

private static boolean isValidPassword(String password) {

// Check if the password length is at least 8 characters

if (password.length() < 8) {

return false;
}

boolean hasUppercase = false;

boolean hasDigit = false;

// Check each character in the password

for (char c : password.toCharArray()) {

// Check if the character is an uppercase letter

if (Character.isUpperCase(c)) {

hasUppercase = true;

// Check if the character is a digit

if (Character.isDigit(c)) {

hasDigit = true;

// Return true only if the password has at least one uppercase letter and one digit

return hasUppercase && hasDigit;

}
package pastpapers;

import java.util.Scanner;

public class Pastpapers {

public static void main(String[] args) {

Scanner scanner=new Scanner(System.in);

// Ask user for input values

System.out.print("Enter the value of x: ");

int x = scanner.nextInt();

System.out.print("Enter the value of N: ");

int N = scanner.nextInt();
// Verify input values are positive

if (x <= 0 || N <= 0) {

System.out.println("Both x and N must be positive.");

return;

// Call exponential calculation function

double result = exponentialCalculation(x, N);

System.out.println("Result of exponential function: " + result);

// Approximate square root of the result

double squareRootResult = squareRoot(result);

System.out.println("Approximate square root: " + squareRootResult);

scanner.close();

public static double exponentialCalculation(int x, int N) {

double sum = 0;

for (int i = 0; i <= N; i++) {

sum += Math.pow(x, i) / factorial(i);

return sum;

public static int factorial(int n) {

int result = 1;

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

result *= i;
}

return result;

public static double squareRoot(double n) {

double lastGuess = 1.0;

double nextGuess = (lastGuess + n / lastGuess) / 2.0;

while (Math.abs(nextGuess - lastGuess) >= 0.0001) {

lastGuess = nextGuess;

nextGuess = (lastGuess + n / lastGuess) / 2.0;

return nextGuess;

}
package pastpapers;

import java.util.Scanner;

public class Pastpapers {

public static void main(String[] args) {

Scanner scanner=new Scanner(System.in);

// Constants for the number of cookies per box and boxes per container

final int COOKIES_PER_BOX = 24;

final int BOXES_PER_CONTAINER = 75;

// Prompt user to enter the total number of cookies

System.out.print("Enter the total number of cookies: ");

int totalCookies = scanner.nextInt();

// Calculate the number of full boxes and leftover cookies

int fullBoxes = totalCookies / COOKIES_PER_BOX;

int leftoverCookies = totalCookies % COOKIES_PER_BOX;

// Calculate the number of full containers and leftover boxes


int fullContainers = fullBoxes / BOXES_PER_CONTAINER;

int leftoverBoxes = fullBoxes % BOXES_PER_CONTAINER;

// Output the results

System.out.println("Total cookies: " + totalCookies);

System.out.println("Full boxes: " + fullBoxes);

System.out.println("Leftover cookies: " + leftoverCookies);

System.out.println("Full containers: " + fullContainers);

System.out.println("Leftover boxes: " + leftoverBoxes);

scanner.close();

}
package pastpapers;

import java.io.*;

import java.util.*;

public class Pastpapers {

public static void main(String[] args) {

for(int i=3;i<=1000-2;i++){

boolean isprime=false;

if((isprime(i)) && (isprime(i+2))){

System.out.println("twin prime are "+i+ " " +(i+2));

public static boolean isprime(int x){

for(int i=2;i<x/2;i++){

if(x%i==0){

return false;

return true;

}
package pastpapers;

//import java.io.*;

import java.util.*;

public class Pastpapers {

public static void main(String[] args) {

Scanner input=new Scanner(System.in);

System.out.println("enter 1 to find area of rectangle ");

System.out.println("enter 2 to find the area of circle ");

int choice=input.nextInt();

switch(choice){

case 1:

System.out.println("enter the length of rectangle");

int length=input.nextInt();

System.out.println("enter width");

int width=input.nextInt();

int area=findarea(length,width);

System.out.println("area of rectangle is "+area);

break;

case 2:

System.out.println("enter the radius of circle");

double radius=input.nextDouble();

double circlearea=findarea(radius);
System.out.println("area of circle is "+circlearea);

break;

public static int findarea(int x,int l){

return x*l;

public static double findarea(double k){

double result=3.14*k*k;

return result;

}
package pastpapers;

//import java.io.*;

import java.util.*;

public class Pastpapers {

public static void main(String[] args) {

Scanner input=new Scanner(System.in);

double sum=0.0;

for(int i=1;i<=624;i++){

sum= sum+1/(Math.sqrt(i)+Math.sqrt(i+1));

System.out.println("The summation is "+sum);

}
package pastpapers;

import java.io.*;

import java.util.*;

import java.util.Random;

public class Pastpapers {

public static void main(String[] args) {

Scanner input=new Scanner(System.in);

Random random = new Random();

try {

BufferedWriter writer = new BufferedWriter(new FileWriter("f://its.txt"));

System.out.println("Enter how many random numbers you want:");

int x = input.nextInt();

int[] array = new int[x];

for (int i = 0; i < x; i++) {

array[i] = random.nextInt(100);

writer.write(Integer.toString(array[i]) + " "); // Adding a space between numbers

}
writer.close();

System.out.println("Random numbers have been written to the file.");

} catch (IOException e) {

e.printStackTrace();

} finally {

input.close(); // Close the scanner to avoid resource leak

/////

package past;

import java.io.*;

import java.util.*;

public class Past {

public static void main(String[] args) {

try {

BufferedReader reader = new BufferedReader(new FileReader("f://its.txt"));

String line;

int count=0;

int maximum=0;

int secondma=0;

while((line=reader.readLine())!=null){

System.out.println("Read line: " + line);

String[]parts=line.split(" ");
count=count+parts.length;

for(int i=0;i<count;i++){

int num=Integer.parseInt(parts[i]);

if(num>maximum){

secondma=maximum;

maximum=num;

} else if (num > secondma && num < maximum) {

secondma = num;

System.out.println("Count: " + count);

System.out.println("Maximum: " + maximum);

System.out.println("Second Maximum: " + secondma);

reader.close();

} catch (IOException e) {

e.printStackTrace();

} finally {

// Close the BufferedReader to avoid resource leak

}
package stringvowel;

import java.util.Scanner;

public class Stringvowel {

public static void main(String[] args) {

Scanner input=new Scanner(System.in);

System.out.println("enter a sentence");

String sentence=input.nextLine();

int length=sentence.length();

System.out.println(length);

int vowels=vowels(sentence);

System.out.println("the vowels it contain is "+vowels);

int consonant=consonant(sentence);

System.out.println("the number of consonant is "+consonant);

public static int vowels(String sentence){

int count=0;

for(int i=0;i<sentence.length();i++){

if(sentence.charAt(i)==' '){

continue;

}
else if(sentence.charAt(i)=='a'|| sentence.charAt(i)=='e' || sentence.charAt(i)=='i'||
sentence.charAt(i)=='o' || sentence.charAt(i)=='u' ){

count++;

return count;

public static int consonant(String sentence){

int count=0;

for(int i=0;i<sentence.length();i++){

if((sentence.charAt(i)!='a'|| sentence.charAt(i)!='e' || sentence.charAt(i)!='i'||sentence.charAt(i)!


='o' || sentence.charAt(i)!='u' ) && sentence.charAt(i)==' '){

count++;

return count;

}
package pkg2darrays;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner input=new Scanner(System.in);

System.out.println("total students are 5 ");

System.out.println("and total subject are five ");

int[][]array=new int[5][5];

for(int i=0;i<5;i++){

System.out.println("enter marks of suject in such a way


MATH,ENGLISH,CHEMISTRY,COMPUTERSCIENCE,PHYSICS");

for(int j=0;j<5;j++){

array[i][j]=input.nextInt();

System.out.println("result summary");

studentresult(array);

subjectstac(array);

printResult(array);

public static void studentresult( int array1[][]){


for(int i=0;i<5;i++){

int sum=0;

for(int j=0;j<5;j++){

sum=sum+array1[i][j];

System.out.println("sum of student in all subject is " +(i+1)+" is "+sum);

public static void subjectstac(int array2[][]){

System.out.println("Marks Obtained by All Students in Each Subject:");

for (int i = 0; i < 5; i++) {

System.out.print("Subject " + (i + 1) + ": ");

for (int j = 0; j < 5; j++) {

System.out.print(array2[j][i] + " ");

System.out.println();

public static void printResult(int[][] marks) {

System.out.println("Printing Result Summary:");

for (int i = 0; i < 5; i++) {

System.out.print("Student " + (i + 1) + ": ");

for (int j = 0; j < 5; j++) {

System.out.print(marks[i][j] + " ");

System.out.println();

}
package javaapplication308;

import java.util.Scanner;

import java.util.Random;

public class JavaApplication308 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

Random random = new Random();

System.out.println("enter the number of rows");

int rows=input.nextInt();

int[][]raggedArray=new int[rows][];

for(int i=0;i<rows;i++){

int cols=random.nextInt(5)+1;

raggedArray[i]=new int[cols];

for(int j=0;j<cols;j++){

raggedArray[i][j]=random.nextInt(100);

// Print ragged array

System.out.println("Ragged Array:");

for (int i = 0; i < rows; i++) {

for (int j = 0; j < raggedArray[i].length; j++) {

System.out.print(raggedArray[i][j] + " ");

}
System.out.println();

// Get number to search from user

System.out.print("Enter a number to search: ");

int target = input.nextInt();

// Search for number in the array

boolean found = false;

for (int i = 0; i < rows; i++) {

for (int j = 0; j < raggedArray[i].length; j++) {

if (raggedArray[i][j] == target) {

System.out.printf("Found %d at position [%d][%d]\n", target, i, j);

found = true;

if (!found) {

System.out.println("Number not found in the array.");

input.close();

}
;ll

package arraylistmerge;

import java.util.Scanner;

public class Arraylistmerge {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int[] list1 = {11,49,83,7,61,35,44,3,12,19};

int[] list2 = {31,19,18,32,45,31,56,43,56,43};

int[] list3 = findlist(list1, list2);

System.out.println("list 3 is");

for(int i=0; i<list3.length; i++){

System.out.print(list3[i] + " ");

public static int[] findlist(int list1[],int list2[]){

int []list3=new int[10];

for(int i=0;i<10;i++){

if(i%2==0){

list3[i] = list2[i];

} else {
list3[i] = list1[i];

return list3;

b) Write JAVA programs that calculate the distance


between two points in 2D and 3D space. In

order to calculate distance in 2D space you should


define a method calculateDistance() which Take
the coordinates (x1, yl) and (x2, y2), and to
calculate distance in 3D space, you should
overload the above method and it should take the
coordinates (x1, y1, z1) and (x2, y2, 22).

Formula for calculating distance in 2D space: d =


√(x2-x)2 + (y2- Y1)2

Formula for calculating distance in 3D space: d =


√(x2-x1)2+(y2- Y1)2 + (Z2-21)2

package n;

import java.util.Scanner;

public class N {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.println("Enter 1 to calculate 2D space");

System.out.println("Enter 2 to calculate 3D space");

System.out.println("Enter 0 to exit");
while (true) {

System.out.println("Enter your choice:");

int choice = input.nextInt();

input.nextLine();

if (choice == 0) {

System.out.println("Exiting...");

break;

} else {

switch (choice) {

case 1:

System.out.println("Enter x1:");

double x1 = input.nextDouble();

input.nextLine();

System.out.println("Enter y1:");

double y1 = input.nextDouble();

input.nextLine();

System.out.println("Enter x2:");

double x2 = input.nextDouble();

input.nextLine();

System.out.println("Enter y2:");

double y2 = input.nextDouble();

input.nextLine();

double result2D = calculateDistance2D(x1, y1, x2, y2);

System.out.println("Distance in 2D space: " + result2D);

break;

case 2:

System.out.println("Enter x1:");

double x1d = input.nextDouble();

input.nextLine();
System.out.println("Enter y1:");

double y1d = input.nextDouble();

input.nextLine();

System.out.println("Enter z1:");

double z1d = input.nextDouble();

input.nextLine();

System.out.println("Enter x2:");

double x2d = input.nextDouble();

input.nextLine();

System.out.println("Enter y2:");

double y2d = input.nextDouble();

input.nextLine();

System.out.println("Enter z2:");

double z2d = input.nextDouble();

input.nextLine();

double result3D = calculateDistance3D(x1d, y1d, z1d, x2d, y2d, z2d);

System.out.println("Distance in 3D space: " + result3D);

break;

default:

System.out.println("Invalid choice. Please try again.");

break;

input.close();

public static double calculateDistance2D(double x1, double y1, double x2, double y2) {

double result=Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));


return result;

public static double calculateDistance3D(double x1, double y1, double z1, double x2, double y2,
double z2) {

double result2= Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2) + Math.pow(z2 - z1, 2));

return result2;

You might also like