0% found this document useful (0 votes)
1 views30 pages

Class X Project File Programsdocx

The document contains a collection of Java programming exercises, each with source code and expected output. Topics include calculating factorials, displaying patterns, Fibonacci and Tribonacci series, array sorting algorithms, and examples of function overloading and call by reference/value. Each program is designed to illustrate specific programming concepts and techniques in Java.

Uploaded by

Nikhil Awasthi
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)
1 views30 pages

Class X Project File Programsdocx

The document contains a collection of Java programming exercises, each with source code and expected output. Topics include calculating factorials, displaying patterns, Fibonacci and Tribonacci series, array sorting algorithms, and examples of function overloading and call by reference/value. Each program is designed to illustrate specific programming concepts and techniques in Java.

Uploaded by

Nikhil Awasthi
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/ 30

Table of Contents

Write a program to calculate factorial using recursion. ...................................................................... 2

Write a program to display triangle of star. ....................................................................................... 3

Write a program to print fibonacci series- ............................................................................................ 4

Write a program to print Tribonacci series of n term- ........................................................................... 5

Write a program to display Example of call by reference. ...................................................................... 6

Write a program to display Example of call by value. ............................................................................ 7

Write a program to input a sentence. Find and display the following: ................................................... 8

Write a program To check whether gine number is pallindrom or not................................................ 10

Write a program to display Example of call by value. .......................................................................... 11

Write a program to display example of function overloading. ............................................................. 12

/* To convert radius of circle into perimeter*/ .................................................................................... 13

Write a program to Search an array element using Binary search technique. ....................................... 14

Write a program to sort an array element in accessinding order by using BubbleSort technique. ......... 15

Write a program to sort an array element by using SelectionSort. ...................................................... 16

Write a program to display maximum or minimum number in 5 element array list. ............................. 17

Write a program to display sum of left and right diagnol. ................................................................... 18

Write a program to genrate sum of all element os a double dimentional array of 3*3 subscript. ......... 19

Write a program to calculate sale of 5 salesman of 12 months. ........................................................... 21

Check Whether or Not the Number is an Armstrong Number .............................................................. 22

Java program to check if a number is Automorphic ............................................................................. 23

Find and display the next 10th character in the ASCII table. ................................................................ 24

Single Inheritance Example ................................................................................................................. 25

Multilevel Inheritance Example .......................................................................................................... 26

Java Interface Example ....................................................................................................................... 27

Java Program to print pattern ............................................................................................................. 28

Number triangle Pattern ..................................................................................................................... 29

// Java Program to print pattern ......................................................................................................... 30


2

Program 1.
Write a program to calculate factorial using recursion.

Source Code

import java.io.*; class


Factorial
{
public static void main(String args[])throws IOException //main function
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter no=");
int n=Integer.parseInt(br.readLine()); //accepting no
Factorial obj=new Factorial();
long f=obj.fact(n);
System.out.println("Factorial="+f); //display factorial
}
public long fact(int n) //Recursive fact()
{
if(n<2)
return 1;
else return(n*fact(n-1));
}
}

Output:-
Enter no=
12
Factorial=479001600
3

Program 2.
Write a program to display triangle of star.

Source Code

import java.util.*; public


class Triangle
{
public static void printTriangle(int n) //main function
{
for(int i=0;i<n;i++) //outer loop gets executed
{
for(int j=n-i;j>1;j--) //inner loop gets executed
{
System.out.print(" "); //print space
}
for(int j=0;j<=i;j++)
{
System.out.print("* "); //print star
}
System.out.println(); //make new line
}
}
public static void main(String[] args)
{ int
n=5;
printTriangle(n);
}
}

Output-
*
**
***
****
*****
4

