You are on page 1of 26

NAME- Khushi Mehta

CLASS-10
SECTION-A
SUBJECT-COMPUTER Application

school- mussoorie international school

topic- 20 programs on blueJ


Acknowledgement
I would like to present my warmest gratitude
to my computer teacher for clearing my doubts
when needed.

1) Write a java program to display the sum of the first 10


natural numbers.
ANS:
1. public class SumOfNaturalNumber 
2. {  
3. public static void main(String[] args)   
4. {  
5. int i, num = 10, sum = 0;  
6. for(i = 1; i <= num; ++i)  
7. {  
sum = sum + i;  
}  
8. System.out.println("Sum of First 10 Natural Numb
ers is = " + sum);  
9. }

OUTPUT:
The sum of first 10 natural numbers is = 55

2) Write a Java program to display the first 10 terms


of Fibonacci series 0,1,1,2,3,5,8……
ANS:
class series
{
public static void main(String[] args)
{
int n = 10, fTerm = 0, sTerm = 1;
System.out.println("Fibonacci Series till " + n + " terms:");
for (int i = 1; i <= n; ++i)
{
System.out.print(fTerm + ", ");

int nextTerm = fTerm + sTerm;


fTerm = sTerm;
sTerm = nextTerm;
}
}
}
OUTPUT:
Fibonacci Series till 10 terms:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34,
3) Write a java program to search an element in an array
using binary
ANS:
class Search

{
public static void Search(int arr[], int first, int last, int key)
{
int mid = (first + last)/2;
while( first <= last ){
if ( arr[mid] < key ){
first = mid + 1;
}
else if ( arr[mid] == key )
{
System.out.println("Element is found at index: " + mid);
break;
}
else
{
last = mid - 1;
}
mid = (first + last)/2;
}
if ( first > last )
{
System.out.println("Element is not found!");
}
}
public static void main(String args[]){
int arr[] = {10,20,30,40,50};
int key = 30;
int last=arr.length-1;
Search(arr,0,last,key);
}
}
Output:
Element is found at index: 2

4) Write a java program to search an element in an


array using linear search
ANS:
public class Search2
{
public static int Search2(int[] arr, int key)
{
for(int i=0;i<arr.length;i++)
{
if(arr[i] == key)
{
return i;
}
}
return -1;
}
public static void main(String a[]){
int[] a1= {10,30,70,100};
int key = 70;
System.out.println(key+" is found at index:
"+Search2(a1, key));
}
}
Output:
70 is found at index: 2

5) Write a java program to sort an array using selection


sort.
ANS:
public class SelectionSort
{
public static void main(String args[])
{
int array[] = {10, 20, 25, 63, 96, 57};
int size = array.length;
for (int i = 0 ;i< size-1; i++)
{
int min = i;
for (int j = i+1; j<size; j++)
{
if (array[j] < array[min])
{
min = j;
}
}
int temp = array[min];
array[min] = array[i];
array[i] = temp;
}
for (int i = 0 ;i< size; i++)
{
System.out.print(" "+array[i]);
}
}
}
Output:
10 20 25 57 63 96

6) Write a java program to sort an array using bubble


sort.
ANS:
class BubbleSort
{
void bubbleSort(int arr[])
{
int n = arr.length;
for (int i = 0; i < n-1; i++)
for (int j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
{
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
public static void main(String args[])
{
BubbleSort ob = new BubbleSort();
int arr[] = {64, 34, 25, 12, 22, 11, 90};
ob.bubbleSort(arr);
System.out.println("Sorted array");
ob.printArray(arr);
}
}
Output:
Sorted array
112 22 25 34 64 90

7) Write a java program to display Factorial of a


number.
ANS:
class Factorial
{
public static void main(String args[])
{
int i,fact=1;
int number=10;
for(i=1;i<=number;i++)
{
fact=fact*i;
}
System.out.println("Factorial of "+number+" is:
"+fact);
}
}
Output:
Factorial of 10 is: 3628800

8) Write a java program To calculate charges for


sending parcels when the charges are as follows :
For 1 k=15.00 rupees
Additional weight thereof = 8.00 rupees.
ANS:
import java.util.*;
class Parcel
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int w, a, b;
double c, d;
System.out.println("Enter the weight in grams");
w = in.nextInt();
if (w<=1000)
{
System.out.println("Charge = Rs.15");
}
else
{
a = w - 1000;
d = (w - 1000)/500;
b = a % 500;
if (b == 0)
{
c = 15 + (d*8);
System.out.println("Charge = "+c);
}
else
{ c = 15 + (int)d*8;
System.out.println("Charge = "+c);
}
}
}
}
Output:
Enter the weight in grams 500
Charge = Rs.15

9) Write a java program to check whether a number is


