Java program 4
1. Write a program to display multiplication table upto given number.
package anything;
import java.util.Scanner;
public class pattern {
public static void main(String[] args) {
int n,i,j;
Scanner sc=new Scanner (System.in);
System.out.println("Enter number;");
n=sc.nextInt();
for(i=1;i<=n;i++)
{
for(j=1;j<=10;j++)
System.out.println(i+"x"+j+"="+i*j);
}
}
}
2. Write a program to display following pattern
*
* *
* * *
* * * *
package anything;
import java.util.Scanner;
public class pattern {
public static void main(String[] args) {
int i,j;
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
System.out.print("* ");
System.out.println();
}}}
3. Write a program to display following pattern
* * * *
* * *
* *
*
package anything;
import java.util.Scanner;
public class pattern {
public static void main(String[] args) {
int i,j;
for(i=1;i<=4;i++)
{
for(j=i;j<=4;j++)
System.out.print("* ");
System.out.println();
}
}}
4. Write a program to display following
1
1 2
1 2 3
1 2 3 4
package anything;
import java.util.Scanner;
public class pattern {
public static void main(String[] args) {
int i,j;
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
System.out.print(j+" ");
System.out.println();
}}}
5. Write a program using while loop to print multiplication table of given number
package anything;
import java.util.Scanner;
public clas s tablewhile {
public static void main(String[] args) {
int i=1,n,r;
Scanner sc=new Scanner (System.in);
System.out.println("Enter number and range");
n=sc.nextInt();
r=sc.nextInt();
while(i<=r)
{
System.out.println(n+"x"+i+"="+n * i);
i++;
}}}
6. Write a program to compute sum of cube of the each individual
package anything;
import java.util.Scanner;
public class sum_cube {
public static void main(String[] args) {
int s,n,p=0;
Scanner sc=new Scanner (System.in);
System.out.println("Enter number: ");
n=sc.nextInt();
while(n!=0)
{
s=n%10;
p=p+s*s*s;
n=n/10;
}
System.out.println("Sum of cube of each number is: "+p);
}
}
7. Write a program find whether given number is Armstrong or not
Example: 322= 3³ + 2³ + 2³
= 27+8+8
= 43 so it is not an Armstrong
package anything;
import java.util.Scanner;
public class pattern {
public static void main(String[] args) {
Double s,n,p=0;
Scanner sc=new Scanner (System.in);
System.out.println("Enter number and range");
n=sc.nextDouble();
r=sc.nextDouble();
double temp=n+0;
while(n!=0)
{
s=n%10;
p=p+Math.pow(s,r);
n=n/10;
}
if(p==temp)
System.out.println("Given number "+temp+" is Armstrong");
else
System.out.println("Given number "+temp+" is not an Armstrong");
}
}
I am not responsible if any thing goes wrong due to this post.
If anyone are not able to understand these above codes then fell free to call.
If you directly copy and paste these code in your ide then it will give error, to avoid error first write this code import java.util.*; at very first line then copy the after public static void main
0 Comments