Program 3.
Write a program to print fibonacci series-
0 1 1 2 3 5 8 13 21 ......n
Source Code
Import java.util.scanner; public
class fibonacci
{
public static void fib() // method
fib {
int a=0,b=1,c,i,n;
Scanner in=new Scanner(System.in)
System.out.println(“Enter how mant term of fibonacci series”);
n=in.nextInt(); // input number
System.out.print(a+ " " +b+" ");
for(i=0;i<n;i++) // execute fibonacci
{
c=a+b;
System.out.print(c+" ");
a=b;
b=c;
}
}
public static void main(String args[])
{
fibonacci obj=new fibonacci(); //create object
obj.fib();

Output-
0 1 1 2 3 5 8 13 21
5

Program 4.
Write a program to print Tribonacci series of n term-
0 0 1 1 2 4 7 13 24 44 ......n
Source Code
Import java.util.scanner; public
class Tribonacci
{
public static void Tribo() // method
Tribo
{
int a=0,b=0,c=1,d,i,n;
Scanner in=new Scanner(System.in)
System.out.println(“Enter how mant term of tribonacci series”);
n=in.nextInt(); // input
number
System.out.print(a+" "+b+" "+c+" ");
for(i=0;i<7;i++) // execute
Tribonacci
{
d=a+b+c;
System.out.print(d+" ");
a=b;
b=c; c=d;
}
}
public static void main(String args[])
{
Tribonacci obj=new Tribonacci(); //create object
obj.Tribo();
}
}

Output-
0 0 1 1 2 4 7 13 24 44
6

Program 5.
Write a program to display Example of call by reference.

Source Code

Public class CallReference


{
public static void main(String[] args) //main function
{
int[] value={125,635};
System.out.println("before display Array method first element of
valuearray"+value[0]);
displayArray(value); //function calling
System.out.println("after display Array method first element of value array"+value[0]);
}

public static void displayArray(int[] value) //Decription of


displayArray
{
System.out.println("Inside display array method element is =" +value[0]);
value[0]=100;
System.out.println("Inside display array method element is ="+value[0]);
}

Output-
before display array method first element of value array 125
Inside display array method element is=125 Inside
display array method element is=100
after display array method first element of value array 100
7

Program 6.
Write a program to display Example of call by value.

Source Code

Public class CallByValue


{
public static void main(String[] args) //main
function
{
int number=25;
System.out.println("Before calling display method number:" +number);
display(number); // calling
display
System.out.println("After display Array number:"+number);
}

public static void display(int num) //description of display


function
{
System.out.println("Inside display method number:" +num);
}

Output-
Before calling display method number:25
Inside display method number:25
After display Array number:25
8

Program 7.
Write a program to input a sentence. Find and display the following:
(i) Number of words present in the sentence
(ii) Number of letters present in the sentence Assume that the sentence has neither
include any digit nor a special character.
import java.util.Scanner;
public class WordsNLetters
{
public static void main(String args[]) { Scanner
in = new Scanner(System.in);
System.out.println("Enter a sentence:");
String str = in.nextLine();
int wCount = 0, lCount = 0;
int len = str.length(); for
(int i = 0; i < len; i++) {
char ch = str.charAt(i);
if (ch == ' ')
wCount++;
else lCount++;
}
/*
* Number of words in a sentence are one more than
* the number of spaces so incrementing wCount by 1
*/
wCount++;
System.out.println("No. of words = " + wCount);
System.out.println("No. of letters = " + lCount); }
}
Output:-
Enter a sentence :
Weather is cold today
No. of words = 4
No. of letters = 18
9

Program 8.
Write a program in Java to accept a word/a String and display the new string after removing
all the vowels present in it.
Sample Input: COMPUTER APPLICATIONS Sample
Output: CMPTR PPLCTNS
import java.util.Scanner;
public class VowelRemoval
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a word or sentence:");
String str = in.nextLine();
int len = str.length();
String newStr = ""; for (int
i = 0; i < len; i++) {
char ch = Character.toUpperCase(str.charAt(i)); if(ch
== 'A' ||ch == 'E' ||ch == 'I' || ch == 'O' ||ch == 'U')
{ continue;
}
newStr = newStr + ch;
}
System.out.println("String with vowels removed");
System.out.println(newStr);
}
}

Output:-
10

Program 9.
Write a program To check whether gine number is pallindrom or
not.

Source Code

import java.util.Scanner; public


class pallin
{
public static void main(String[] args)
//main function
{
int s=0,r,n;
Scanner in=new Scanner(System.in);
System.out.println("Enter any number:-");
n=in.nextInt();
//accept no
int m=n;
while(n>0)
{
r=n%10;
s=(s*10)+r;
//calculate pallindrom
n=n/10;
}
if(m==s)
System.out.print("Pallindrome");
else
System.out.print("not Pallindrome");
}}

Output-
Enter any number:- 121
Pallindrome
11

Program 10.
Write a program to display Example of call by value.
Source Code
Public class CallByValue

{ public static void main(String[] args) //main

function

int number=25;

System.out.println("Before calling display method number:" +number);

display(number); // calling display

System.out.println("After display Array number:"+number);

public static void display(int num) //description of display function

System.out.println("Inside display method number:" +num);

Output-
Before calling display method number:25

Inside display method number:25

After display Array number:25


12

Program 11.
Write a program to display example of function overloading.
Source Code
public class sum

{ public int sum(int x, int y) //overloaded funtion with two

parameter

return(x+y);

public double sum(int x,int y,int z) //overloaded funtion with three parameter

return(x+y+z);

public double sum(double x,double y) //overloaded funtion double type

return(x+y);

public static void main(String[] args)

sum s=new sum();

System.out.println(s.sum(10,20)); //calling sum fun. With diff. argument

System.out.println(s.sum(10,20,30));

System.out.println(s.sum(10.5,20.5));

Output-
30
60.0
31.0
13

Program 12.
/* To convert radius of circle into perimeter*/
import java.util.Scanner;
public class simple
{
public static void main(String[] agrs)
{
Scanner in=new Scanner(System.in);

System.out.println("Enter radius of circle");

double radius=in.nextDouble(); final

double pi=3.14159; double

perm=2*pi*radius;

System.out.println("Radius of circle is:" +radius);


System.out.println("Perimeter of the circle is:" +perm);
}
}
output-
Enter radius of circle
45.6
Radius of Circle:45.6
Perimeter of the circle:286.513008
14

Program 13.
Write a program to Search an array element using Binary search technique.
Source Code import
java.util.*; public class
BinarySearch {
public static void main(String args[]) //main function
{
int li,hi,mi;
int a[]={2,3,4,5,6,7,8,11,13,16}; //intializing array element
for(int i=0;i<10;i++)
{
System.out.println(a[i]); //Display array element
}
Scanner sc=new Scanner(System.in);
System.out.println("Enter search element");
int serc=sc.nextInt(); //accepting search element
hi=a.length-1; li=0; mi=(li+hi)/2;
//find mid position while(li<=hi)
//loop gets evaluvate
{
if(a[mi]==serc)
{
System.out.println("Element is at"+mi+"index position");
break;
}
else if(a[mi]<serc)
{
li=mi+1;
}
else if(a[mi]>serc)
{
hi=mi-1;
}
mi=(li+hi)/2;
}
if(li>hi) {
System.out.println("Element not found");
}}}
Output- Binary
search
23
4
5
67
8
11
15 18
Enter search element:
15
11 element is at 7 index
position
15

Program 14.
Write a program to sort an array element in accessinding order by using BubbleSort
technique.
Source Code

import java.util.Scanner;
public class BubbleSort
{
public static void main(String[] args) //main function
{
int list[]={65,47,40,9,37,72,45,173}; //initializing array element
int len=list.length;
for(int i=0;i<len-1;i++)
{
for(int j=0;j<len-1-i;j++)
{
if(list[j]>list[j+1])
{
int tmp=list[j];
list[j]=list[j+1];
list[j+1]=tmp;
}
}
}
System.out.println("sorted array is:-");
for(int i=0;i<len;i++)
{
System.out.println(list[i]); // Display sorted element
}
}
}
Output-
Sorted array is:-
9
37
40
45
47
65
72
17
16

Program 15.
Write a program to sort an array element by using SelectionSort.
Source Code
import java.util.Scanner; public
class SelectionSort
{ public static void main(String[] args) //main
function
{
int i,j,min,temp=0; int a[]={65,47,40,9,37,72,45,173};
//initializing array element for(i=0;i<a.length;i++)
{ min=i;
for(j=i+1;j<a.length;j++)
{
{
min=j;
}
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
System.out.println("Sorted array is:-");
for(i=0;i<a.length;i++)
{
System.out.println(a[i]+" "); //Display sorted array element
}
}
}

Output-
Sorted array is:-
9
37
40
45
47
65
72
173
18
17

Program 16.
Write a program to display maximum or minimum number in 5
element array list.
Source Code
import java.util.Scanner; public
class MinMax
{ public static void main(String[] args) //main
function
{
int number[]=new int[5];
int min,max;
Scanner keyboard=new Scanner(System.in);
System.out.println("Enter 5 integer-");
for(int i=0;i<5;i++) //accepting array element
number[i]=keyboard.nextInt(); min=number[0]; max=number[0];
for(int i=0;i<5;i++)
{
if(number[i]>max)
max=number[i];
if(number[i]<min)
min=number[i];
}

System.out.println("The gratest number of array element is:"+max);


System.out.println("The Smallest number of array element is:"+min);
}
}

Output-
Enter 5 integer-
3
4
5
6
8
The gratest number of array element is:8
The smallest number of array element is:3
18

Program 17.
Write a program to display sum of left and right diagnol.
Source Code
import java.util.*; class
DigonalSum
{ public static void main(String args[]) //main
function
{
int i,j,sum=0; int[][] a={{2,4,5},{8,9,1},{3,1,9}};
//initializing array matrix for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(a[i][j] +" ");
} System.out.println();
} i=0;
for(j=0;j<3;j++)
{

sum=sum+a[i][j];
i++;
}
//System.out.println("Sum="+sum);
j=2;
for(i=0;i<3;i++)
{
sum=sum+a[i][j];
j--;
}
System.out.print("Sum of diagnoal:"+sum); //print diagnoal sum
}
}

Output-
To diaplay diagpnal sum
245
891
319
Sum of diagnol:37
19

Program 18.
Write a program to genrate sum of all element os a double dimentional array
of 3*3 subscript.
Source Code
import java.util.Scanner; public class MatrixSum
{ public static void main(String[] args) //main
function
{ int matrixA[][]={{1,2,3},{4,5,6,},{7,8,6}}; // initializing
matrixA int matrixB[][]={{11,12,3},{14,15,16},{17,18,19 // initializing
matrixB int matrixC[][]=new int[3][3]; // declare
matrixC System.out.println("MatrixA is:-"); for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(matrixA[i][j]+" "); // read matrixA
}
System.out.print("\n");
}
System.out.println("MatrixB is:-"); //read matrixB
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(matrixB[i][j]+" ");
}
System.out.print("\n");
}
System.out.println("MatrixC is:-");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
matrixC[i][j]=matrixA[i][j]+matrixB[i][j];
}
}
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
20

System.out.print(matrixC[i][j]+" "); //display sum of two


matrix
}
System.out.print("\n");
}
}
}