special two-digit or not. A special number is such
that the sum of the digits is added to product of
the digits such that the result is equal to the
original number.
ANS:
import java.util.Scanner;
public class Number3
{
public void checkNumber()
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a 2 digit number: ");
int orgNum = in.nextInt();
int num = orgNum;
int count = 0, digitSum = 0, digitProduct = 1;
while (num != 0)
{
int digit = num % 10;
num /= 10;
digitSum += digit;
digitProduct *= digit;
count++;
}
if (count != 2)
System.out.println("Invalid input, please enter a
2-digit number");
else if ((digitSum + digitProduct) == orgNum)
System.out.println("Special 2-digit number");
else
System.out.println("Not a special 2-digit
number");
}
}

10) Write a java program . Write a program to


display the following:
13579
35791
57913
79135
91357
ANS:
import java.util.Scanner;
public class Pattern
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter n: ");
int n = scanner.nextInt();
for (int i = 1; i <= n; i++)
{
int num = 2 * i - 1;
for (int j = 1; j <= n - i + 1; j++)
{
System.out.print(num + " ");
num = num + 2;
}
int num2 = 1;
for (int j = 1; j <= i - 1; j++)
{
System.out.print(num2 + " ");
num2 = num2 + 2;
}
System.out.println();
}
}
}
Output:
Enter n: 5
13579
35791
57913
79135
91357

11) Write down a java program to obtain first eight


numbers of this series:
1,11,111,1111,……..
ANS:
import java.util.Scanner;
public class Series
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of terms: ");
int n = sc.nextInt();
int s = 0, c;
for (c = 1; c <= n; c++)
{
s = s * 10 + 1;
System.out.print(s + " ");
}
}
}
Output:
Enter the number of terms: 8
1 11 111 1111 11111 111111 1111111 11111111

12) Write a java program to display n elements of


Fibonacci series without using the recursion.
ANS:
class Fibonacci
{
public static void main(String args[])
{
int n1=0,n2=1,n3,i,count=10;
System.out.print(n1+" "+n2);
for(i=2;i<count;++i)
{
n3=n1+n2;
System.out.print(" "+n3);
n1=n2;
n2=n3;
}
}
}
Output-
0 1 1 2 3 5 8 13 21 34

13) Write a java program to take a variable number


and check whether it is a palindrome or not.
ANS:
class palindrome
{
public static void main(String args[])
{
int r,sum=0,temp;
int n=242;

temp=n;
while(n>0){
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
System.out.println("palindrome number ");
else
System.out.println("not palindrome");
}

OUTPUT –
On taking 325 it will show ‘not palindrome’
14) Write a java program to check whether the given
number is a Armstrong number or not.
ANS:
import java.util.Scanner;
import java.lang.Math;
public class armstsrong
{
static boolean isArmstrong(int n)
{
int temp, digits=0, last=0, sum=0;
temp=n;
while(temp>0)
{
temp = temp/10;
digits++;
}
temp = n;
while(temp>0)
{
last = temp % 10;
sum += (Math.pow(last, digits));
temp = temp/10;
}
if(n==sum)
return true;
else return false;
}
public static void main(String args[])
{
int num;
Scanner sc= new Scanner(System.in);
System.out.print("Enter the limit: ");
num=sc.nextInt();
System.out.println("Armstrong Number up to "+
num + " are: ");
for(int i=0; i<=num; i++)
if(isArmstrong(i))
System.out.print(i+ ", ");
}
}
Output:
Armstrong Number up to 211 are:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153,

15) Write a java program to reverse a number using


while loop and for loop.
ANS:
public class reverse
{
public static void main(String[] args)
{
int number = 123456654, reverse = 0;
while(number != 0)
{
int remainder = number % 10;
reverse = reverse * 10 + remainder;
number = number/10;
}
System.out.println("The reverse of the given
number is: " + reverse);
}
}
Output:
456654321

16) Write a java program to convert the given


numbers into words.
ANS:
class NumberWord
{
static void numberToWords(char num[])
{
int len = num.length;
if (len == 0)
{
System.out.println("The string is empty.");
return;
}
if (len > 4)
{
System.out.println("\n The given number has more
than 4 digits.");
return;
}
String[] onedigit = new String[] {"Zero", "One",
"Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
"Nine"};
String[] twodigits = new String[] {"", "Ten", "Eleven",
"Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen",
"Seventeen", "Eighteen", "Nineteen"};
String[] multipleoftens = new String[] {"", "",
"Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy",
"Eighty", "Ninety"};
String[] poweroftens = new String[] {"Hundred",
"Thousand"};
System.out.print(String.valueOf(num) + ": ");
if (len == 1)
{
System.out.println(onedigit[num[0]-'0']);
return;
}
int x = 0;
while (x < num.length)
{
if (len >= 3)
{
if (num[x] - '0' != 0)
{
System.out.print(onedigit[num[x] - '0'] + " ");
System.out.print(poweroftens[len - 3]+ " ");
}
--len;
}
else
{
if (num[x] - '0' == 1)
{
int sum = num[x] - '0' + num[x + 1] - '0';
System.out.println(twodigits[sum]);
return;
}
else if (num[x] - '0' == 2 && num[x + 1] - '0'
== 0)
{
System.out.println("Twenty");
return;
}
else
{
int i = (num[x] - '0');
if (i > 0)
System.out.print(multipleoftens[i]+ " ");
else
System.out.print("");
++x;
if (num[x] - '0' != 0)
System.out.println(onedigit[num[x] - '0']);
}
}
++x;
}
}
public static void main(String args[])
{
numberToWords("673".toCharArray());
numberToWords("85".toCharArray());
numberToWords("".toCharArray());
}
}

OUTPUT:
673: Six Hundred Seventy Three
85: Eighty Five

17) Write a java program to check whether the


