0% found this document useful (0 votes)
29 views11 pages

Assignment 01 OOP

This document contains source code and output screenshots for 10 programming questions assigned as lab work. The questions cover topics like printing prime numbers between 1-100, checking if a number is even or odd, reversing elements in an integer array, finding duplicate elements in an array, merging two arrays, and checking if the sum of two integers equals a third integer. For each question, the source code, any inputs/outputs, and screenshots of the program running are provided.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
29 views11 pages

Assignment 01 OOP

This document contains source code and output screenshots for 10 programming questions assigned as lab work. The questions cover topics like printing prime numbers between 1-100, checking if a number is even or odd, reversing elements in an integer array, finding duplicate elements in an array, merging two arrays, and checking if the sum of two integers equals a third integer. For each question, the source code, any inputs/outputs, and screenshots of the program running are provided.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 11

COMSATS UNIVERSITY ISLAMABAD

(SAHIWAL CAMPUS)

Department of Computer Science

Subject: OOP(LAB)
Submitted to: M. SHAFQAT
Submitted by: ARIF AQEEL AHMAD
Reg. No: FA22-BCS-023
Date: 25 Sep. 2023
COMPUTER SCIENCE DEPARTMENT
Q:1: Print all prime numbers between 1—100
SOURCE CODE:
package labWork;

public class Assig_01 {

public static void main(String[] args) {


System.out.println("THIS IS QUESTION_01");

int i, j, factor;

final int num=100;

for (i=1 ; i<num ; i++) {

factor=0;

for(j =1; j<=i; j++) {

if(i %j==0) {

factor++;
}
}
if(factor==2)
{
System.out.println(i + " : is primes");

}
}

SCREENSHOT OF OUTPUT:-
Q2 :- Sum of all the prime numbers between 1—100;
SOURCE CODE :-
package labWork;

public class Assig_01 {

public static void main(String[] args) {


System.out.println("THIS IS QUESTION_02");

int i, j, factor;
int sum=0;

final int num=100;

for (i=1 ; i<num ; i++) {

factor=0;

for(j =1; j<=i; j++) {

if(i %j==0) {

factor++;
}
}
if(factor==2)
{

sum=sum+i;
}

}
System.out.println("SUM OF ALL PRIME NUMBERS BETWEEN 1--100 IS:” sum);
}

}
SCREENSHOT OF OUTPUT :-

Q3 :-Check given number is odd or even


SOURCE CODE:
package labWork;
import java.util.Scanner;

public class Check_even_odd {

public static void main(String[] args) {


System.out.println("CHECK EVEN AND ODD NUMBERS");

Scanner obje= new Scanner(System.in);//object is created

System.out.println("ENTER NUMBER: "); //show message to user


int num= obje.nextInt(); //taking number from user and storing in nu

if(num %2==0) {
System.out.println("GIVEN NUMBER IS EVEN");
}
else
System.out.print("GIVEN NUMBER IS ODD");

SCREENSHOT:
Q: Numbers divisible by 3 , 5 and both
SOURCE CODE:
package labWork;

public class Number {


public static void main( String a[]) {

final int number=100;

System.out.println("number is divisible by both 3 and 5: ");

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


if(i %3==0 && i%5==0) {
System.out.print(i + " , ");
}
}

System.out.println();

// Numbers which are divisible by 3 only

System.out.println("NUMBERS WHICH ARE DIVISBLE BY ONLY 3: ");

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


if(i %3==0) {
System.out.print(i+ " , " );
}
}

System.out.println();

// Numbers which are divisible by 5 only


System.out.println("NUMBERS WHICH ARE DIVISBLE BY ONLY 5: ");

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


if(i %5==0) {
System.out.print(i+ " , " );
}
}
}

SCREENSHOT:-

Q:- Convert seconds to minutes and hours


SOURCE CODE:
package labWork;
import java.util.Scanner;

public class Minute_hours {

public static void main(String[] args) {


int seconds;

//create object
Scanner second= new Scanner (System.in);
System.out.println("ENTER SECONDS: ");

seconds=second.nextInt();

//to convert into minutes:


int minutes= seconds /60;

System.out.println("NUMBER OF MINUTES IN GIVEN SECONDS ARE: "+


minutes);

//to convert into hours:

int hours=seconds/3600;
System.out.println("NUMBER OF HOURS IN GIVEN SECONDS ARE: "+ hours);
}

SCREENSHOT:

Q: Reverse the elements of integers Array


SOURCE CODE:-
package labWork;

public class Reverse_array {

public static void main(String[] args) {

int array[]={2,3,4,5,6,7,8,9};

System.out.println("ENTRIES OF ARRAY BEFORE REVERSING: ");


for(int i=0; i<array.length; i++) {
System.out.print(array[i] + " ");
}

System.out.println();

System.out.println("VALUES OF ARRAY AFTER REVERSING: ");

for(int i= array.length-1; i>=0; i--) {


System.out.print(array[i] +" ");
}

}
}

OUTPUT SCREENSHOTS

Q: Duplicate element in Array


SORUCE CODE:
package labWork;
import java.util.Scanner;
public class Duplicate_value {

public static void main(String[] args) {

Scanner objec=new Scanner(System.in);

int size;
System.out.println("TOTAL NUMBER OF ELEMENTS: ");

size=objec.nextInt();

int [] arra=new int[size];

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


System.out.println("VALUE AT "+i+ " index:");
arra[i]=objec.nextInt();
}

System.out.println("YOU HAVE ENTRED VALUES: ");


for(int i=0; i<size; i++) {
System.out.println(arra[i]+ " ");
}
//duplicate value
for(int i=0; i<size; i++) {

for(int j=i+1; j<size; j++) {


if(arra[i]==arra[j]) {
System.out.println("DUPLICATE VLUE IS: "+arra[j]);
}
}
}

SCREENSHOT:

Q:Merge two arrays:


package labWork;

public class Merge_01 {

public static void main(String[] args) {

int a[]= { 2,4,5,6,8 };

// second array
int b[] = { 10,12,23,34,45 };

// determining length of first array


int a1 = a.length;
// determining length of second array
int b1 = b.length;

// resultant array size


int c1 = a1 + b1;
// Creating a new array
int[] c = new int[c1];

for (int i = 0; i < a1; i = i + 1) {

c[i] = a[i];
}

// array into resultant array


for (int i = 0; i < b1; i = i + 1) {

// Storing the elements in the


// resultant array
c[a1 + i] = b[i];
}

for (int i = 0; i < c1; i = i + 1) {

// print the element


System.out.println(c[i]);
}
}

SCREENSHOT:

Q:Sum is equal to third integer


package labWork;
import java.util.Scanner;
public class Sum_of_two_integers {

public static void main(String[] args){

Scanner scanner = new Scanner(System.in);


System.out.println("Enter the first integer: ");

int a = scanner.nextInt();

System.out.println("Enter the second integer: ");


int b = scanner.nextInt();

System.out.println("Enter the third integer: ");


int c = scanner.nextInt();

System.out.print( "SUM IS EQUAL TO: " );


System.out.println(a+b);
if (a+b==c)
System.out.println( "sum of the first two integers equal to the third
integer "+"\n"+"true");

else
System.out.println("NOT EQAUL TO THIRD INTEGER");
}
}

SCREENSHOT:

THE END

You might also like