All Together.docx

  • Uploaded by: Somesh Dwivedi
  • 0
  • 0
  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View All Together.docx as PDF for free.

More details

  • Words: 4,197
  • Pages: 49
NAME : SOMESH DWIVEDI CLASS : IX SEC

: A

SUB : COMPUTER FILE TOPIC : JAVA PROGRAMS

PREFACE In today’s time we need to perform various lengthy calculations and various other tasks. Doing all this manually would consume a lot of time. In order to perform the tasks quickly and with more accuracy computer programs are developed.

In this file I have written programs in java programming language which is a popular Object Oriented Programming (OOP) language where we can create Applets(the programs downloaded from the net) but it’s is a case – sensitive programming language

The file consists of 30 Java programs made for verifying a range of numbers viz; Armstrong number, Palindrome number, Special number, Buzz number, for shops to calculate discounts, gifts to be given on purchase of items of various amounts, for displaying various types of series using different programming logic etc.

INDEX S.no.

Topic

Pg.no

1.

Front Page

1

2.

Preface

2

3.

Index

3

4.

Introduction

4

5

Java programs

5 to

6.

Acknowledgement

Remark

INTRODUCTION Java is an Object Oriented Programming language i.e. , it is a style of programming based on objects , classes and inheritance . An object is a software bundle of data and related operations. A class is a template of objects that consists of or that defines objects of a certain kind.

It was developed primarily by James Gosling at Sun Micro Systems (which has since been acquired by Oracle Corporation). In 1991, the Sun Microsystems developed a programming language to solve the problems of consumer electronics. Its most advantageous feature is that it is platform independent language. It has the capability to be transported from one computer to another .

It was first launched on 23 May 1995. Since then it has been used to write programs to solve complex real-life problems such as verification of numbers , varying discounts on various prices , etc. As of 2016, Java is most popular programming language in use , particularly for client-server web applications, with a reported 9 million developers.

JAVA PROGRAMS Program 1): Write a program to input 10 numbers and separately display the sum of even and odd numbers import java.util.*; public class sum_even_odd { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int i,n,esum = 0,osum = 0; for(i = 1 ; i<=10 ; i++) { System.out.println("Enter a number"); n = sc.nextInt(); if (n%2 == 0) esum =esum+n ; else osum = osum + n; } System.out.println("Sum of even numbers =" + esum); System.out.println("Sum of odd numbers" + osum); } }

Program 2): Write a program to input a number and display the new number after reversing the digits of the original number . The program also displays the absolute difference between original number and the reversed number .

import java.util.*; public class absolute_difference { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter a number"); int n = sc.nextInt();int i,d,sum=0; for (i = n ; i>0 ; i = i/10) { d =i%10 ; sum = sum*10 + d ; } int ab_diff = Math.abs(sum - n); System.out.println("Reversed number = "+sum); System.out.println("Absolute difference = "+ab_diff); } }

Program 3): Write a program to calculate the GCD or the Greatest Common Divisor of two numbers . import java.util.*; public class gcd { public static void main (String args[]) { Scanner sc= new Scanner(System.in); System.out.println("Enter two numbers "); int m =sc.nextInt(); int n =sc.nextInt(); int p = m * n;int i,hcf=0; for(i=1;i
Program 4): Write a program to enter ten numbers and display the smallest and greatest of them.

import java.util.*; public class greatest_smallest { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int i,n,max = 0; System.out.println("Enter a number "); n= sc.nextInt();int min = n; for (i=1;i<=9;i++) { System.out.println("Enter a number "); n= sc.nextInt(); if (n>max) max=n; else if (n<min) min = n; } System.out.println("Maximum number: " +max); System.out.println("Minimum number: " +min); } }

Program 5): Write a program to input a number and to display the first 7 multiples of that number. ( Example : 7*1=7 )

import java.util.*; public class first_7_multiples { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter a number"); int n = sc.nextInt();int i,a=0; for(i=1;i<=7;i++) { a = n*1; System.out.println(n+"*"+i+"="+a); } } }

Program 6): Write a program to check the number of digits in a number and display whether it contains odd number or even number of digits .

