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

SP23 BSE 022 - Labtask

The document contains code for 10 programming tasks. The tasks involve basics like calculating factorials, counting zeros in input, and finding maximum/minimum values. Loops and conditionals are used. Printing patterns like stars and calendars are also demonstrated.

Uploaded by

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

SP23 BSE 022 - Labtask

The document contains code for 10 programming tasks. The tasks involve basics like calculating factorials, counting zeros in input, and finding maximum/minimum values. Loops and conditionals are used. Printing patterns like stars and calendars are also demonstrated.

Uploaded by

Waleed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

LAB TASK 5

LAIBA ZAHOOR

SP23-BSE-022
Task-1:

import java.util.Scanner;

public class labtask2


{
public static void main(String[]args)
{
Scanner input = new Scanner(System.in);

System.out.print("Enter a number for factorial: ");


int num = input.nextInt();

int factorial = 1; // Initialize the factorial to 1

// Calculate the factorial using a for loop


for (int i = 1; i <= num; i++)
{
factorial *= i; // Multiply by the current number
}
System.out.println(factorial);
}
}
Task-3:
import java.util.Scanner;

public class labtask3


{
public static void main(String[]args)
{
Scanner input = new Scanner(System.in);

System.out.print("Enter the number of integers you will enter: ");


int num = input.nextInt();

int zeroes = 0;
System.out.println("Now enter the actual numbers: ");

for (int i = 0; i < num; i++)


{

int num2 = input.nextInt();


if (num2 == 0)
{
zeroes++;
}
}
System.out.println("Total number of zero digits are: "+zeroes);
}
}

Task-4:
import java.util.Scanner;

public class labtask4


{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
int countingVariable = 0;
System.out.print("Enter values(Entering a zero will terminate the code): ");

while(true)
{

int num = input.nextInt();


if (num == 0)
break;
countingVariable++;
}
System.out.println("Entered values are "+countingVariable + " in numbers.");
}
}

Task-5:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int max = Integer.MIN_VALUE; // Initialize max to the smallest possible integer

while (true) {
int num = scanner.nextInt();
if (num == 0) {
break; // End the loop when 0 is encountered
}

if (num > max) {


max = num; // Update max if the current number is greater
}
}

if (max != Integer.MIN_VALUE) {
System.out.println("The largest element is: " + max);
} else {
System.out.println("No numbers were entered.");
}
}
}
Task-6:
import java.util.Scanner;

public class labtask6 {


public static void main(String[]args)
{
Scanner scanner = new Scanner(System.in);
int max = Integer.MIN_VALUE; // Initialize max to the smallest possible
integer
int index = -1; // Initialize the index to -1

int currentIndex = 0; // Initialize the current index to 0

while (true) {
int num = scanner.nextInt();
if (num == 0) {
break; // End the loop when 0 is encountered
}

if (num > max) {


max = num; // Update max if the current number is greater
index = currentIndex; // Update the index of the largest element
}

currentIndex++; // Increment the current index


}

if (index != -1) {
System.out.println("Index of the largest element is: " + index);
} else {
System.out.println("No numbers were entered.");
}
}
}

Task-7:
import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int evenCount = 0;
while (true) {
int num = scanner.nextInt();
if (num == 0) {
break; // End the loop when 0 is encountered
}

if (num % 2 == 0) {
evenCount++; // Increment the even count if the current number is even
}
}

System.out.println("Number of even elements: " + evenCount);


}
}

Task-8:
import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int count = 0;
int prevNum = Integer.MIN_VALUE; // Initialize previous number to the
smallest possible integer

while (true) {
int num = scanner.nextInt();
if (num == 0) {
break; // End the loop when 0 is encountered
}

if (num > prevNum) {


count++; // Increment count if the current number is greater than the
previous one
}

prevNum = num; // Update the previous number


}