number is automorphic or not.
ANS:
public class automorphic
{
static boolean isAutomorphic(int num)
{
int square = num * num;

while (num > 0)


{
if (num % 10 != square % 10)
return false;
num = num/10;
square = square/10;
}
return true;
}
public static void main(String args[])
{
System.out.println(isAutomorphic(26) ?
"Automorphic" : "Not Automorphic");
System.out.println(isAutomorphic(73) ?
"Automorphic" : "Not Automorphic");
}
}

Output:
Not Automorphic
Not Automorphic

18) Write a java program to check if the given


number is peterson.
ANS:
import java.io.*;
public class Peterson
{
static int[] fact = new int[] { 1, 1, 2, 6, 24, 120,
720, 5040, 40320, 362880 };
static boolean peterson(int n)
{
int num = n;
int sum = 0;
while (n > 0)
{
int digit = n % 10;
sum += fact[digit];
n = n / 10;
}
return (sum == num);
}
static public void main(String[] args)
{
int n = 145;

if (peterson(n))
System.out.println("Yes");
else
System.out.println("No");
}
}
Output:
Yes

19) Write a java program to check all the sunny


numbers between a specified range.
ANS:
import java.util.*;
public class SunnyNumberExample1
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number to check: ");
int N=sc.nextInt();
isSunnyNumber(N);
}
static boolean findPerfectSquare(double num)
{
double square_root = Math.sqrt(num);
return((square_root - Math.floor(square_root)) ==
0);
}
static void isSunnyNumber(int N)
{
if (findPerfectSquare(N + 1))
{
System.out.println("The given number is a
sunny number.");
}
else
{
System.out.println("The given number is not a
sunny number.");
}
}
}
Output:
Enter a number to check: 34
The given number is not a sunny number.

20) Write a java program using switch case to display


a) name of the week
ANS:
class WeekDays
{
public static void main(String s[])
{
int day = 4;
switch(day)
{
case 1:
System.out.println(“Monday”);
break;
case 2:
System.out.println(“Tuesday”);
break;
case 3:
System.out.println(“Wednesday”);
break;
case 4:
System.out.println(“Thursday”);
break;
case 5:
System.out.println(“Friday”);
break;
default:
System.out.println(“Weekend”);
break;
}
}
}
Output:
Thursday

b) Name of the month.


ANS:
class month
{
public static void main(String arg[]) {
int mnth = 11;
switch (mnth)
{
case 1:
System.out.println(“Showing Month: January”);
break;
case 2:
System.out.println(“Showing Month: February”);
break;
case 3:
System.out.println(“Showing Month: March”);
break;
case 4:
System.out.println(“Showing Month: April”);
break;
case 5:
System.out.println(“Showing Month: May”);
break;
case 6:
System.out.println(“Showing Month: June”);
break; case 7:
System.out.println(“Showing Month: July”);
break;
case 8:
System.out.println(“Showing Month: August”);
break;
case 9:
System.out.println(“Showing Month: September”);
break;
case 10:
System.out.println(“Showing Month: October”);
break;
case 11:
System.out.println(“Showing Month: November”);
break;
case 12:
System.out.println(“Showing Month: December”);
break;
default:
System.out.println(“Invalid input – Wrong month
number.”);
break;
}
}
}
Output:
Showing Month: November

c) Name of the season.


ANS:
class season
{
public static void main(String args[])
{
int month = 12;
String season;
switch (month)
{
case 12:
case 1:
case 2:
season = “Winter”;
break;
case 3:
case 4:
case 5:
season = “Spring”;
break;
case 6:
case 7:
case 8:
season = “Summer”;
break;
case 9:
case 10:
case 11:
season = “Autumn”;
break;
default:
season = “rain”;
}
System.out.println(“ ecember is in the “ + season + “.”);
}
}
OUTPUT:
ecember is in the Winter.

x-----x-----x-----x-----x-----x-----x-----x-----x-----x-----x-----
x-----x-----x-----x-----x-----x-----x-----x-----x-----x-----x-----
x-----x-----x-----x-----x-----x
THANK YOU

You might also like