import java.util.*; public class number_even_odd { public static void main (String args[]) { Scanner sc = new Scanner (System.in); System.out.println("Enter a number"); int n = sc.nextInt(); int sum = 0; int i,d; for (i=n;i>0;i=i/10) { d=i%10; sum =sum +1 ; } System.out.println("The number of digits : "+ sum); if (sum%2==0) System.out.println("It contains even number of digits"); else System.out.println("It contains odd number of digits"); } }

Program 7): Write a program to check for an Armstrong number. ( A number is said to be an Armstrong Number if the sum of its digits’ cubes is same as the original number .)

import java.util.*; public class Armstrong_number { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter a number"); int n = sc.nextInt();int i,d,sum = 0; for(i = n;i > 0;i=i/10) { d= i%10; sum = sum + (d*d*d); } if(sum == n) System.out.println("It is Armstrong number"); else System.out.println("It is not an Armstrong number "); } }

Program 8): Write a program to check for a neon number . (A number is said to be a neon number if the sum of the digits of the number’s square is same as the original number . )

import java.util.*; public class neon_number { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter a number"); int n = sc.nextInt(); int nc = n*n;int d,i,sum = 0; for(i=n ; i>0 ; i=i/10) { d= i%10; sum = sum + d ; } if (sum == n) System.out.println("It is a neon number"); else System.out.println("It is not a neon number"); } }

Program 9): Write a program to check whether a number is a prime number or not . ( A number is said to be a Prime number if it has no factor other than 1 or the number itself . ) import java.util.*; public class prime_number { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter a number"); int n = sc.nextInt(); int i,c = 0; for(i =1 ; i<=n ; i =i+1) { if (n%i == 0) c =c +1 ; } if (c==2) System.out.println("It is a prime number"); else System.out.println("It is not a prime number"); } }

Program 10 ):Write a program to input a number check whether it is a special number or not . ( A number is a special number if the sum of the factorial of the digits of the number is same as the original number . ) import java.util.*; public class special_number { public static void main(String args[]) { Scanner sc= new Scanner(System.in); System.out.println("Enter a numnber "); int n = sc.nextInt();int i,d,f = 1,j,s = 0; for (i = n ; i>0 ; i = i/10) { d = i%10 ; f= 1; for (j =1 ; j<=d ;j=j+1) { f= f* j ; } s = s + f; } if (s == n) System.out.println("It is a special number "); else System.out.println("It is not a special number");

} }

Program 11): Write a program to enter a number and check whether it is a niven number of not . (A number is said to be a niven number if if it is divisible by the sum of its digits .)

import java.util.*; public class niven_number { public static void main (String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter a number"); int n = sc.nextInt();int i , d , sum = 0; for(i=n;i>0;i=i/10) { d= i% 10; sum=sum+d; } if (n%sum == 0) System.out.println("It is a niven number"); else System.out.println("It is not a niven number"); } }

Program 12): Write a program to input a number and check whether it is a perfect number or not . ( A number is said to be a perfect number if it is equal to the sum of its factors other than the number itself .) import java.util.*; public class perfect_number { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter a number"); int n = sc.nextInt();int i,sum = 0; for(i = 1 ; i
Program 13): Write a program to input a number of three digits and display the greatest of three numbers

import java.util.*; public class largest_3_digit_number { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter the three numbers of three digits each"); int n1 = sc.nextInt(); int n2 = sc.nextInt(); int n3 = sc.nextInt(); if (n1>n2) { if (n1>n3) System.out.println("The largest number: "+n1); else System.out.println("The largest number: "+n3); } else { if (n2>n3) System.out.println("The largest three digit number: "+n2); else System.out.println("The largest three digit number: "+n3); } } }

Program 14):Write a program to input a number the check whether it is a Palindrome number or not . ( A number is said to be a Palindrome number if it is read in reverse order is read same as read in correct order . )

import java.util.*; public class palindrome_number { public static void main (String args []) { Scanner sc = new Scanner(System.in); System.out.println("Enter a number"); int n = sc.nextInt();int i,d,sum = 0; for(i= n;i>0;i=i/10) { d = i%10; sum = sum*10+d; } if (sum==n) System.out.println("The number is Palindrome"); else System.out.println("The number is Palindrome"); } }

Program 15): Write a program to check whether a number is a buzz number or not . (A number is said to be a buzz number if either its last digit is seven or it is completely divisible by seven ).

