0% found this document useful (0 votes)
16 views38 pages

PM Project

Uploaded by

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

PM Project

Uploaded by

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

ASSESSMENT SHEET

Name: Pradyumn Mishra

Class: X

Section: B

Roll No.: 34

Internal Examiner: .................................

External Examiner: .................................


ACKNOWLEDGEMENT
The success and final outcome of this project required a lot of guidance and assistance
from many people and I am extremely privileged to have got this all along the completion
of my project. All that I have done is only due to such supervision and assistance and I
would not forget to thank them.

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.

3 Write a program to sort the elements


in an array using bubble sort
technique.
4 Write a program to enter numbers in
an array and search the given number
using binary search technique.

5 Write a program to enter numbers in


an array and search the given number
using linear search technique.
* Functions
6 Write a java program to input a
string(str) and a character (Ch) and
check if string (str) begins with
inputted character (Ch) or not using
the given functions.
7 Write a program to ask user to print
fibonacci series upto ‘n’ terms or print
prime numbers upto given number ‘n’.
8 Write a program to calculate and
display the sum of odd and even
numbers separately till nTH number.
9 Write a Java program that includes a
function named square(int num) which
takes an integer as a parameter and
returns the square of that integer.
10 Write a Java program that includes a
function named factorial(int) that
takes a non-negative integer as a
parameter and returns the factorial of
that integer.

* 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 );
}
}

VARIABLE DESCRIPTION TABLE


VARIABLE DATATYPE PURPOSE
i int Looping Variable

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();
}
}

VARIABLE DESCRIPTION TABLE


Variable Name Data Type Description

arr int[] Array to store the elements


to be sorted

scanner Scanner Scanner object to take user


input
i int Loop variable for iterating
over the array for sorting

minIndex int Index of the smallest


element in the unsorted part
of array
j int Loop variable for finding the
minimum element

temp int Temporary variable used to


swap elements in the array
QUESTION
Write a program to sort the elements in an array using bubble sort technique.

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();
}
}

VARIABLE DESCRIPTION TABLE


Variable Name Data Type Description
arr int[] Array to store the elements
to be sorted.
scanner Scanner Scanner object to take user
input.
i int Loop variable for controlling
the number of passes.
j int Loop variable for comparing
adjacent elements.
temp int Temporary variable used to
swap elements in the array.
QUESTION
Write a program to sort the elements in an array using bubble sort technique.

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();
}
}

VARIABLE DESCRIPTION TABLE


Variable Name Data Type Description
arr int[] Array to store the elements
to be sorted
scanner Scanner Scanner object to take user
input
i int Loop variable for controlling
the number of passes
j int Loop variable for comparing
adjacent elements
temp int Temporary variable used to
swap elements in the array
QUESTION
Write a program to enter numbers in an array and search the given number
using binary search technique.

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();
}
}

VARIABLE DESCRIPTION TABLE

Variable Name Data Type Description


arr int[] Array to store the elements
to be sorted and searched
scanner Scanner Scanner object to take user
input
key int Number to be searched in
the array
low int Lower bound of the search
range
high int Upper bound of the search
range
mid int Middle index of the current
search range
found boolean Flag to indicate if the
number was found
QUESTION
Write a program to enter numbers in an array and search the given number
using linear search technique.

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();
}
}

VARIABLE DESCRIPTION TABLE

Variable Name Data Type Description


arr int[] Array to store the elements
to be searched
scanner Scanner Scanner object to take user
input
key int Number to be searched in
the array
found boolean Flag to indicate if the
number was found
i int Loop variable for iterating
through the array
FUNCTIONS
QUESTION
Write a java program to input a string(str) and a character(ch) and check if
string (str) begins with inputted character (ch) or not using the given
functions.

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+"\'");
}
}

VARIABLE DESCRIPTION TABLE


