Sem 5
Sem 5
//define limit
int limit = 50;
/*
Output of List Even Numbers Java Example would be
Printing Even numbers between 1 and 50
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38
40 42 44 46 48 50
*/
2
Program2 - Factorial of a number
/*
This program shows how to
calculate Factorial of a number.
*/
int number = 5;
/*
* Factorial of any number is! n.
* For example, factorial of 4 is 4*3*2*1.
*/
/*
Output of the Factorial program would be
Factorial of a number is 120
*/
3
Program3 - Compare Two Numbers using else-if
/*
*/ public class
CompareTwoNumbers { public
args) {
4
System.out.println(num1 + " is equal to " +
num2);
than 234
*/
5
public class
DetermineLeapYearExample {
2004;
/*
6
Output of the example
leap year
*/
7
System.out.println("Fibonacci Series upto
" + limit); for(int i=0; i< limit; i++){
System.out.print(series[i] + " ");
}
}
}
/*
Output of the Fibonacci Series Java Example would be
Fibonacci Series upto 20
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
2584 4181
*/
8
/*
* If the number is equal to it's reversed number, then
* the given number is a palindrome number.
*
* For ex,121 is a palindrome number while 12 is not.
*/
//reverse the number
while(number > 0){ temp =
number % 10;
number = number
/ 10;
reversedNumber = reversedNumber * 10
+ temp;
}
if(numbers[i] == reversedNumber)
System.out.println(numbers[i] + " is a palindrome");
else
System.out.println(numbers[i] + " not a palindrome
");
}
}
}
/*
Output of Java Palindrome Number Example would be
121 is a palindrome number
13 is not a palindrome number
34 is not a palindrome number
11 is a palindrome number
22 is a palindrome number
54 is not a palindrome number
*/
9
Program7- Generate prime numbers
between 1 & given number
/*
Prime Numbers Java Example
This Prime Numbers Java example shows how to
generate prime numbers between 1 and given
number using for loop.
*/
//define limit
int limit = 100;
10
}
/*
Output of Prime Numbers example would be
Prime numbers between 1 and 100
1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61
67 71 73 79 83 89 97
*/
*
**
***
****
*****
*/
11
}
}
}
/*
Output of the above program would be
*
**
***
****
*****
*/
Program9 – Reversed pyramid using for
loops & decrement operator.
/*
Java Pyramid 5 Example
This Java Pyramid example shows how to generate
pyramid or triangle like given below using for
loop.
12345
1234
123
12
1
*/
System.out.println("");
}
}
}
/*
*/
program.
*/
public class
NestedSwitchExample { public
13
static void main(String[]
args) {
/*
below example.
*/
int i = 0;
int j =
1;
switch(i)
case 0:
switch(j)
case 0:
System.out.println("i is 0, j is 0");
14
break;
case 1:
System.out.println("i is 0, j is 1");
break;
default:
System.out.println("nested
default case!!");
break;
default:
15
/* Output
would be, i
is 0, j is
*/
Program11 - Calculate Circle Area
using radius
/*
This program shows how to
calculate area of circle
using it's radius.
*/
import
java.io.BufferedReader;
import
java.io.IOException;
import
java.io.InputStreamRead
er;
16
Integer.parseInt(br.readLine(
));
}
//if invalid value was entered
catch(NumberFormatException ne)
{
System.out.println("Invalid radius value"
+ ne);
System.exit(0);
}
catch(IOException ioe)
{
System.out.println("IO Error :" + ioe);
System.exit(0);
}
/*
* Area of a circle is
* pi * r * r * where r is a radius
of a circle.
*/
/*
Output of Calculate Circle Area using Java Example
would be
Please enter radius of a circle
19
Area of a circle is 1134.1149479459152
*/
17
Program12 - Factorial of a number using recursion
/*
This program shows how to
calculate Factorial of a number
using recursion function.
*/
import
java.io.BufferedReader;
import
java.io.IOException;
import
java.io.InputStreamReader
;
18
if(b <= 1)
//if the number is 1 then return 1
return 1;
else
//else call the same function with the value -
1 return b * fact(b-1);
}
}
/*
Output of this Java example would be
Enter
the number:
5
Factorial of the number is: 120
*/
Program13 – pyramid of numbers using
for loops
/*
Exception{
19
BufferedReader keyboard = new
BufferedReader (new
InputStreamReader(System.in));
System.out.println("Enter Number:");
(keyboard.readLine());
System.out.println("Enter X:");
int x= Integer.parseInt
(keyboard.readLine());
int y = 0;
System.out.print(y
+ "\t"); y = y + x;
System.out.println("");
20
}
/* Output of this
example would be
Enter
Number:
Enter
X:
1 2
3 4 5
6 7 8 9
10 11 12 13 14
--------------------------------
-------------Enter Number:
21
5
Enter
X:
2 4
6 8 10
12 14 16 18
20 22 24 26 28
--------------------------------
--------------
Enter
Number:
Enter
X:
3 6
22
9 12 15
18 21 24 27
30 33 36 39 42
*/
Program14 – To Find Maximum of Two Numbers.
/*
Integer.parseInt(args[0]); int j
= Integer.parseInt(args[1]);
if(i > j)
"+j); else
"+i);
23
}
*/ class Minoftwo{
Integer.parseInt(args[0]);
int j =
Integer.parseInt(args[1]); int
result = (i<j)?i:j;
System.out.println(result+" is a
minimum value");
24
Program 16
/* Write a program that will read a float type value
from the keyboard and print the following output.
->Small Integer not less than the number.
->Given Number.
System.out.println("Given Number
: "+i);
i=1;i<=5;i++){
System.out.println((int)(Math.random()*100
));
}
}
case 9: case 8:
System.out.println("Excellent");
break; case 7:
26
System.out.println("Very Good");
break; case 6:
System.out.println("Good");
break; case 5:
System.out.println("Work Hard");
break; case 4:
System.out.println("Poor");
break; case 3:
case 2: case 1:
case 0:
System.out.println("Very Poor");
break; default:
27
class Sum_Product_ofDigit{
Integer.parseInt(args[0]);
"+num+" is : "+result);
28
Program 20 - Write a program to find
sum of all integers greater than 100
and less than 200 that are divisible
by 7
class SumOfDigit{ public static void
for(int i=100;i<=200;i++){
if(i%7==0) result+=i;
} System.out.println("Output of
Program is : "+result);
Input - 5
Output - 1 2 3 4 5 */
int num =
29
Integer.parseInt(args[0]);
for(int i=1;i<=num;i++){
System.out.println(result);
num = Integer.parseInt(args[0]);
System.out.println("*****MULTIPLICATION
TABLE*****"); for(int
i=1;i<=num;i++){ for(int
j=1;j<=num;j++){
30
}
System.out.print("\n");
Integer.parseInt(args[0]); int
num2 = Integer.parseInt(args[1]);
System.out.println("\n***Before
Swapping***");
System.out.println("Number 1 :
"+num1);
System.out.println("Number 2 :
31
num2;
System.out.println("\n***After
Swapping***");
System.out.println("Number 1 :
"+num1);
System.out.println("Number 2 : "+num2);
"+days+" days");
32
}
Input -
Output :
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1
*/ class InvertTriangle{
Integer.parseInt(args[0]);
33
for(int j=1;j<=num;j++){
System.out.print("\n");
num--; }
34
check = check +
(int)Math.pow(remainder,3); num
= num / 10;
if(check
== n)
System.out.println(n+" is an
Number");
Input - 124
35
try{ int num =
Integer.parseInt(args[0]);
check int
reverse=0,remainder;
}
String result=""; //contains the
switch(remainder){
case 0 :
result =
break; case 1 :
36
result =
break; case 2 :
result =
break; case 3 :
result =
break; case 4 :
result =
break; case 5 :
result =
break; case 6 :
result =
break;
37
case 7 :
result =
break; case 8 :
break; case 9 :
result =
break; default:
result="";
System.out.println(result);
}catch(Exception e){
System.out.println("Invalid Number
Format");
}
38
}
HarmonicSeries{
num--; }
is "+result);
39
Program 29 - Write a program to find
average of consecutive N Odd no. and
Even no.
class EvenOdd_Avg{ public static void
Integer.parseInt(args[0]); int
cntEven=0,cntOdd=0,sumEven=0,sumOdd=0;
n; } else{
} n--; } int
evenAvg,oddAvg; evenAvg =
is "+evenAvg);
is "+oddAvg);
40
}
2 3
4 5 6 7 8 9 10 ... N */ class
c=0; int n =
Integer.parseInt(args[0]);
if(c!=n){
c++;
System.out.print(c+" ");
} else
break loop1; }
System.out.print("\n");
41
}
for(int i=2;i<num;i++){
if(num%i==0) {
Number"); flag = 1;
break; } }
if(flag==0)
42
Example :
Integer.parseInt(args[0]); int n =
n) System.out.println(n+" is a
Number");
}
Program 33 - Display Triangle as
follow
43
0
1 0
1 0 1 0 1 0 1 */ class Output2{
for(int i=1;i<=4;i++){
for(int j=1;j<=i;j++){
System.out.print(((i+j)%2)+" ");
System.out.print("\n");
44
Program 34 Display odd
numbers between1--100
class OddNumber {
public static void main(String args[]) {
System.out.println("The Odd Numbers
are:"); for (int i = 1; i <= 100; i++) {
if (i % 2 != 0) {
System.out.print(i + " ");
}
}
}
}
class SumOfNum
{
public static void main(String args[])
{ int
sum = 0;
for (int i = 1; i <= 100; i++)
{
if (i % 2 != 0)
{
45
sum = sum + i;
}
}
System.out.println("The Sum Of 100 Odd Numbers are:" + sum);
}
}
class TotalNumOfOddNum
{
public static void main(String args[])
{ int count = 0;
for(int i = 1;i <= 100;i++)
{
if(i % 2 != 0)
{
count++;
}
}
System.out.println("The Count Of Odd Numbers are:" + count);
}
}
46
Program 36 Find sum of first n
numbers
class SumOfNum
{
public static void main(String args[])
{ int
sum = 0; int
n=10;
for(int i = 1;i <= n;i++)
{
sum = sum + i;
}
System.out.println("The Sum Of "+n+" Numbers are:" + sum);
}
}
47
while (num > 0)
{
rem = num % 10;
sum = sum + rem;
num = num / 10;
}
48
}
else
bill = units * 6;
}
else
bill = units * 5;
}
n = 153; // input
value arg = n;
for (int i = 1; i < n; i++)
{
while (n > 0)
49
{
r = n % 10;
sum = sum + (r * r * r);
n = n / 10;
}
if (arg == sum)
{
System.out.println("Given number is armstrong number: " + arg);
}
else
{
System.out.println("Given number is not armstrong number: " +
arg);
}
}
50
{
if (sum == i)
{
System.out.print(i + " ");
}
sum = 0;
}
51
}
}
Program 41 Print
given number in
words
public class NumberToWords
{
String ten[] = { " ", " ", " Twenty", " Thirty", " Forty", " Fifty", " Sixty", "Seventy",
" Eighty", " Ninety" };
if (n > 19)
{
System.out.print(ten[n / 10] + " " + one[n % 10]);
}
else
{
52
System.out.print(one[n]);
}
if (n > 0)
System.out.print(ch);
}
int n=28;
System.out.print(n);
if (n <= 0)
{
System.out.println("Enter numbers greater than 0");
}
else
{
NumberToWords a = new NumberToWords();
a.pw((n / 1000000000), " Hundred");
a.pw((n / 10000000) % 100, " crore");
a.pw(((n / 100000) % 100), " lakh");
a.pw(((n / 1000) % 100), " thousand");
a.pw(((n / 100) % 10), " hundred");
a.pw((n % 100), " ");
}
}
53
Program 42 to check the given
number is Palindrome or not
public class PalindromeNumberCheck
{
while (n > 0)
{
r = n % 10;
rev = rev * 10 + r;
n = n / 10;
if (rev == pal)
{
System.out.println(" The given no is palindrome "+ rev);
}
else
{
System.out.println("The given no is not palindrome " + rev);
54
}
55
{
System.out.print(i + " ");
}
rev = 0;
}
}
Program 44 to print N prime numbers
and find sum and average
public class PrimeNumberUptoN
{
Program 46
*
**
***
****
*****
}
k=k-
2;
for(j=0; j<=i; j++)
{
58
System.out.print("* ");
}
System.out.println();
}
Program 47
***************
******* *******
****** ******
***** *****
**** ****
*** ***
** **
* *
* *
** **
*** ***
**** ****
***** *****
****** ******
******* *******
59
***************
60
{
continue;
}
System.out.print("*");
}
p--;
System.out.println();
}
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
System.out.print("*");
}
for(k=q*2-2;k>1;k--)
{
System.out.print(" ");
}
for(m=i;m!=0;m--)
{
if(m==n)
{
continue;
}
61
System.out.print("*");
}
System.out.println();
q--;
}
Program 48
Print Floyds
triangle
import
java.util.Scanner;
class FloydsTriangle
{
public static void main(String args[])
{
62
{
for ( int j = 1 ; j <= i ; j++ )
{
System.out.print(count+" ");
count++;
}
System.out.println(); 53
2 58
456 789 Error! Bookmark not defined.
11 12 13 14 Error! Bookmark not defined.
}
}
}
1
16 17 18 19 20 21
63
Program 49 to remove duplicate
words in given string
public class RemoveDuplicate
{
for(int j=i+1;j<words.length;j++)
{
if(words[i].equals(words[j]))
{
words[j]=null;
}
}
}
}
for(int k=0;k<words.length;k++)
64
{
if(words[k]!=null)
{
System.out.println(words[k]);
}
}
Program 50 to count each words and total
number of words in given string
import java.io.IOException;
65
String[] words =
st.split("\\s"); int[] fr = new
int[words.length];
for (int i = 0; i < fr.length; i++)
fr[i] = 0;
for (int i = 0; i < words.length; i++)
{
for (int j = 0; j < words.length; j++)
{
if (words[i].equals(words[j]))
{
fr[i]++;
}
}
}
66
words[i] = "";
}
}
}
int total = 0;
System.out.println("Words and words
count:"); for (int i = 0; i < words.length; i++)
{
if (words[i] != "")
{
System.out.println(words[i] + "=" + fr[i]);
total += fr[i];
}
}
System.out.println("Total words counted: " + total);
}
}
67
}
68