import java.util.*; public class buzz_number { public static void main (String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter a number"); int n = sc.nextInt();int a = n%7 ; int b = n%10 ; if (a==0) System.out.println("It is a buzz number"); else if (b==7) System.out.println("It is a buzz number"); else System.out.println("It is not a buzz number"); } }

Program 16): Write a program to accept a number and check whether it is a “Spy Number” or not . (A number is a spy number if the sum of its digits equals the product of its digits)

import java.util.*; public class spy_number { public static void main (String args []) { Scanner sc = new Scanner(System.in); System.out.println("Enter a number : "); int n = sc.nextInt(); int i,p=1 ; int d,sum = 0 ; for (i=n;i>0;i= i /10) { d= i%10; sum= sum + d ; p = p*d ; } if (sum == p) System.out.println("It is a spy number"); else System.out.println("It is not a spy number"); } }

Program 17):Write a program to display the first 10 terms of the Fibonacci series ( Example : 0,1,1,2,3,5……. )

import java.util.*; public class fibonacci_series { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int a =0 ; int b = 1 ;int i; for (i=1 ; i<=5;i++) { System.out.print(a + " , "); System.out.print(b+ " , "); a=a+b; b=b+a; } } }

Program 18): Write a program to display the sum of the series : 1 3 5 7 19 + + + + ………… + 2 4 6 8 20

import java.util.*; public class fraction_series { public static void main(String args[]) { Scanner sc = new Scanner(System.in); double i,n,d,sum = 0.0; for(i=1.0;i<=19.0;i++) { d = i+1.0; sum = sum + i/d; } System.out.println("Sum of the series = "+ sum); } }

Program 19): Write a program to input a number and display its factorial

import java.util.*; public class factorial_number { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter a number"); int n = sc.nextInt(); int i,a =1; for (i=1 ; i<=n;i++) { a = a*i; } System.out.println("Factorial of a number: " + a); } }

Program 20): Using the switch-case statement find the area , perimeter and diagonal of a rectangle as per the user’s choice : The formulae : Area = Length * Breadth Perimeter= 2*(Length + Breadth) Diagonal = √𝑙𝑒𝑛𝑔𝑡ℎ2 + 𝑏𝑟𝑒𝑎𝑑𝑡ℎ2

import java.util.*; public class rectangle { public static void main (String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter 1 to calculate area"); System.out.println("Enter 2 to calculate perimeter"); System.out.println("Enter 3 to calculate diagnol"); int ch = sc.nextInt(); System.out.println("Enter length and breadth"); double l = sc.nextDouble(); double b = sc.nextDouble(); switch (ch) { case 1: double a = l*b ; System.out.println("Area = "+ a); break ; case 2: double p = (l+b)*2; System.out.println(" Perimeter = "+ p); break ; case 3: double d = Math.sqrt(l*l + b*b); System.out.println(" Diagnol = " + d); break ; default :

System.out.println("Wrong choice !! "); break ; } } }

