0% found this document useful (0 votes)
28 views9 pages

Java Programs for Time Conversion and Series

The document contains 14 code snippets that solve various problems involving series calculations and number operations. The snippets take user input, perform calculations like summing series, finding smallest numbers, and checking properties of triangles. They output results such as sums, counts, and whether numbers or triangles meet certain conditions.

Uploaded by

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

Java Programs for Time Conversion and Series

The document contains 14 code snippets that solve various problems involving series calculations and number operations. The snippets take user input, perform calculations like summing series, finding smallest numbers, and checking properties of triangles. They output results such as sums, counts, and whether numbers or triangles meet certain conditions.

Uploaded by

Gagan Agrawal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Write a program to input a time in second's and convert it into Hours, Minute and Seconds and print the

results in following format. (E.g. If


the given time is 12035 seconds, then it will be printed as 3H: 20M: 35S)

import [Link].*;
class Convert_Time
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
[Link](“Enter time in seconds”);
int s=[Link]();
int h=s/3600;
s=s%3600;
int m=s/60;
s=s%60;
[Link](h+"H: "+m+"M: "+s+"S");
}
}

Write a program to input 3 unique integers and print the smallest among them.

import [Link].*;
class Smallest
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
int a,b,c;
[Link](“Enter 3 integers:”);
a=[Link]();
b=[Link]();
c=[Link]();
if(a<b && a<c)
[Link](“Smallest:”+a);
else if(b<a && b<c)
[Link](“Smallest:”+b);
else
[Link](“Smallest:”+c);
}
}
Write a program to input the three sides of a triangle and check whether it forms a triangle or not, if it forms a triangle, check whether it is
an equilateral, isosceles or a scalene triangle. (Hint: To form a triangle, each side should be less the sum of the other two sides)

import [Link].*;
class triangle
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
int a,b,c;
[Link](“Enter 3 sides:”);
a=[Link]();
b=[Link]();
c=[Link]();
if(a<b+c && b<a+c && c<a+b)
{
if(a==b && b==c)
[Link](“Equilateral triangle”);
else if(a==b || b==c || c==a)
[Link](“Isosceles triangle”);
else
[Link](“Scalene triangle”);
}
else
[Link](“Cannot form a triangle”);
}
}

A cloth showroom has announced the following festival discounts on the purchase of items, based on the total cost of the items
purchased:
Total Cost Discount (in Percentage)
Less than 2000 5%
2001 to 5000 25%
5001 to 10000 35%
Above 10000 50%
Write a program to input the total cost and compute and display the amount to be paid by the customer after availing the discount.

import [Link].*;
class Showroom
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
float tc,d,ap;
[Link](“Enter the total cost of the items:”);
tc=[Link]();
if(tc<=2000)
d=5/100f*tc;
else if(tc>=2001 && tc<=5000)
d=25/100f*tc;
else if(tc>=5001 && tc<=10000)
d=35/100f*tc;
else
d=50/100f*tc;
ap=tc-d;
[Link](“Amount Payable:”+ap);
}
}
Write a program to accept a number and then check whether first and last digit of the given number is same or not. Then print a message
either "Both end is same" or "End is not same". (Note: Program should check that the given number must have at least two digit, if not
then print a error message, otherwise solve the given problem).

import [Link].*;
class Digits
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
[Link](“Enter a number:”);
int n=[Link]();

if(n>9)
{
int lastd=n%10;
int d=0;
while(n>0)
{
d=n%10;
n/=10;
}
if(d==lastd)
[Link]("Both end is same");
else
[Link]("Both end is not same");
}
else
[Link]("Number must be atleast two digits");
}
}

Write a program to input any 50 numbers (including positive and negative).


Perform the following tasks:
(a) Count the positive numbers
(b) Count the negative numbers
(c) Sum of positive numbers
(d) Sum of negative numbers

import [Link];
public class Integers
{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
int pSum = 0, pCount = 0, nSum = 0, nCount = 0;
[Link]("Enter 50 numbers");
for (int i = 1; i <= 50; i++) {
int n = [Link]();
if (n >= 0) {
pSum += n;
pCount++;
}
else {
nSum += n;
nCount++;
}
}
[Link]("Positive Count = " + pCount);
[Link]("Positive Sum = " + pSum);
[Link]("Negative Count = " + nCount);
[Link]("Negative Sum = " + nSum);
}
}
Write a program to calculate the sum of all odd numbers and even numbers between a range of numbers from m to n (both inclusive)
where m < n. Input m and n (where m<n).

import [Link];
public class OddEvenSum
{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter m: ");
int m = [Link]();
[Link]("Enter n: ");
int n = [Link]();
long sumOdd = 0, sumEven = 0;

if (m > n) {
[Link]("m should be less than n");
}
else {
for (int i = m; i <=n; i++) {
if (i % 2 == 0)
sumEven += i;
else
sumOdd += i;
}

[Link]("Even Sum: " + sumEven);


[Link]("Odd Sum: " + sumOdd);
}
}
}

Write a program in Java to find the sum of the given series :


(a) 1 + 4 + 9 + ...... + 400

public class aSeries


