0% found this document useful (0 votes)
2 views

Java_Assignment(Questions 1 - 8)

The document contains eight Java programs that perform various tasks, including printing numbers, calculating sums, finding factorials, checking prime numbers, and identifying maximum and minimum values from user input. Each program is encapsulated in a class and utilizes loops and conditionals to achieve its functionality. Additionally, there is a program that generates a specific pattern of numbers.

Uploaded by

anujabagade359
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java_Assignment(Questions 1 - 8)

The document contains eight Java programs that perform various tasks, including printing numbers, calculating sums, finding factorials, checking prime numbers, and identifying maximum and minimum values from user input. Each program is encapsulated in a class and utilizes loops and conditionals to achieve its functionality. Additionally, there is a program that generates a specific pattern of numbers.

Uploaded by

anujabagade359
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

1] Write a program to print number from 1 to 10.

package Assignment;

public class Question1 {

public static void main(String[] args) {


for (int a = 1; a <= 10; a++) {
System.out.println(a);
}

}
===================================================================================
==============
2] Write a program to calculate the sum of first 10 natural number.

package Assignment;

public class Question2 {

public static void main(String[] args) {


int sum = 0;
for (int a = 1; a <= 10; a++) {
sum += a;
}
System.out.println(sum);
}

}
===================================================================================
================
3] Write a program to find the factorial value of any number entered through the
keyboard.

package Assignment;
import java.util.Scanner;

public class Question3 {

public static void main(String[] args) {


int number;
int fact=1;
Scanner sc=new Scanner(System.in);
System.out.println("Enter number");
number=sc.nextInt();
while(number>0)
{
fact=fact*number;
number--;
}
System.out.println("factoial is " + fact);
}

}
===================================================================================
=================
4] Two numbers are entered through the keyboard. Write a program to find the value
of one number raised to the power of another.

package Assignment;
import java.util.Scanner;
import java.math.*;

public class Question4 {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(Math.pow(a, b));

}
===================================================================================
===================
5] Write a program to sum of digits of given integer number.

package Assignment;
import java.util.Scanner;
import java.math.*;

public class Question5 {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int sum = 0;
while (n > 0)
{
int a = n % 10;
sum = sum + a;
n = n / 10;

}
System.out.println("Sum of digit is: " + sum);

}
===================================================================================
=====================
6] Write a program to check given number is prime or not.

package Assignment;
import java.util.Scanner;

public class Question6 {

public static void main(String[] args) {


int counter=0;
Scanner sc=new Scanner(System.in);
System.out.println("enter number");
int no=sc.nextInt();
for(int i=1;i<=no;i++)
{
if(no%i==0)
{
counter++;
}
}
if(counter==2)
{
System.out.println("Number is prime :");
}
else
{
System.out.println("Number is not prime :");
}
}

}
===================================================================================
========================
7] Write a program to enter the numbers till the user wants and at the end it
should display the count of positive, negative and zeros entered.

package Assignment;
import java.util.Scanner;

public class Question7 {

public static void main(String[] args) {


Scanner sc=new Scanner(System.in);
System.out.println("How many numbers you want to add");
int size=sc.nextInt();
int counterZero=0;
int counternegative=0;
int counterpositive=0;
System.out.print("Enter the number");
for(int i=1;i<=size;i++)
{

int num=sc.nextInt();
if(num==0)
{
counterZero++;
}
else if(num<0)
{
counternegative++;
}
else
{
counterpositive++;
}
}
System.out.println("no of zeros:"+counterZero);
System.out.println("no of negative numbers: " + counternegative);
System.out.println("no of positive numbers: " + counterpositive);

}
===================================================================================
==========================
8] Write a program to enter the numbers till the user wants and at the end it
should display the maximum and minimum number entered.

package Assignment;
import java.util.Scanner;
public class Question8 {

public static void main(String[] args) {


Scanner sc=new Scanner(System.in);
System.out.println("How many number you want to add");
int size=sc.nextInt();
int []a=new int[size];
System.out.print("Enter the number");
for(int i=0;i<size;i++)
{
a[i]=sc.nextInt();
}
int max=a[0];
int min=a[0];
System.out.println("======");
for(int i=1;i<size;i++)
{
if(max<a[i])
{
max=a[i];
}
}
System.out.println("Maximum value is " + max);

System.out.println("======");
for(int i=1;i<size;i++)
{
if(min>a[i])
{
min=a[i];
}
}
System.out.println("Minimum value is " + min);
}

}
===================================================================================
============================
Get the following pattern:-
C]1

11

111

1111

11111

package Assignment;
public class QuestionC {

public static void main(String[] args) {


int size=5;
for(int i=1;i<=size;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print("1");
}
System.out.println();
}
}

You might also like