Program 21): Using a switch statement, write a menu driven program to: (a) Generate and display the first 10 terms of the Fibonacci series (b) Find the sum of the digits of an integer entered by the user. import java.util.*; public class case_for { public static void main (String args[]) { Scanner sc =new Scanner(System.in); System.out.println("Enter 1 to display fibonacci series "); System.out.println("Enter 2 to find sum of all digits of the number the user shall enter "); int ch = sc.nextInt();int a,b,d,i=1 ;int sum = 0; switch (ch) { case 1: a=0;b=1; for (i =1 ; i<=5; i=i+1) { System.out.print(a +", "); System.out.print(b+", "); a=a+b; b=b+a; } break ; case 2: System.out.println("Enter a number"); int n =sc.nextInt(); for (i=n;i>0;i=i/10) { d=i%10; sum=sum+d; } System.out.println(" Sum of the digits : "+sum); break ; default :

System.out.println(" wrong choice !! "); break ; } } }

Program 22): Write menu – driven program to check whether (a) a number is a Prime number (b) a number is Armstrong number . An appropriate error message should be displayed for an incorrect choice .

import java.util.*; public class case_numbers { public static void main (String args []) { Scanner sc = new Scanner (System.in); System.out.println("Enter 1 to check for Prime number and 2 to check for Armstrong number"); int ch =sc.nextInt();int n ,d,i,sum = 0; switch (ch) { case 1: System.out.println("Enter a number"); n = sc.nextInt(); int a; for (i = 1 ; i<=n ; i++) { a = n%i; if (a == 0) sum= sum+1; } if (sum == 2) System.out.println("It is Prime Number"); else System.out.println("It is not a prime number"); break ; case 2 : System.out.println("Enter a number"); n=sc.nextInt(); for (i = n ; i>0 ; i = i/10) {

d = i%10; sum = (d*d*d) + sum ; } if (sum == n ) System.out.println("It is Armstrong number"); else System.out.println("It is not an Amstrong number "); break; default : System.out.println("Wrong choice !! "); } } }

Program 23): a library charges fine for books returned late .Following are the fines : Number of days Fine First 5 days 40 p per day th th 6 to 10 days 65p per day th More than 10 day 80p per day Design a class in java to calculate the fine assuming book to be returned 10 days late .

import java.util.*; public class library { public static void main (String args []) { Scanner sc = new Scanner(System.in); System.out.println("Enter the number of days the book is returned late :"); int n = sc.nextInt();double f=0,f1=0,f2=0,f3=0; if (n<=5) f = 0.40*5.0; else if ((n>6)&&(n<=10)) { f1 = 5.0 * 0.40; f2 = n-5.0 * 0.65; f= f1+f2; } else if (n>10) { f1 = 5.0 * 0.40; f2 = 5.0 * 0.65; f3 = n-10.0 * 0.80 ; f = f1 + f2 + f3 ; } System.out.println("The number of days book returnrd late : "+n); System.out.println("The fine to be paid = "+f); } }

Program 24): Write a program to calculate the tax on water consumed according to the following conditions : Water Consumed Tax Rate (in Gallons) (in Rs. ) Upto 60 NIL >=60 but 120 or less 300 >120 but 220 or less 500 >220 but 350 or less 800 >350 1200 Calculate the annual water tax based on the entered value of consumption. import java.util.*; public class water_tax { public static void main(String args[]) { Scanner sc =new Scanner(System.in); System.out.println("Enter water consumed in gallons "); double wc =sc.nextDouble(); double tx = 0 ; if (wc <=60) tx = 0.0; else if ((wc >= 60)&&(wc <=120)) tx = 300.0; else if ((wc>120)&& (wc<= 220)) tx = 500.0 ; else if ((wc >220)&&(wc<= 350)) tx = 800.0 ; else if (wc > 350) tx = 1200.0 ; System.out.println("Water consumed (in gallons) =" + wc); System.out.println("Amount to be paid = " + tx); } }