Variable Name Type Description
s String The string to check if it
starts with a specific
character.
c char The character to compare
with the first character of
the string
ch char The character to check
against the first character
of str.
val boolean The result of the check if str
starts with ch (not used in
the provided code).
str String The input string that is being
checked in the method
mymain.
QUESTION
Write a program to ask user to print fibonacci series upto ‘n’ terms or print
prime numbers upto given number ‘n’.

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);
}
}

VARIABLE DESCRIPTION TABLE


Variable Data Type Description
x int Loop counter for iterating
through numbers.
sume int Sum of even numbers
calculated.
sumo int Sum of odd numbers
calculated.
n int User input for the upper
limit for summation.
obj meow9 Instance of the meow9 class
for calling methods.
sc Scanner Scanner object for user
input.
wagpo String[] Command-line arguments
passed to the main method
(not used in this program).
QUESTION
Write a Java program that includes a function named square(int num) which
takes an integer as a parameter and returns the square of that integer.

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

Variable Name Data Type Description


num int The integer input to
calculate the square.
number int The integer entered by the
user.
result int The result of the square
calculation.
scanner Scanner Scanner object for reading
user input.
QUESTION
Write a Java program that includes a function named factorial(int
n) that takes a non-negative integer as a parameter and returns the factorial
of that integer.

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();
}
}

VARIABLE DESCRIPTION TABLE


Variable Name Data Type Description
n int The non-negative integer
input for which the factorial
is calculated.
result long The factorial result.
number int The integer entered by the
user.
scanner Scanner Scanner object for reading
user input.
STRINGS
QUESTION
Write a program to find the largest word.

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();
}
}

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.
largestWord String The largest word found in
the input sentence.
inputSentence String The sentence entered by the
user.
scanner Scanner Scanner object for reading
user input.
QUESTION
Write a program to find the smallest word.

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.

smallestWord String The smallest word


found in the input
sentence.

inputSentence String The sentence entered


by the user.
scanner Scanner Scanner object for
reading user input.
QUESTION
Write a program to input any string and display the name in given form.

SOLUTION
import java.util.Scanner;

public class NameFormatter {


public static String formatName(String name) {
String[] words = name.split(" ");
StringBuilder formattedName = new StringBuilder();
for (String word : words) {
if (word.length() > 0) {
formattedName.append(Character.toUpperCase(word.charAt(0)));
formattedName.append(word.substring(1).toLowerCase());
formattedName.append(" ");
}
}
return formattedName.toString().trim();
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter a name: ");
String inputName = scanner.nextLine();
String formattedName = formatName(inputName);
System.out.println("Formatted name: " + formattedName);
scanner.close();
}
}

VARIABLE DESCRIPTION TABLE


Variable Name Data Type Description
name String The input name provided
by the user.
words String[] Array of words obtained
by splitting the name.
formattedName StringBuilder The formatted name with
proper title case.
inputName String The name entered by the
user.
scanner Scanner Scanner object for
reading user input.
QUESTION
Write a program to encode a word to pig latin.

SOLUTION
import java.util.Scanner;

public class Piglat {


public static String toPigLatin(String word) {
String pigLatinWord = "";
char firstLetter = word.charAt(0);

if ("AEIOUaeiou".indexOf(firstLetter) != -1) {
pigLatinWord = word + "way";
} else {
pigLatinWord = word.substring(1) + firstLetter + "ay";
}

return pigLatinWord;
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter a word: ");
String inputWord = scanner.nextLine();
String pigLatinWord = toPigLatin(inputWord);
System.out.println("The Pig Latin version is: " + pigLatinWord);
scanner.close();
}
}

VARIABLE DESCRIPTION TABLE

Variable Data Type Description


Name
word String The input word
provided by the user.
pigLatinWord String The encoded word in
Pig Latin.
firstLetter char The first letter of the
input word.
inputWord String The word entered by
the user.
scanner Scanner Scanner object for
reading user input.
QUESTION
Write a program to display each word of string in reverse order.

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.

You might also like