Tested Java Programs

Just another WordPress.com weblog

Program to find sum,average,minimum and maximum of N numbers using input through dialog box

/* Program to find sum,average,minimum and maximum of N numbers using input through dialog box */

import java.io.*;
import javax.swing.*;
class prog_1c
{
public static void main(String args[])throws IOException
{
int k,sum=0,i,count=0,max,min,n;
int arr[] = new int[10];
float avg;
k=Integer.parseInt(JOptionPane.showInputDialog(“Enter how many numbers”));

for(i=0;i<k;i++)
{
n=Integer.parseInt(JOptionPane.showInputDialog(“Enter the number”));
arr[i] = n;
}
max = arr[0];
min = arr[0];
while(count < k)
{
sum += arr[count];
if(min > arr[count])
min = arr[count];
if (max < arr[count])
max = arr[count];
count += 1;
}
avg = sum/k;
JOptionPane.showMessageDialog(null,”The sum is “+sum+” The average is ”
+avg+” Maximum is ” +max+ ” Minimum is ” +min);
System.exit(0);
}
}

August 4, 2009 Posted by | Uncategorized | Leave a comment

Program to find sum,average,minimum and maximum of N numbers using command line arguments

/* Program to find sum,average,minimum and maximum of N numbers using command line arguments */

import java.io.*;
class prog_1a
{
public static void main(String args[])
{
int sum=0,i,count=0,max,min,n;
float avg;
if (args.length == 0)
System.out.println(“Argument is expected “);
else
{
i = args.length;
max = Integer.parseInt(args[0]);
min = Integer.parseInt(args[0]);
while(count < i)
{
n = Integer.parseInt(args[count]);
sum += n;
if(min > n)
min = n;
if (max < n)
max = n;
count += 1;
}
avg = sum/i;
System.out.println(“The sum is ” +sum);
System.out.println(“The average is ” +avg);
System.out.println(“Maximum is ” +max);
System.out.println(“Minimum is ” +min);
}
}
}

August 4, 2009 Posted by | Uncategorized | Leave a comment

How to check if a given number is Armstrong number ?

/* To check if a given number is Armstrong number */
import java.io.*;
class prog_9 {
public static void main(String argv[])throws IOException
{
int k=0;
int i=0,num,divisor=1,k1,quo,sum=0;
System.out.println(“To check if a given number is Armstrong number”);
System.out.println(“Enter the number : “);
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
String str;
str=br.readLine();
i = str.length();
k = Integer.parseInt(str);
k1=k;
for(num=1;num<i;num++)
divisor = divisor*10;
for(num=0;num<i;num++)
{
quo=k/divisor;
sum=sum+(quo*quo*quo);
k=k%divisor;
divisor=divisor/10;
}
if(sum==k1)
System.out.println(“Number is Armstrong number”);
else
System.out.println(“Number is not Armstrong number”);
}
}

August 4, 2009 Posted by | Uncategorized | 1 Comment