Program 25): ‘Kumar electronics ‘ has announced the following seasonal discounts on purchase of certain items Purchase Amount Discount on Laptop Discount on Desktop PC Upto Rs. 25000 0.0% 5.0% Rs. 25001 to Rs 50000 5.0% 7.5% Rs. 50001 to Rs 100000 7.5% 10.0% More than Rs. 100000 10.0% 15.0% Write a program to enter the amount of purchase and print the discount on laptop and desktop and also their desktop import java.util.*; public class kumar_electronics { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter amount of purchase = "); double a = sc.nextInt();double dl = 0.0,dd = 0.0; if(a<=25000.0) { dl = 0.0; dd = a*5.0/100.0; } if ((a>25000.0)&&(a<=50000.0)) { dl = a*5.0/100.0; dd= a*7.5/100.0; } if ((a>50000.0)&&(a<=100000.0)) { dl = a*7.5/100.0; dd = a*10.0/100.0; } if (a>100000) { dl = a*10.0/100.0; dd = a*15.0/100.0; }

System.out.println("Discount on Laptop = " + dl); System.out.println("Discount on Desktop = "+ dd); System.out.println("Amount of Laptop = "+ (a-dl)); System.out.println("Amount of Desktop = "+(a-dd)); } }

Program 26):A pre-paid taxi charges from the passengers as per the given tariff is given below : Distance Rate Upto 5 km 100 Rs. For the next 10 km 10 Rs/km For the next 10 km 8 Rs/km More than 25 km 5 Rs/km Write a program to to input the distance covered and calculate the amount to be paid by the passenger.

import java.util.*; public class taxi_charges { public static void main (String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter taxi number "); int txno = sc.nextInt(); System.out.println("Enter the distance travelled"); int d = sc.nextInt(); int a = 0; if(d<=5) a = 100; else if ((d>5)&&(d<=15)) a= d*10; else if ((d>15)&&(d<=25)) a = d*8; else if (d>25) a = d* 5; System.out.println("Taxi number = "+ txno); System.out.println("Distance travelled " + d); System.out.println("Amouint to be paid = " + a); } }

Program 27): A computerized ticket counter of an underground metro rail station charges for each ride at the following rate: Age Amount 18 or above Rs. 5 5 or above but below 18 Rs. 3 Accompanying kids below 5 NIL Write a java application to take number of peoples of various ages as inputs . At the end of the journey the program states the number of persons of various age groups and total amount received as fares . import java.util.*; public class metro { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter the number of passengers"); int np = sc.nextInt();int i,age,a=0,ab18 = 0,bt = 0,be5 = 0; for(i = 1;i <= np ; i ++) { System.out.println("Enter age : "); age = sc.nextInt(); if(age <= 18) { a=a+5; ab18 = ab18 + 1; } if ((age >= 5)&&(age < 18)) { a= a+5; bt = bt + 1; } if (age<5) be5 = be5 + 1; } System.out.println("Persons of age 18 years or above : " + ab18); System.out.println("Persons of age 5 years or above but below 18 years : " + bt);

System.out.println("Children below 5 yrs of age : "+be5); System.out.println("Total amount collected : "+ a); } }

Program 28):Develop a java class to computerize the billing operation of a telephone company : Number of calls Amount per call <=50 Free Next 100 calls 50p Next 150 calls 80p Rest of the calls Rs. 1.20 A rent of Rs. 120 is charged from every person . A tax of 15% is charged on the sum of the charges and rent . The total amount is added to the sum of charges and rent . Print the bill of the customer . import java.util.*; public class call_rates { public static void main (String args []) { Scanner sc = new Scanner(System.in); System.out.println("Enter customer's name : "); String name = sc.nextLine(); System.out.println("Enter number of calls"); int nc = sc.nextInt(); double ac = 0; if (nc <= 50) ac=0; else if (nc <= 150) ac = 0.50 * (nc-50); else if (nc <= 300) ac = 0.50 * 100 + 0.80 * (nc - 150); else if (nc > 300) ac = 0.50 *100 + 0.80 *(nc-150) + 1.20 * (nc - 300); double tc = ac*115/100 + 120; System.out.println("BILL :"); System.out.println("Name : " +name); System.out.println("Amount = "+ tc + " Rupees"); } }

Program 29 ): SBI Home finance revised its rate of interest for Public Deposits as follows: Years

