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

Lab Report # 3

Uploaded by

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

Lab Report # 3

Uploaded by

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

1 lab task # 3

Lab Report No 3
OOP

Submitted By:
Abdullah (010)
Muhammad Ahmad (035)
Ahmed Abdullah (054)

Submitted to:
Miss Sibgha Batool

Dated:
Week 03

Department of Computer Science,


HITEC University, Taxila
Lab Task No 1
Write a program that will used ternary operator and enter the age of a person
and print the person is a child or an adult.
Solution:
Brief description (3-5 lines)
 In this program, we have used ternary operator in print statement to print adult or child
 Syntax of ternary operator is
(condition)? Expression1:Expression2 ;
The code

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("enter ur age:");
int age = sc.nextInt();
System.out.println((age>=18)?"adult":"child");
}
}

The results (Screenshot)

2
Lab Task No 2
Write a program in C++ to input marks obtained by a student in a subject. The
total marks are 100. Find out the grade of the student by using the if-else
nested structure. The grade is:
If marks are equal to or greater than 90, grade is A.
If marks are equal to or greater than 70 and less than 90, grade is B.
If marks are equal to or greater than 50 and less than 70, grade is C.
If marks are less than 50, grade is “F”.
Solution:
Brief description (3-5 lines)
 In this program, we have used the if-else statement that can take many conditions and execute
accordingly
 First the user enters marks and the conditions are checked one by one and marks are displayed
The code

import java.util.Scanner;

public class second {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("enter marks:");
int marks = sc.nextInt();
if (marks>=90 && marks<=100)
System.out.println("grade A");
else if (marks>=70 && marks<90)
System.out.println("grade B");
else if (marks>=50 && marks<70)
System.out.println("grade C");
else if (marks<50)
System.out.println("grade F");
else
System.out.println("incorrect marks entered");
}
}

The results (Screenshot)

3
4
Lab Task No 3

Write a program which take input from user and check that whether that
letter is vowel or not?

Solution:
Brief description (3-5 lines)
 In the below program we have used if else statement and given many conditions in if
 For the input of a character type charat() is used.

The code

import java.util.Scanner;

public class third {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("enter a character:");
char ch = sc.next().charAt(0);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
System.out.println("u entered a vowel");
else
System.out.println("u entered a consonant");
}
}

The results (Screenshot)


Lab Task No 4

Print first 10 Fibonacci numbers using for loop.


Solution:
Brief description (3-5 lines)
 In this program we have used for loop to print the Fibonacci series
 We have started from 2 bcz the first two numbers have been predefined

The code
import java.util.Scanner;

public class third {


public static void main(String[] args) {
int a=0,inp,b=1,c;
Scanner sc = new Scanner(System.in);
inp = sc.nextInt();
for (int i =2 ; i<inp;i++){
c=a+b;
System.out.println(c);
a=b;
b=c;
}
}
}

The results (Screenshot)


Lab Task No 5

Print table and get table number from user using for loop.
Solution:
Brief description (3-5 lines)
 In this program we have used for loop to print the table of the user input

The code
import java.util.Scanner;

public class third {


public static void main(String[] args) {
int inp;
Scanner sc = new Scanner(System.in);
System.out.println("enter number of which u want table of:");
inp= sc.nextInt();
for (int i=1;i<=10;i++){
System.out.println(inp + "x" +i+"="+inp*i);
}
}
}

The results (Screenshot)


Lab Task No 6

Write a java program that print a half pyramid using characters.

Solution:
Brief description (3-5 lines)
 In this program we have used nested for loop outer loop is for the rows and the outer for loop is for
the columns
 And characters have been printed using the ASCII code
The code
import java.util.Scanner;

public class third {


public static void main(String[] args) {
int inp;
Scanner sc = new Scanner(System.in);
System.out.println("enter number of rows :");
inp= sc.nextInt();
for (int i=0;i<=inp;i++){
for (int j = 0;j<=i;j++){
System.out.print((char) (65+j)+" ");
}
System.out.println();
}
}
}

The results (Screenshot)


Lab Task No 7

Write a program in java to display the given below patter using nested loop.
Solution:
Brief description (3-5 lines)
 In this program we have used 3 for loops one loop for the number of rows inner first loop for printing
spaces pattern and second inner loop for the numbers
 %-7d is a format specifier string used in printf that is used to print formatted strings
The code
import java.util.Scanner;

public class third {


public static void main(String[] args) {
int inp, b = 1;
Scanner sc = new Scanner(System.in);
System.out.println("enter number of rows :");
inp = sc.nextInt();
for (int i = 1; i <= inp; i++) {
for (int j = 0; j < inp-i; j++) {
System.out.print(" ");
}
for (int k =0;k<i;k++){
System.out.printf("%-7d",b++);
}
System.out.println();
}
}
}

The results (Screenshot)


Lab Task No 04
Conclusion:
In this lab report we have learnt:
 Use of ternary operators
 Use of conditional statements
 Use of loops
 Printing different patterns

You might also like