PM Project
PM Project
Class: X
Section: B
Roll No.: 34
I respect and thank Mr. DN Rai Sir, for providing me an opportunity to do this project and
giving us all support and guidance which made me complete the project duly. I am
extremely thankful to him providing such a nice support and guidance even after having
his busy schedule managing the corporate affairs.
I owe my deep gratitude to my project guide i.e. none other than my friends, who took
keen interest in my project work and guided me all along, till the completion of my project
work by providing all the necessary information by developing a good system.
I would not forget to remember my parents and siblings for their encouragement and
more over for their timely support and guidance till the completion of this project.
At last, I would also like to thank the examiner for taking pains to go through this project
and I would also welcome suggestions for improvement.
- Pradyumn
Mishra
INDEX
S.No. Categor Name of Program Page
y No.(s
)
* Arrays
1 Write a program to input 10 numbers
in an array and find the sum of all even
numbers.
2 Write a program to sort the elements
in an array using selection sort
technique.
* String
11 Write a program to find the largest
word.
12 Write a program to find the smallest
word.
13 Write a program to input any string
and display the name in given form.
14 Write a program to encode a word into
pig Latin.
15 Write a program to display each word
of string in reverse order.
* Primary
Programs
16
17
18
19
20
COMPUTER PROGRAMS
Arrays
QUESTION
Write a program to input 15 numbers in an Array and find
the average of the odd numbers in the inputted array.
SOLUTION
import java.util.*;
class sum
{
public static void main()
{
int a[] = new int[10];
int i, s=0;
Scanner sc = new Scanner(System.in);
System.out.println(“Enter 10 Numbers”);
for( i=0 ; i<=9 ; i++ )
{
a[i] = sc.nextInt(); }
for( i=0 ; i<=9 ; i++ )
{
if( a[i] % 2 == 0 )
s = s + a[i];
}
System.out.println( “Sum of all the even numbers is” + s );
}
}
s int Variable for storing the sum of all even numbers from the
inputted Array.
a[] int Array for storing values.
QUESTION
Write a program to sort the elements in an array using selection sort technique.
SOLUTION
import java.util.Scanner;
public class SelectionSort {
public static void main(String[] args) {
int[] arr = new int[10];
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < arr.length; i++) {
arr[i] = scanner.nextInt();
}
for (int i = 0; i < arr.length - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
scanner.close();
}
}
SOLUTION
import java.util.Scanner;
public class BubbleSort {
public static void main(String[] args) {
int[] arr = new int[10];
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < arr.length; i++) {
arr[i] = scanner.nextInt();
}
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
for (int i = 0; i < arr.length; i++) {
SOLUTION
import java.util.Scanner;
public class BubbleSort {
public static void main(String[] args) {
int[] arr = new int[10];
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < arr.length; i++) {
arr[i] = scanner.nextInt();
}
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
scanner.close();
}
}
SOLUTION
import java.util.Arrays;
import java.util.Scanner;
public class BinarySearch {
public static void main(String[] args) {
int[] arr = new int[10];
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < arr.length; i++) {
arr[i] = scanner.nextInt();
}
Arrays.sort(arr);
System.out.print("Enter the number to search: ");
int key = scanner.nextInt();
int low = 0;
int high = arr.length - 1;
int mid;
boolean found = false;
while (low <= high) ( mid = (low + high) / 2;
if (arr[mid] == key) {
System.out.println("Number found at index: " + mid);
found = true;
break;
} else if (arr[mid] < key) {
low = mid + 1;
} else {
high = mid - 1;
}
}
if (!found) {
System.out.println("Number not found in the array.");
}
scanner.close();
}
}
SOLUTION
import java.util.Scanner;
public class LinearSearch {
public static void main(String[] args) {
int[] arr = new int[10];
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < arr.length; i++) {
arr[i] = scanner.nextInt();
}
System.out.print("Enter the number to search: ");
int key = scanner.nextInt();
boolean found = false;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == key) {
System.out.println("Number found at index: " + i);
found = true;
break;
}
}
if (!found) {
System.out.println("Number not found in the array.");
}
scanner.close();
}
}
SOLUTION
import java.util.*;
class fun_return02
{
boolean Check(String s,char c) //function prototype
{
if(s.charAt(0)==c)
return true;
else
return false;
}
void mymain(String str, char ch)
{
//boolean val= Check(str,ch); //Methode 1 calling
if(Check(str,ch)) //Methode 2
System.out.println("\""+str+"\" begins with \'"+ch+"\'"); //Escape
sequences or
Non-graphic characters
else
System.out.println("\""+str+"\" does not begins with \'"+ch+"\'");
}
}
SOLUTION
import java.util.Scanner;
public class Series_fib {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Choose an option:");
System.out.println("1. Print Fibonacci series up to n terms");
System.out.println("2. Print prime numbers up to n");
int choice = scanner.nextInt();
System.out.print("Enter the value of n: ");
int n = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("Fibonacci series up to " + n + " terms:");
printFibonacci(n);
break;
case 2:
System.out.println("Prime numbers up to " + n + ":");
printPrimes(n);
break;
default:
System.out.println("Invalid choice");
}
scanner.close();
}
public static void printFibonacci(int n) {
int a = 0, b = 1;
for (int i = 0; i < n; i++) {
System.out.print(a + " ");
int next = a + b;
a = b;
b = next;
}
System.out.println();
}
public static void printPrimes(int n) {
for (int num = 2; num <= n; num++) {
boolean isPrime = true;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.print(num + " ");
}
}
System.out.println();
}
}
VARIABLE DESCRIPTION TABLE
Variable Data Type Description
scanner Scanner Scanner object for user input.
choice int User's choice of series to
print.
n int Number of terms for Fibonacci
or limit for primes.
a int First number in Fibonacci
series.
b int Second number in Fibonacci
series.
next int Next number in Fibonacci
series.
num int Current number being
checked for primality.
isPrime boolean Flag to determine if num is
prime.
i int Loop counter for primality
test.
QUESTION
Write a program to calculate and display the sum of odd and even numbers
separately till nTH number.
SOLUTION
import java.util.*;
class meow9
{
void sum(int n)
{
int x , sume = 0 , sumo = 0;
for(x = 1; x <= n; x++)
{
if(x%2 == 0)
sume += x;
else
sumo += x;
}
System.out.println("Sum of even numbers is =" + sume);
System.out.println("Sum of odd numbers is =" + sumo);
}
public static void main(String wagpo[])
{
meow9 obj = new meow9();
Scanner sc = new Scanner(System.in);
System.out.println("Enter the upper limit");
int n = sc.nextInt();
obj.sum(n);
}
}
SOLUTION
import java.util.Scanner;
public class Squarecal{
public static int square(int num) {
return num * num;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an integer: ");
int number = scanner.nextInt();
int result = square(number);
System.out.println("The square of " + number + " is " + result + ".");
scanner.close();
}
}
VARIABLE DESCRIPTION TABLE
SOLUTION
import java.util.Scanner;
public class Factorial {
public static long factorial(int n) {
long result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a non-negative integer: ");
int number = scanner.nextInt();
if (number < 0) {
System.out.println("Please enter a non-negative integer.");
} else {
long result = factorial(number);
System.out.println("The factorial of " + number + " is " + result + ".");
}
scanner.close();
}
}
SOLUTION
import java.util.Scanner;
public class Largestword {
public static String findLargestWord(String sentence) {
String[] words = sentence.split(" ");
String largestWord = "";
for (String word : words) {
if (word.length() > largestWord.length()) {
largestWord = word;
}
}
return largestWord;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String inputSentence = scanner.nextLine();
String largestWord = findLargestWord(inputSentence);
System.out.println("The largest word is: " + largestWord);
scanner.close();
}
}
SOLUTION
import java.util.Scanner;
public class SmallestWordFinder {
public static String findSmallestWord(String sentence) {
String[] words = sentence.split(" ");
String smallestWord = words[0];
for (String word : words) {
if (word.length() < smallestWord.length()) {
smallestWord = word;
}
}
return smallestWord;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String inputSentence = scanner.nextLine();
String smallestWord = findSmallestWord(inputSentence);
System.out.println("The smallest word is: " + smallestWord);
scanner.close();
}
}
VARIABLE DESCRIPTION TABLE
Variable Name Data Type Description
sentence String The input sentence
provided by the user.
words String[] Array of words
obtained by splitting
the sentence.
SOLUTION
import java.util.Scanner;
SOLUTION
import java.util.Scanner;
if ("AEIOUaeiou".indexOf(firstLetter) != -1) {
pigLatinWord = word + "way";
} else {
pigLatinWord = word.substring(1) + firstLetter + "ay";
}
return pigLatinWord;
}
SOLUTION
import java.util.Scanner;
public class stringrev {
public static String reverseWordsInString(String sentence) {
String[] words = sentence.split(" ");
StringBuilder reversedSentence = new StringBuilder();
for (String word : words) {
String reversedWord = new StringBuilder(word).reverse().toString();
reversedSentence.append(reversedWord).append(" ");
}
return reversedSentence.toString().trim();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String inputSentence = scanner.nextLine();
String result = reverseWordsInString(inputSentence);
System.out.println("Reversed words: " + result);
scanner.close();
}
}
VARIABLE DESCRIPTION TABLE
Variable Name Data Type Description
sentence String The input sentence
provided by the user.
words String[] Array of words
obtained by splitting
the sentence.
reversedSentence StringBuilder The sentence
constructed with each
word reversed.
reversedWord String The reversed version
of each word.
inputSentence String The sentence entered
by the user.
scanner Scanner Scanner object for
reading user input.