Cumulative Interest Annual Income Source (Scheme(p.a.)) (compounded annually) 1 10% 2 10.5% 3 11.5% 11% 4 11.5% 11% 5 11.5% 11% Deposit under cumulative scheme is accepted for a period between 3 and 5 years only . Write a program in java to find : (a)Amount due for the sum invested under cumulative option scheme , by using formula A=P.(1+0.01r)^t (b)Interest for each year , under the annual interest scheme using the formula : I = .01 * P * R

import java.util.*; public class SBI { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter type of interest"); System.out.println("1 for Cumulative Interest"); System.out.println("2 for Annual Interest Scheme"); int ch = sc.nextInt();double p,a,r,i,t,ti = 0; switch (ch) { case 1 : System.out.println("Enter number of years"); int n = sc.nextInt(); if ((n > 2)) { System.out.println("Enter Principal ");

p = sc.nextDouble();r =11.5; a = p*Math.pow((1+(0.01*r)),2.0); System.out.println("Amount= " + a); } else System.out.println("Cumulative Interest is not for 1 or 2 years "); break ; case 2: System.out.println("Enter Principal and time "); p = sc.nextDouble();t =sc.nextDouble() ; if(t==1) { r=10; for (i=1;i<=t;i++) { i = p*r*1/100 ; ti = ti + i; } System.out.println("Amount = "+(p + ti)); } else if (t == 2) { r = 10.5; for (i=1;i<=t;i++) { i = p*r*1/100 ; ti = ti + i; } System.out.println("Amount = "+(p + ti)); } else if ((t == 3)||(t==4)||(t==5)) { r = 11; for (i=1;i<=t;i++) { i = p*r*1/100 ; ti = ti + i;

} System.out.println("Amount = "+(p + ti)); } else System.out.println("Years should not be more than 5 or in decimals "); break ; default : { System.out.println("Wrong choice"); } } } }

Program 30): A survey is conducted by a welfare association among the members of a locality to count the number of persons in favor and against the government policies . If the person favors the policy , he enters, 1 and if he is against , he enters 2 and if he has no opinion then he enters 0 ,WAP in java to count the number of persons I each category assuming the total number of persons to be 10 .

import java.util.*; public class government_policy { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int ch,i,y=0 , n=0 , nop = 0; System.out.println("Enter: 1 if u are in favour of it "); System.out.println(" 2 if you are against it "); System.out.println(" 0 if you have no opinion "); System.out.println("Enter according to the above mentioned criteria when asked below"); for (i = 1;i<=10;i++) { System.out.println("Enter your choice"); ch = sc.nextInt(); if (ch == 1) y = y+1 ; else if (ch==2) n = n+1; else if (ch == 0) nop = nop +1; else System.out.println("Wrong choice"); } System.out.println(" The number of persons in favour category: " + y); System.out.println("The number of persons in not favour category: "+n); System.out.println("The number of persons in can't say category: "+nop); } }

ACKNOWLEDGEMENT I would like express my special thank of gratitude to my respected teacher Mr. Asif Ismail , sir who not only taught us, how to write programs in Java programming language but also (for our summer break) gave us a golden opportunity to learn, practice and understand more about this programming language by giving 30 java programs to write as homework (for the more we practice, the more we learn).

Secondly I would also like to thank my parents and friends who gave me emotional support thus helping me to complete my project within the limited time frame.

I would also like to thank the authors of the following books, which helped me in completing my project: 1. UNDERSTANG COMPUTER APPLICATIONS WITH BLUEJ Mr. Vijay Kumar Pandey and Mr. Dilip Kumar Dey

by

2. TOGRTHER WITH COMPUTER APPLICATIONS Mrs. Aditi Grover

by

Related Documents

All
June 2020 28
All
July 2020 23
All
November 2019 40
All
May 2020 30
All
October 2019 40
All
October 2019 26

More Documents from ""

All Together.docx
June 2020 3
Documentation.docx
May 2020 3
Case A.docx
December 2019 8
Concept Of Km.docx
December 2019 4
Network Architecture
April 2020 25