{
public static void main(String args[]) {
int sum = 0;
for (int i = 1; i <= 20; i++)
{ sum += (i*i); }
[Link]("Sum = " + sum);
}
}
Write a program in Java to find the sum of the given series :
(b) 1 + (1/2) + (1/3) + ...... + (1/20)

public class bSeries


{
public static void main(String args[]) {
double sum = 0.0;
for (int i = 1; i <= 20; i++)
{ sum += (1.0 / i); }
[Link]("Sum = " + sum);
}
}
Write a program in Java to find the sum of the given series :
(c) 1 + (1/3) + (1/5) + ...... + (1/19)
public class cSeries
{
public static void main(String args[]) {
double sum = 0.0;
for (int i = 1; i <= 19; i = i + 2)
{ sum += (1.0 / i); }
[Link]("Sum = " + sum);
}
}
Write a program in Java to find the sum of the given series :
(d) (1/2) + (2/3) + (3/4) + ...... + (19/20)
public class dSeries
{
public static void main(String args[]) {
double sum = 0.0;
for (int i = 1; i <= 19; i++)
{ sum += (i / (double)(i + 1)); }
[Link]("Sum = " + sum);
}
}
Write a program in Java to find the sum of the given series :
(e) 2 - 4 + 6 - 8 + ...... - 20
public class eSeries
{
public static void main(String args[]) {
double sum = 0.0;
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0)
sum -= i * 2;
else
sum += i * 2;
}
[Link]("Sum = " + sum);
}
}
Write a program in Java to find the sum of the given series :
(f) (1*2) + (2*3) + ...... + (19*20)
public class fSeries
{
public static void main(String args[]) {
int sum = 0;
for (int i = 1; i <= 19; i++)
{ sum += i * (i + 1); }
[Link]("Sum = " + sum);
}
}
Write a program in Java to find the sum of the given series :
(g) f. 0, 3, 8, 15, 24, 35, …, 99
class gSeries
{
public static void main(String args[]) {
{
int i;
for(i=1;i<=10;i++)
{
[Link]((i*i-1)+“ ”);
}
}
}

Write a program in Java to find the sum of the given series :


h. 1, 2, 4, 7, 11, 16, 22, 29, …, upto n terms. [Take n as input]
import [Link].*;
class hSeries
{
public static void main(String args[])
{
int i,n,s=1;
Scanner sc=new Scanner([Link]);
[Link](“Enter the number of terms:”);
n=[Link]();
for(i=1;i<=n;i++)
{
[Link](s+“ ”);
}}}
Write a program in Java to find the sum of the given series :
(i) 2, 4, 8, 14, 22, 32, 44, 59, …, upto n terms. [Take n as input]

import [Link].*;
class iSeries
{
public static void main(String args[])
{
int i,n,s=2,c=2;
Scanner sc=new Scanner([Link]);
[Link](“Enter the number of terms:”);
n=[Link]();
for(i=1;i<=n;i++)
{
[Link](s+“ ”);
s=s+c;
c=c+2;
}
}

Write a program in Java to find the sum of the given series :


(j) 1, 2, 5, 10, 17, 26, 37, 50, …, upto n terms. [Take n as input]

import [Link].*;
class jSeries
{
public static void main(String args[])
{
int i,n,s=1,c=1;
Scanner sc=new Scanner([Link]);
[Link](“Enter the number of terms:”);
n=[Link]();
for(i=1;i<=n;i++)
{
[Link](s+“ ”);
s=s+c;
c=c+2;
}
}
}

Write a program in Java to find the sum of the given series :


(k) 1, 1, 2, 3, 5, 8, 13, 21, 34, …, upto n terms. [Take n as input]

import [Link].*;
class kSeries
{
public static void main(String args[])

{
int i,n,f=1,s=0,t;
Scanner sc=new Scanner([Link]);
[Link](“Enter the number of terms:”);
n=[Link]();
for(i=1;i<=n;i++)
{
t=f+s;
[Link](t+“ ”);
f=s;
s=t;
}
}
}
Write a program in Java to find the sum of the given series :
( l). 1, 2, 5, 12, 29, 70, 169, …, upto n terms. [Take n as input]
class Series
{
public static void main(String args[])
{
int i,n,f=1,s=0,t;
Scanner sc=new Scanner([Link]);
[Link](“Enter the number of terms:”);
n=[Link]();
for(i=1;i<=n;i++)
{
t=f+2*s;
[Link](t+“ ”);
f=s;
s=t;
}
}
}

Write a program in Java to find the sum of the given series :

(m) x/2 + x/5 + x/8 + x/11 +…+ x/20

[Link](“Enter the value of x:”);


x=[Link]();
for(i=2;i<=20;i+=3)
{ sum+=(float)x/i; }
[Link](“Sum:”+sum);

(n) To print the series 0, 3, 7, 15, 24 … n terms


[Link](“Enter the number of terms:”);
n=[Link]();
for(i=1;i<=n;i++)
{
s=i*i-1;
[Link](s+“ ”);
}

Write a program in Java to find the sum of the given series :

(0) 1 11 111 1111 11111


f=0;
for(i=1;i<=5;i++)
{
f=f*10+1;
[Link](f+“ ”);
}
Write a program to input an integer and find its factorial. Factorial of a number is the product
of all natural numbers till that number. For example factorial of 5 is 120 since 1×2×3×4×5=120.

import [Link].*;
class factorial
{
public static void main(String args[])
{
long i,n,f=1;
Scanner sc=new Scanner([Link]);
[Link](“Enter an integer:”);
n=[Link]();
for(i=1;i<=n;i++)
{
f=f*i;
}
[Link](“Factotrial:”+f);
}
}
Write a program to input an integer and check whether it is a prime number or not.
import [Link].*;
class primeornot
{
public static void main(String args[])

{
long i,n,c=0;
Scanner sc=new Scanner([Link]);
[Link](“Enter an integer:”);
n=[Link]();
for(i=1;i<=n;i++)
{
If(n%i==0)
c++;
}
If(c==2)
[Link](“Prime Number”);
else
[Link](“Not a Prime Number”);
}
}

Write a program to input two integers and find their Least Common Multiple(L.C.M).
For Example,
INPUT:
Enter 2 integers:
12
8
OUTPUT:
L.C.M. = 24
Ans. import [Link].*;
class LCM
{
public static void main(String args[])

{
Scanner sc=new Scanner([Link]);
int i,a,b,l=0;
[Link](“Enter 2 numbers:”);
a=[Link]();
b=[Link]();
for(i=a;i<=a*b;i++)
{
if(i%a==0 && i%b==0)
{
l=i;
break;
}
}
[Link](“L.C.M.:”+l);
}
}

Write a program to input two integers and find their Highest Common Factor(H.C.F).
For Example,
INPUT:
Enter 2 integers:
12
8
OUTPUT:
H.C.F. = 4
Ans.
import [Link].*;
class HCF
{
public static void main(String args[])

{
Scanner sc=new Scanner([Link]);
int i,a,b,l=0;
[Link](“Enter 2 numbers:”);
a=[Link]();
b=[Link]();
for(i=a;i<=a*b;i++)
{
if(i%a==0 && i%b==0)
{
l=i;
break;
}
}
[Link](“HCF.:”+(a*b)/l);
}
}
Write a program to input a number and check and print whether it is a Pronic number or not.
(Pronic number is the number which is the product of two consecutive integers)
Examples: 12 = 3 × 4
20 = 4 × 5
42 = 6 × 7

import [Link].*;
class PRONIC
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
int n,i,f=0;
[Link](“Enter a number:”);
n=[Link]();
for(i=1;i*(i+1)<=n;i++)
{
if(i*(i+1)==n)
{
[Link](“Pronic Number”);
f=1;
}
}
if(f==0)
[Link](“Not a pronic number”);
}
}