Output-
MatrixA is:-
123
456
786
MatrixB is:-
11 12 3
14 15 16
17 18 19
MatrixC is:-
12 14 6
18 20 22
24 26 25
21

Program 19.
Write a program to calculate sale of 5 salesman of 12 months.
Source Code

import java.util.Scanner; public


class Salesman
{ public static void main(String[] args) //main
function
{
Scanner in=new Scanner(System.in);
int sales[][]=new int[5][12]; int
i,j,total; try
{
//accepting and displaying sale of 5 salesman for 12 month
for(i=0;i<5;i++)
{
total=0;
for(j=0;j<12;j++)
{
System.out.println("Month"+(j+1)+":");
sales[i][j]=in.nextInt();
total+=sales[i][j];
}
System.out.println("total sales of salesman"+(i+1)+"="+total);
}
}
catch(Exception e)
{}
}
}
22

Program 20.
Check Whether or Not the Number is an Armstrong Number
Source Code
public class Main
{
public static void main (String[]args)
{
int num = 407, len;

// function to get rder(length)


len = order (num);

// check if Armstrong
if (armstrong (num, len))
System.out.println(num + " is armstrong");
else
System.out.println(num + " is armstrong");

static int order (int x)


{ int len =
0; while (x !=
0 )
{
len++; x
= x / 10;
}
return len;
}
static boolean armstrong (int num, int len)
{
int sum = 0, temp, digit;
temp = num;

// loop to extract digit, find power & add to sum


while (temp != 0)
{
// extract digit
digit = temp % 10;

// add power to sum sum = sum +


(int)Math.pow(digit, len); temp /= 10;
};
return num == sum;
}
}
Output
407 is armstrong
23

Program 21.
Java program to check if a number is Automorphic
Source Code
import java.io.*; class
Test {
// Function to check Automorphic number
static boolean isAutomorphic(int N)
{
// Store the square
if(N < 0) N = -N;
int sq = N * N;

// Start Comparing digits


while (N > 0) {
// Return false, if any digit of N doesn't
// match with its square's digits from last
if (N % 10 != sq % 10)
return false;

// Reduce N and square


N /= 10;
sq /= 10;
}

return true;
}

// Driver method
public static void main(String[] args)
{
int N = 5;

System.out.println(isAutomorphic(N) ? "Automorphic" : "Not Automorphic");


}
}
Output
Automorphic
24

Program 22.
Find and display the next 10th character in the ASCII table.
Source Code
import java.util.Scanner;
public class Ans_1
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a character: ");
char ch = in.next().charAt(0); char
nextCh = (char)(ch + 10);
System.out.println("Tenth character from " + ch + " is " + nextCh);
}
}
Output
Enter a character: Computer is an electronic device
Tenth character from C is M
25

