Today We Practised Some more Programs related to the Loops.The programs are as following:
Count the Digits in a Number.
Print the Armstrong Number Series between 1 to 500
Find digit Place in a Number
Factorial of a number
Sum of Factorial Series.
Program to Count the Digits in a Number.
import javax.swing.*;
public class countdigits {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int num,count;
count = 0;
num= Integer.parseInt(JOptionPane.showInputDialog("Enter the number"));
while (num !=0)
{
count ++;
num = num /10;
}
System.out.print("the no. of digits is " +count);
}
}
Program to Print the Armstrong Number Series between 1 to 500
public class armstrong
{
/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
int a,c,d;
int b;
for (a=1;a<500;a++){
b=a;
c=b%10;
b=b/10;
d=b%10;
b=b/10;
if (a == (b*b*b)+(c*c*c)+(d*d*d))
{
System.out.println(a);
}}}}
Program to Find digit Place in a Number
import javax.swing.*;
public class digitplace {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int num,dig,loc=0,tmp, count = 0;
num= Integer.parseInt(JOptionPane.showInputDialog("Enter the number"));
dig= Integer.parseInt(JOptionPane.showInputDialog("Enter the digit whose location you want to find"));
tmp = num;
while (num !=0)
{
count ++;
num = num /10;
}
while (count != 0)
{
if (tmp % 10 == dig)
{
loc = count;
}
tmp = tmp /10;
count --;
}
if (loc == 0)
{System.out.println("the entered digit is not present ");}
else{
System.out.println("the location is "+loc);
}
}
}
Program to find Factorial of a Number
import javax.swing.*;
public class factorial {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int num,fact,c;
fact = 1;
num = Integer.parseInt(JOptionPane.showInputDialog("enter the number for which you want to calculate the factoral"));
for(c=1;c<=num;c++)
{ fact = fact*c;
}
System.out.print("the factorial is "+fact);
}
}
Program to Calculate Sum Of Factorial Series.
import javax.swing.JOptionPane;
public class sumoffact {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int num,fact,c,sum;
fact = 1;
sum = 0;
num = Integer.parseInt(JOptionPane.showInputDialog("enter the number for which you want to calculate the factoral"));
for(c=1;c<=num;c++)
{ fact = fact*c;
sum = sum +fact;
}
System.out.print("the sum of factorial is "+sum);
}
}
0 comments:
Post a Comment