0% found this document useful (0 votes)
17 views6 pages

OS Lab2

The document contains 9 tasks assigned to a student to learn C++ syntax and programming concepts. The tasks cover printing output, taking user input, calculations, control flow, loops, functions, and more. For each task the student provides the full C++ code to solve the problem, such as calculating area of shapes, determining if a number is prime, printing the Fibonacci series, and other programming exercises.

Uploaded by

punjjaabb44
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)
17 views6 pages

OS Lab2

The document contains 9 tasks assigned to a student to learn C++ syntax and programming concepts. The tasks cover printing output, taking user input, calculations, control flow, loops, functions, and more. For each task the student provides the full C++ code to solve the problem, such as calculating area of shapes, determining if a number is prime, printing the Fibonacci series, and other programming exercises.

Uploaded by

punjjaabb44
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/ 6

Lab 2 (10-10-2023)

C++ SYNTAX

Student Name: __Bilal Hasan Khan__Roll No BSCS 202137

1. Introduction to C++
Task 1: Write a C++ program that prints your name and a brief message to the console.
#include <iostream>
using namespace std;
int main() {
cout << "My name is Bilal hasan " <<endl;
cout << "I am student of Namal ";
return 0;
}

2. Basics of C++
Task 2: Create a program that calculates the area of a rectangle using user input for its
length and width.
#include <iostream>
using namespace std;
int main() {
int length;
int width;

cout << "Give length= ";

cin >> length;


cout << "Give width= ";
cin >> width;

cout << "Area= " << length*width;

return 0;
}
3. Input and Output
Task 3: Develop a program that asks the user for their age and then prints a message
indicating whether they are a minor or an adult.
#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter Age= ";
cin >> age;
if (age >= 18) {
cout << "You are an adult.";
}
else {
cout << "You are minor";
}
return 0;
}

4. Control Flow
Task 4: Write and run a C++ program that takes three numbers as an input and prints the
maximum number from them using If-Else statements.
Sample Input: 12 45 7
Output: The Maximum number is 45.
#include <iostream>
using namespace std;
int main() {
int num1;
int num2;
int num3;
cout << "Enter number1= ";
cin >> num1;
cout << "Enter number2= ";
cin >> num2;
cout << "Enter number3= ";
cin >> num1;
if (num1>num2 && num1>num3) {
cout << num1 << "is greater";
}
else if (num2>num1 && num2>num3){
cout << num2 << "is greater";
}
else {
cout << num3 << "is greater";
}
return 0;
}

Task 5: Write and run a C++ program that takes a number from the user and prints either
it is a prime number or not using loops and If-Else statements. (User will be allowed to
enter different numbers one by one until the user prints “-1”).
Sample Input: 7
Output: It is a prime number.
Sample Input: 4
Output: It is not a prime number.
#include <iostream>
using namespace std;
int main() {
int num1;
bool check=true;
while (check){
cout << "Enter number to check prime= ";
cin >> num1;
if (num1 == -1){
break;
}
for (int i = 2; i <= num1-1; i++){
if (num1%i==0){
cout << "Number is not prime" << endl;
break;
}
else {
cout << "Number is prime" << endl;
break;
}
}
}
return 0;
}

Task 6: Write and run a C++ program that takes a number from the user and prints the
Fibonacci series up to the given number.
Sample Input: 7
Output: 0, 1, 1, 2, 3, 5, 8.
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of terms in the Fibonacci series: ";
cin >> n;
int first = 0, second = 1;
cout << n << endl;
cout << first << " " << second << " ";
for (int i = 2; i < n; i++) {
int next = first + second;
cout << next << " ";
first = second;
second = next;
}
cout << endl;
return 0;
}

Task 7: Write and run a C++ program that takes a number from the user and tell either it
is even or odd. (User will be allowed to enter different numbers one by one until the user
prints “-1”).
Sample Input: 4
Output: It is an even number.
Sample Input: 9
Output: It is an odd number.
#include <iostream>
using namespace std;
int main() {
int num1;
bool check=true;
while (check){
cout << "Enter number to check odd even= ";
cin >> num1;
if (num1 == -1){
check=false;
break;
}
if (num1%2==0){
cout << "Number is Even" << endl;
}
else {
cout << "Number is Odd" << endl;
}
}
return 0;
}

5. Functions

Task 8: Write a C++ function that calculates and returns the factorial of an integer.
#include <iostream>
using namespace std;
int factorial(int num1){
int total=1;
for (int i=1; i<num1+1;i++){
total=total*i;
}
return total;
}
int main() {
int num1;
cout << "Enter number to find factorial";
cin >> num1;
int result=factorial(num1);
cout << "Factorial of " << "is " << result;
return 0;
}

Task 9: Write a program that calculates and prints the area of a circle given its radius
using a separate function.
#include <iostream>
using namespace std;
float area(int num1){
float total;
float pi=3.17;
total=pi*num1*num1;
return total;
}
int main() {
int num1;
cout << "Enter radius to find Area of circle= ";
cin >> num1;
float result=area(num1);
cout << "Area is " << result;
return 0;
}

You might also like