System.out.println("Number of elements greater than their neighbors above:


" + count);
}
}
Task-9:
a). public class Main {
public static void main(String[] args) {
int rows = 4; // Change the number of rows as needed

for (int i = 0; i < rows; i++) {


for (int j = 0; j < 10; j++) { // Print 10 asterisks in each row
System.out.print("*");
}
System.out.println(); // Move to the next line after printing each row
}
}
}

b).
public class Main {
public static void main(String[] args) {
int rows = 5; // Change the number of rows as needed
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}

c).
public class Main {
public static void main(String[] args) {
int rows = 5; // Change the number of rows as needed

for (int i = 1; i <= rows; i++) {


// Print spaces before the asterisks
for (int j = 1; j <= rows - i; j++) {
System.out.print(" ");
}

// Print asterisks
for (int k = 1; k <= i; k++) {
System.out.print("*");
}

System.out.println();
}
}
}

d).
public class Main {
public static void main(String[] args) {
int rows = 5; // Change the number of rows as needed

for (int i = 1; i <= rows; i++) {


// Print spaces before the asterisks
for (int j = 1; j <= rows - i; j++) {
System.out.print(" ");
}

// Print asterisks
for (int k = 1; k <= 2 * i - 1; k++) {
System.out.print("*");
}

System.out.println();
}
}
}

e).
public class Main {
public static void main(String[] args) {
int rows = 5; // Change the number of rows as needed

for (int i = 1; i <= rows; i++) {


// Print spaces before the numbers
for (int j = 1; j <= rows - i; j++) {
System.out.print(" ");
}

// Print numbers
for (int k = 1; k <= 2 * i - 1; k++) {
System.out.print(i);
}
System.out.println();
}
}

}
Task-10: import java.util.Scanner;

public class Calendar {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the year: ");


int year = scanner.nextInt();
System.out.print("Enter the first day of the year (1 for Sunday, 2 for Monday,
etc.): ");
int firstDay = scanner.nextInt();

for (int month = 1; month <= 12; month++) {


int daysInMonth = getDaysInMonth(month, year);
String monthName = getMonthName(month);

System.out.println("\n" + monthName + " " + year);


System.out.println("Sun Mon Tue Wed Thu Fri Sat");

// Print leading spaces for the first day of the month


for (int i = 1; i < firstDay; i++) {
System.out.print(" ");
}

for (int day = 1; day <= daysInMonth; day++) {


System.out.printf("%3d ", day);

// Move to the next line for the next week


if ((day + firstDay - 1) % 7 == 0 || day == daysInMonth) {
System.out.println();
}
}

// Calculate the first day of the next month


firstDay = (firstDay + daysInMonth) % 7;
}
}

// Function to get the number of days in a month


public static int getDaysInMonth(int month, int year) {
int[] daysInMonth = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (month == 2 && isLeapYear(year)) {
return 29;
}
return daysInMonth[month];
}

// Function to check if a year is a leap year


public static boolean isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}

// Function to get the name of a month


public static String getMonthName(int month) {
String[] monthNames = {"", "January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November", "December"};
return monthNames[month];
}
}
LAB TASK 10:
import java.util.Scanner;
public class labTask1 {
public static void main(String [] args){
Scanner input = new Scanner(System.in);

System.out.print("enter the integer for A " );


int A = input.nextInt();
System.out.print("enter the integer for B ");
int B = input.nextInt();
while(A<= B){
System.out.print(A + "," );
A++ ;
}
System.out.println(" ");
if(A< B){
for(int i = A; i <= B ; i++){
System.out.print( i + ",");
}
}
else{
for(int j = A ; j >= B ; j--){
System.out.print( j + ",");
}
}
System.out.print(" enter the integer for N " );
int N = input.nextInt();
int sum = 0 ;
for(int i = 1 ; i <= N ; i++){
sum += i ;
}
System.out.println( sum );
int sum1 = 0 ;
for(int i = 1 ; i <= N ; i++){
sum1 += i*i*i ;
}
System.out.println( sum1);
}
}

You might also like