Common questions

Powered by AI

The program iterates through potential consecutive integer pairs, multiplying each pair to check if it matches the given number. It iterates so long as the product of the pair is less than or equal to the number. If a pair produces the number, it is labeled a pronic number. Otherwise, the program concludes it is not a pronic number .

The program reads 50 numbers, counts, and calculates the sum of positive and negative numbers separately. It maintains separate counters and accumulators for positive and negative numbers, incrementing the appropriate counter and updating the sum based on whether the number is positive or negative .

The program initializes a factor accumulator to 1. It reads an integer input and uses a for-loop, incrementing from 1 up to the input value. Within the loop, it multiplies the accumulator by the current iterator value. After the loop completes, the factorial is printed as the product of all integers up to the given input .

The program checks if the input number has at least two digits. If true, it calculates the last digit using modulo 10 and uses an iterative process dividing the number by 10 to isolate the first digit. It then compares these two digits and prints a message indicating whether they are the same or not .

The program divides the input time in seconds by 3600 to calculate hours. It then uses the remainder of this division to calculate minutes by dividing it by 60, and the final remainder gives the seconds. The results are printed in the format 3H: 20M: 35S .

The program calculates the sum of the series by iterating through each fraction term, adding reciprocals of integers from 1 to 20 (inclusive), accumulating the sum in a floating-point variable and prevents integer division errors by calculating `1.0 / i` where i varies from 1 to 20. The result is printed at the end .

The program first checks if the sum of any two sides is greater than the third side to determine if a valid triangle can be formed. If valid, it classifies the triangle as equilateral if all sides are equal, isosceles if any two sides are equal, and scalene if all sides are different .

The program compares the three input integers using a series of if-else statements. It first checks if the first number is smaller than the other two, then checks the second number against the other two, and defaults to the third number as the smallest if the other conditions are not met .

The program first checks that the starting value (m) is less than the ending value (n). It then iterates over the range, using conditional statements to check if each number is even or odd. Even numbers are added to the `sumEven` accumulator and odd numbers to the `sumOdd` accumulator. The sums are printed at the end .

The program applies discounts to the total cost based on specified ranges. If the total cost is less than 2000, it applies a 5% discount. For costs between 2001 to 5000, 25% is applied; for 5001 to 10000, 35%; and any amount above 10000 receives a 50% discount. The program calculates the discount amount and subtracts it from the total cost to display the amount payable .

You might also like