Program 23.
Single Inheritance Example
Source Code
class Animal{ void
eat(){System.out.println("eating...");}
}
class Dog extends Animal{ void
bark(){System.out.println("barking...");}
}
class TestInheritance{ public static
void main(String args[]){ Dog
d=new Dog();
d.bark();
d.eat();
}}
Output: barking...
eating...
26

Program 24.
Multilevel Inheritance Example
class Animal{ void
eat(){System.out.println("eating...");}
}
class Dog extends Animal{ void
bark(){System.out.println("barking...");}
}
class BabyDog extends Dog{ void
weep(){System.out.println("weeping...");}
}
class TestInheritance2{ public static
void main(String args[]){ BabyDog
d=new BabyDog(); d.weep();
d.bark();
d.eat();
}}
Output: weeping...
barking...
eating...
27

Program 25.
Java Interface Example
In this example, the Printable interface has only one method, and its implementation
is provided in the A6 class.
interface printable{ void
print();
}
class A6 implements printable{ public void print(){
System.out.println("Hello");}
public static void main(String args[])
{
A6 obj = new A6();
obj.print();
}
}
Output:
Hello
28

Program 26.
Java Program to print pattern
// Square hollow pattern
import java.util.*;
public class GeeksForGeeks {
// Function to demonstrate pattern
public static void printPattern(int n)
{
int i, j;
// outer loop to handle number of rows
for (i = 0; i < n; i++) {
// inner loop to handle number of columns
for (j = 0; j < n; j++) {
// star will print only when it is in first
// row or last row or first column or last
// column
if (i == 0 || j == 0 || i == n - 1
|| j == n - 1) {
System.out.print("*");
}
// otherwise print space only.
else {
System.out.print(" ");
}
}
System.out.println();
}
}

// Driver Function
public static void main(String args[])
{
int n = 6;
printPattern(n);
}}
Output
29

Program 26.
Number triangle Pattern
// Java Program to print pattern
// Number triangle pattern
import java.util.*;
public class GeeksForGeeks {
// Function to demonstrate pattern
public static void printPattern(int n)
{
int i, j;
// outer loop to handle number of rows
for (i = 1; i <= n; i++) {
// inner loop to print space
for (j = 1; j <= n - i; j++) {
System.out.print(" ");
}
// inner loop to print star
for (j = 1; j <= i; j++) {
System.out.print(i + " ");
}
// print new line for each row
System.out.println();
}
}

// Driver Function
public static void main(String args[])
{
int n = 6;
printPattern(n);
}
}

Output
30

Program 27.
// Java Program to print pattern
// Number-increasing pyramid
import java.util.*;

public class GeeksForGeeks {


// Function to demonstrate pattern
public static void printPattern(int n)
{
int i, j;
// outer loop to handle number of rows
for (i = 1; i <= n; i++) {
// inner loop to handle number of columns
for (j = 1; j <= i; j++) {
// printing column values upto the row
// value.
System.out.print(j + " ");
}

// print new line for each row


System.out.println();
}
}

// Driver Function
public static void main(String args[])
{
int n = 6;
printPattern(n);
}
}
Output

======➔

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy