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

COSC201101052 - Assignment 1

The document contains 12 Java programs that demonstrate the use of various loop constructs like while, do-while, for, nested loops. The programs include examples that count from 1 to 10, calculate factorials, find the maximum and minimum of a set of numbers, reverse a number, and print patterns using nested loops. Overall the programs show how to apply different loop types to solve common programming problems.

Uploaded by

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

COSC201101052 - Assignment 1

The document contains 12 Java programs that demonstrate the use of various loop constructs like while, do-while, for, nested loops. The programs include examples that count from 1 to 10, calculate factorials, find the maximum and minimum of a set of numbers, reverse a number, and print patterns using nested loops. Overall the programs show how to apply different loop types to solve common programming problems.

Uploaded by

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

Roll # : COSC201101052

Name : Shukran Maryam


Class : BS-COSC-2A
Course Code : COSC-1102
Course Title : Object Oriented Programming
Instructor : Sir Muizzud-din
Assignment : 01
Programs from Lab # 6

Program # 1:
Write a program that displays counting from 1 to 10, and from 10 to 1. Using
while loop.

public class DisplayCounting


{
public static void main(String args[])
{
int i = 1;

System.out.println("Counting from 1 to 10: ");


while(i<=10)
{
System.out.println(i);
i++;
}

int c = 10;

System.out.println("Counting from 10 to 1: ");


while(c>=1)
{
System.out.println(c);
c--;
}
}
}
Program # 2:
Write a program that displays counting from 1 to 5, and the sum of counting,
using while loop.

public class SumOfCounting


{
public static void main(String args[])
{
int c = 1;
int sum = 0;

System.out.println("Counting from 1 to 5: ");


while(c<=5)
{
System.out.println(c);
sum = sum + c;
c++;
}

System.out.print("Sum is: " + sum);


}
}
Program # 3:
Write a program that prints the square of all even numbers. From 2 to 20, using
do while loop.

public class SquareEvenNo


{
public static void main(String args[])
{
int c = 1;

System.out.println("Square of even numbers from 2 to 20: ");


do
{
if(c%2==0)
System.out.println("Square of " + c + " is " + c*c);
c++;
}
while(c<=20);
}
}
Program # 4;
Write a program that inputs a number from user and display a table of that
number, using for loop.

import java.util.Scanner;

public class TableForLoop


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

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


num = input.nextInt();
for(int c=1; c<=10; c++)
System.out.println(num + "*" + c + "=" + num*c);
}
}
Program # 5:
Write a program that gets starting and ending point from user, and displays all
odd numbers in the given range. Using do-while loop.

import java.util.Scanner;

public class OddNoInRange


{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int c, s, e;

System.out.print("Enter starting number: ");


s = input.nextInt();
System.out.print("Enter ending number: ");
e = input.nextInt();

c = s;
do
{
if(c%2!=0)
System.out.println(c);
c++;
}
while(c<=e);
}
}
Program # 6:
Write a program that gets two numbers from the user and displays the result
of first number raise to the power of second number using for loop.

import java.util.Scanner;

public class FirstNoRaisedToSecond


{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int a, b, c, r;

System.out.print("Enter first number: ");


a = input.nextInt();
System.out.print("Enter second number: ");
b = input.nextInt();

r=1;
for(c=1; c<=b; c++)
{
r = r*a;
}

System.out.print("Result is " + r);


}
}
Program # 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. Use for loop.

import java.util.Scanner;

public class NumbersLoop


{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int number, positive = 0, negative = 0, zero = 0;
char choice;

for(int a=1; a>0; a++)


{
System.out.println("Enter a number: ");
number = input.nextInt();
if (number > 0)
positive++;
else if (number < 0)
negative++;
else
zero++;
System.out.print("Do you want to continue(y/n)? ");
choice = input.next().charAt(0);
if(choice == 'y' || choice == 'Y')
continue;
else
break;
}

System.out.println("Positive numbers entered: " + positive++);


System.out.println("Negative numbers entered: " + negative++);
System.out.println("Zeroes entered: " + zero++);
}
}
Program # 8:
Write a program that gets input from the user until user enters -1, and display
the output. Use for loop.

import java.util.Scanner;

public class LoopNumbers


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

for(n=1; n!=-1; )
{
System.out.print("Enter a number: ");
n = input.nextInt();
System.out.println("You entered " + n);
}

System.out.print("End of Program..");
}
}
Program # 9:
Write a program to get an integer from a user and reverse it. This program
should assume that user inputs positive integer only. Use While loop.

import java.util.Scanner;

public class ReverseNum


{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int n, t, r;

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


n = input.nextInt();;
r = 0;
t = n;
while(t>0)
{
r = 10*r + t%10;
t = t/10;
}

System.out.println("Actual number: " + n);


System.out.println("Revered number: " + r);
}
}
Program # 10:
Write a program that inputs a number from user using for loop. If the number
is greater than 0, its sum is calculated and the next number is input. If the
number is less than or equal to 0 the loop exits. Use break statement.

import java.util.Scanner;

public class SumOfNum


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

for(x=1; x>0; x++)


{
System.out.print("Enter a number: ");
num = input.nextInt();;
if(num<=0)
break;
int sum = num + num;
System.out.println("Sum of number with itself: " + sum);
}
}
}
Program # 11:
Write a program that prints tables of numbers 1 to 5, using nested for loop.
Such that the output should be as:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50

public class DisplayTable


{
public static void main(String[] args)
{
int i,j;

for(i=1; i<=5; i++)


{
for(j=1; j<=10; j++)
{
System.out.print(i*j + " ");
}
System.out.print("\n");
}
}
}
Program # 12:
Write a program that prints the following shape, using nested for loop:
1
22
333
4444
55555

