0% found this document useful (0 votes)
57 views8 pages

Calculate Average of Numbers Entered by User: Qus:-Write A Program

The document contains code snippets for several Java programs: 1) A program to calculate the average of numbers entered by the user. It uses arrays and loops to store input values and calculate the total and average. 2) A program to reverse a string entered by the user. It uses recursion to reverse the string character by character. 3) A program to find duplicate characters in a string. It uses a HashMap to store each character as a key and its count as value.

Uploaded by

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

Calculate Average of Numbers Entered by User: Qus:-Write A Program

The document contains code snippets for several Java programs: 1) A program to calculate the average of numbers entered by the user. It uses arrays and loops to store input values and calculate the total and average. 2) A program to reverse a string entered by the user. It uses recursion to reverse the string character by character. 3) A program to find duplicate characters in a string. It uses a HashMap to store each character as a key and its count as value.

Uploaded by

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

 Qus:- Write a program Calculate average of numbers

entered by user

import java.util.Scanner;
public class JavaExample {

public static void main(String[] args) {


System.out.println("How many numbers you want to enter?");
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
/* Declaring array of n elements, the value
* of n is provided by the user
*/
double[] arr = new double[n];
double total = 0;

for(int i=0; i<arr.length; i++){


System.out.print("Enter Element No."+(i+1)+": ");
arr[i] = scanner.nextDouble();
}
scanner.close();
for(int i=0; i<arr.length; i++){
total = total + arr[i];
}

double average = total / arr.length;

System.out.format("The average is: %.3f", average);


}
}

Output:-
How many numbers you want to enter?
5
Enter Element No.1: 12.7
Enter Element No.2: 18.9
Enter Element No.3: 20
Enter Element No.4: 13.923
Enter Element No.5: 15.6
The average is: 16.225
Qus: Write A program Program to reverse a string entered by
user

import java.util.Scanner;
public class JavaExample {

public static void main(String[] args) {


String str;
System.out.println("Enter your username: ");
Scanner scanner = new Scanner(System.in);
str = scanner.nextLine();
scanner.close();
String reversed = reverseString(str);
System.out.println("The reversed string is: " + reversed);
}

public static String reverseString(String str)


{
if (str.isEmpty())
return str;
//Calling Function Recursively
return reverseString(str.substring(1)) + str.charAt(0);
}
}

OUTPUT:-

Enter your username:


How are you doing?
The reversed string is: ?gniod uoy era who
Qus:-write a program to Java Program to find duplicate
Characters in a String

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Details {

public void countDupChars(String str){

//Create a HashMap
Map<Character, Integer> map = new HashMap<Character, Integer>();

//Convert the String to char array


char[] chars = str.toCharArray();

/* logic: char are inserted as keys and their count


* as values. If map contains the char already then
* increase the value by 1
*/
for(Character ch:chars){
if(map.containsKey(ch)){
map.put(ch, map.get(ch)+1);
} else {
map.put(ch, 1);
}
}

//Obtaining set of keys


Set<Character> keys = map.keySet();

/* Display count of chars if it is


* greater than 1. All duplicate chars would be
* having value greater than 1.
*/
for(Character ch:keys){
if(map.get(ch) > 1){
System.out.println("Char "+ch+" "+map.get(ch));
}
}
}

public static void main(String a[]){


Details obj = new Details();
System.out.println("String: BeginnersBook.com");
System.out.println("-------------------------");
obj.countDupChars("BeginnersBook.com");

System.out.println("\nString: ChaitanyaSingh");
System.out.println("-------------------------");
obj.countDupChars("ChaitanyaSingh");

System.out.println("\nString: #@$@!#$%!!%@");
System.out.println("-------------------------");
obj.countDupChars("#@$@!#$%!!%@");
}
}

Output:-
String: BeginnersBook.com
-------------------------
Char e 2
Char B 2
Char n 2
Char o 3

String: ChaitanyaSingh
-------------------------
Char a 3
Char n 2
Char h 2
Char i 2

String: #@$@!#$%!!%@
-------------------------
Char # 2
Char ! 3
Char @ 3
Char $ 2
Char % 2
Qus:-write a Java
Program to find GCD of two numbers
using for loop
public class GCDExample1 {

public static void main(String[] args) {

//Lets take two numbers 55 and 121 and find their GCD
int num1 = 55, num2 = 121, gcd = 1;

/* loop is running from 1 to the smallest of both numbers


* In this example the loop will run from 1 to 55 because 55
* is the smaller number. All the numbers from 1 to 55 will be
* checked. A number that perfectly divides both numbers would
* be stored in variable "gcd". By doing this, at the end, the
* variable gcd will have the largest number that divides both
* numbers without remainder.
*/
for(int i = 1; i <= num1 && i <= num2; i++)
{
if(num1%i==0 && num2%i==0)
gcd = i;
}

System.out.printf("GCD of %d and %d is: %d", num1, num2, gcd);


}

Output:- GCD of 55 and 121 is: 11


Qus: Write a java program to welcome the user using applet.

package Applet;

import java.applet.*;
import java.awt.*;

public class WelcomeToApplet extends Applet


{
// create paint class to print on the screen.
public void paint (Graphics g)
{
// Enter message with co-ordinates to make it in
centre.
g.drawString ("Welcome To java", 25, 50);

}
}

Output:-
Qus:- Write a Java program to draw a
rectangle using swing
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
class MyCanvas extends JComponent
{
public void paint(Graphics g)
{
g.drawRect (10, 10, 200, 200);
}
}
public class DrawRectangle
{
public static void main(String[] a)
{
JFrame window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setBounds(30, 30, 300, 300);
window.getContentPane().add(new MyCanvas());
window.setVisible(true);
}
}
Output:-

You might also like