Java_prgms
4 subscribers
14 files
2 links
Share java programms
Download Telegram
Swap two numbers using temporary variable


public class SwapNumbers {

public static void main(String[] args) {

float first = 1.20f, second = 2.45f;

System.out.println("--Before swap--");
System.out.println("First number = " + first);
System.out.println("Second number = " + second);

// Value of first is assigned to temporary
float temporary = first;

// Value of second is assigned to first
first = second;

// Value of temporary (which contains the initial value of first) is assigned to second
second = temporary;

System.out.println("--After swap--");
System.out.println("First number = " + first);
System.out.println("Second number = " + second);
}
}
Check whether a number is even or odd using if...else statement


import java.util.Scanner;

public class EvenOdd {

public static void main(String[] args) {

Scanner reader = new Scanner(
System.in);

System.out.print("Enter a number: ");
int num = reader.nextInt();

if(num % 2 == 0)
System.out.println(num + " is even");
else
System.out.println(num + " is odd");
}
}
Check whether an alphabet is vowel or consonant using if..else statement


public class VowelConsonant {

public static void main(String[] args) {

char ch = 'i';

if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' )
System.out.println(ch + " is vowel");
else
System.out.println(ch + " is consonant");

}
}
7. Write a JavaScript program to display the current day and time in the following format.

<script>
var today = new Date();
var day = today.getDay();
var daylist=["Sunday"
,"Monday","Tuesday"
,"Wednesday","Thursday","Friday",
"Saturday"];

console.log("Today is : " + daylist[day] + ".");

var hour = today.getHours();
var minute = today.getMinutes();
var second = today.getSeconds();
var prepand = (hour >= 12)? " PM ":" AM ";
hour = (hour >= 12)? hour - 12: hour;

if (hour===0 && prepand===' PM ')
{
if (minute===0 && second===0)
{
hour=12;
prepand=' Noon';
}
else
{
hour=12;
prepand=' PM';
}
}
if (hour===0 && prepand===' AM ')
{
if (minute===0 && second===0)
{
hour=12;
prepand=' Midnight';
}
else
{
hour=12;
prepand=' AM';
}
}

console.log("Current Time : "+hour + prepand + " : " + minute + " : " + second);

</script>
Forwarded from JavaScript Tutorials
4. Program to change style of a text at runtime.

<script>
function chngstyle()
{
document.getElementById('div1').style.color = "Red"
document.getElementById('div1').style.backgroundColor = "yellow"
document.getElementById('div1').style.width = "100"
document.getElementById('div1').style.height = "100"

}
</script>

<div id="div1"> Welcome to all </div>

<input type="button" value="Click" onclick="chngstyle()">
Forwarded from JavaScript Tutorials
JavaScript Tutorials:
2. 2. Program to swap two strings.

<html>
<head>
<script> function swap()
{

var txt = document.getElementById('t1').value;
document.getElementById('t1').value = document.getElementById('t2').value; document.getElementById('t2').value = txt;

}

</script>
</head>
<body>

<input type="text" id="t1"> <input type="text" id="t2">
<br>
<input type="button" value="Swap" onclick="swap()">

</body>
</html>