public class NestedShape


{
public static void main(String[] args)
{
int i, j, s;

for(i=1; i<=5; i++)


{
for(s=5-i; s>0; s--)
System.out.print(" ");
for(j=1; j<=i; j++)
System.out.print(i);
System.out.print("\n");
}
}
}
Programs from Lab # 7

Program # 1:
Write a program that inputs an integer (say n) from user, Then calculate the
sum of squares of integers up to n. (i.e., sum = 12 + 22 + 32 + … + n2).

import java.util.Scanner;

public class SumOfSquares


{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int n, c;
int sum=0;

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


n = input.nextInt();
for(c=1; c<=n; c++)
sum = sum + c;

System.out.print("Sum is: " + sum);


}
}
Program # 2:
Write a program to enter the numbers till the user wants and at the end it
should display the maximum and minimum number entered.

import java.util.Scanner;

public class MaxAndMinNo


{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int n, max=0, min=32767;
char choice;

for(int a=0;a>=0;a++)
{
System.out.print("Enter a number : ");
n = input.nextInt();

if(n>max)
max=n;
if(n<min)
min=n;

System.out.print("Do you want to Continue(y/n)? ");


choice = input.next().charAt(0);

if(choice=='y' || choice=='Y')
continue;
else
break;
}

System.out.println("Maximum Number: " + max);


System.out.print("Minimum Number : " + min);
}
}
Program # 3:
Write a program that inputs an integer from user, and calculates the factorial
of that integer. (factorial of n = 1*2*3*…*n)

import java.util.Scanner;

public class FactorialOfNo


{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int n, c, f;
f = 1;

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


n = input.nextInt();
for(c=1; c<=n; c++)
f = f * c;

System.out.print("Factorial of " + n + " is " + f);


}
}
Program # 4:
Write a program that inputs a number from the user using for loop. It displays
the number if it is greater than 0, otherwise it inputs another number using
continue statement. The loop will iterate 10 times.

import java.util.Scanner;

public class DisplayNoGreaterThanZero


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

for(x=1; x<=10; x++)


{
System.out.print("Enter a number: ");
num = input.nextInt();
if(num<=0)
continue;
System.out.println("You entered " + num);
}
}
}
Program # 5:
Write a program that inputs an integer (say n) from user, and prints Fibonacci
series up to n. The Fibonacci sequence is a series where the next term is the sum
of pervious two terms. (i.e., 0, 1, 1, 2, 3, 5, 8, 13, 21)

import java.util.Scanner;

public class Fibonacci


{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int n, t1=0, t2=1;

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


n = input.nextInt();

System.out.print("Upto " + n + ": ");


while (t1 <= n)
{
System.out.print(t1 + " + ");
int sum = t1 + t2;
t1 = t2;
t2 = sum;
}
}
}
Program # 6:
Write a program that declares an array of size 5. Input values in the array from
user, and then display all values of array.

import java.util.Scanner;

public class SimpleArray


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

System.out.println("Enter 5 values in array: ");


array[0] = input.nextInt();
array[1] = input.nextInt();
array[2] = input.nextInt();
array[3] = input.nextInt();
array[4] = input.nextInt();

System.out.println("Displaying entered values: ");


System.out.println(array[0]);
System.out.println(array[1]);
System.out.println(array[2]);
System.out.println(array[3]);
System.out.println(array[4]);
}
}
Program # 7
Write a program that inputs five integers from user and stores them in an
array. It then displays all values in the array by adding 1 in all values.

import java.util.Scanner;

public class ArrayWithIncrement


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

System.out.println("Enter 5 integers: ");


array[0] = input.nextInt();
array[1] = input.nextInt();
array[2] = input.nextInt();
array[3] = input.nextInt();
array[4] = input.nextInt();

System.out.println("Displaying entered integers with increment of 1: ");


for(int x=0; x<5; x++)
System.out.println(array[x]+1);
}
}
Program # 8
Write a program in C++ that takes age of five persons and then just display the
age of each person using arrays.

import java.util.Scanner;

public class ArrayAge


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

System.out.print("Enter age of 5 persons: ");


for(int x=0; x<5; x++)
age[x] = input.nextInt();

System.out.println("Age of 1st person: " + age[0]);


System.out.println("Age of 2nd person: " + age[1]);
System.out.println("Age of 3rd person: " + age[2]);
System.out.println("Age of 4th person: " + age[3]);
System.out.println("Age of 5th person: " + age[4]);
}
}
Program # 9:
Write a program that inputs five numbers in an array, and displays them in
actual and reverse order.

import java.util.Scanner;

public class ArrayReverse


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

System.out.println("Enter 5 numbers: ");


for(int x=0; x<5; x++)
array[x] = input.nextInt();

System.out.println("Actual Order: ");


for(int x=0; x<=4; x++)
System.out.print(array[x] + " ");
System.out.print("\n");

System.out.println("Reverse order: ");


for(int x=4; x>=0; x--)
System.out.print(array[x] + " ");
}
}
Program # 10:
Write a program that inputs five values from user, stores them in an array and
displays the sum, average of these values.

import java.util.Scanner;

public class ArraySumAvg


{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int sum = 0;
float average;
int[] array = new int[5];

System.out.println("Enter 5 numbers: ");


for(int x=0; x<5; x++)
array[x] = input.nextInt();

for(int x=0; x<5; x++)


sum = sum + array[x];
System.out.println("Sum of numbers: " + sum);
average = (float)sum/5;
System.out.print("Average: " + average);
}
}

You might also like