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

lab paper of cpp

The document outlines ten programming tasks in C++, each with a specific objective and corresponding code implementation. Tasks include a student grading system, prime number verification, factorial calculation, Fibonacci sequence generation, matrix operations (addition, transposition, multiplication), string reversal, a simple calculator, and number pattern generation. Each task is accompanied by an explanation of its functionality.

Uploaded by

abdullahwan876
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)
13 views

lab paper of cpp

The document outlines ten programming tasks in C++, each with a specific objective and corresponding code implementation. Tasks include a student grading system, prime number verification, factorial calculation, Fibonacci sequence generation, matrix operations (addition, transposition, multiplication), string reversal, a simple calculator, and number pattern generation. Each task is accompanied by an explanation of its functionality.

Uploaded by

abdullahwan876
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/ 7

Task 1: Student Grading System

Objective: Assign grades and provide remarks based on the score.

cpp
Copy
#include <iostream>
using namespace std;

int main() {
int score;
char grade;
cout << "Enter score: ";
cin >> score;

// Grade assignment
if (score >= 90) grade = 'A';
else if (score >= 80) grade = 'B';
else if (score >= 70) grade = 'C';
else grade = 'F';

// Remarks based on grade


switch (grade) {
case 'A': cout << "Excellent\n"; break;
case 'B': cout << "Good\n"; break;
case 'C': cout << "Needs Improvement\n"; break;
case 'F': cout << "Failed\n"; break;
}
return 0;
}

Explanation:

 The program takes the score as input, assigns a grade using if-else, and then uses a
switch statement to display remarks based on the grade.

Task 2: Prime Number Verification

Objective: Check if a number is prime.

cpp
Copy
#include <iostream>
#include <cmath>
using namespace std;

bool is_prime(int n) {
if (n <= 1) return false;
for (int i = 2; i <= sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
}

int main() {
int num;
cout << "Enter a number: ";
cin >> num;
cout << (is_prime(num) ? "Prime" : "Not Prime") << endl;
return 0;
}

Explanation:

 The function checks divisibility from 2 to the square root of the number. If no divisor is
found, it's prime.

Task 3: Factorial Calculation

Objective: Calculate the factorial of a number.

cpp
Copy
#include <iostream>
using namespace std;

unsigned long long factorial(int n) {


unsigned long long result = 1;
while (n > 1) {
result *= n--;
}
return result;
}

int main() {
int num;
cout << "Enter a number: ";
cin >> num;
cout << "Factorial: " << factorial(num) << endl;
return 0;
}

Explanation:

 The program calculates the factorial by multiplying the number with decreasing values
until 1.

Task 4: Fibonacci Sequence


Objective: Print Fibonacci numbers up to a specified value.

cpp
Copy
#include <iostream>
using namespace std;

void print_fibonacci(int n) {
int a = 0, b = 1;
while (a <= n) {
cout << a << " ";
int next = a + b;
a = b;
b = next;
}
}

int main() {
int num;
cout << "Enter a number: ";
cin >> num;
print_fibonacci(num);
cout << endl;
return 0;
}

Explanation:

 The Fibonacci sequence is printed by iteratively adding the previous two numbers.

Task 5: Matrix Addition

Objective: Add two 3x3 matrices.

cpp
Copy
#include <iostream>
using namespace std;

void matrix_addition(int mat1[3][3], int mat2[3][3], int result[3][3]) {


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
result[i][j] = mat1[i][j] + mat2[i][j];
}
}
}

int main() {
int mat1[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
int mat2[3][3] = {{9,8,7},{6,5,4},{3,2,1}};
int result[3][3];
matrix_addition(mat1, mat2, result);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << result[i][j] << " ";
}
cout << endl;
}
return 0;
}

Explanation:

 It adds corresponding elements of two matrices and stores the result in a new matrix.

Task 6: Matrix Transposition

Objective: Transpose a 3x3 matrix.

cpp
Copy
#include <iostream>
using namespace std;

void transpose_matrix(int mat[3][3], int transposed[3][3]) {


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
transposed[j][i] = mat[i][j];
}
}
}

int main() {
int mat[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
int transposed[3][3];
transpose_matrix(mat, transposed);

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


for (int j = 0; j < 3; j++) {
cout << transposed[i][j] << " ";
}
cout << endl;
}
return 0;
}

Explanation:

 The matrix is transposed by swapping rows with columns.


Task 7: Matrix Multiplication

Objective: Multiply two 3x3 matrices.

cpp
Copy
#include <iostream>
using namespace std;

void multiply_matrices(int mat1[3][3], int mat2[3][3], int result[3][3]) {


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
result[i][j] = 0;
for (int k = 0; k < 3; k++) {
result[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
}

int main() {
int mat1[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
int mat2[3][3] = {{9,8,7},{6,5,4},{3,2,1}};
int result[3][3];

multiply_matrices(mat1, mat2, result);

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


for (int j = 0; j < 3; j++) {
cout << result[i][j] << " ";
}
cout << endl;
}
return 0;
}

Explanation:

 The result of the multiplication is calculated by summing the products of the


corresponding elements in rows and columns.

Task 8: Reverse String

Objective: Reverse a string using pointers.

cpp
Copy
#include <iostream>
#include <cstring>
using namespace std;
void reverse_string(char str[]) {
int start = 0, end = strlen(str) - 1;
while (start < end) {
swap(str[start], str[end]);
start++;
end--;
}
}

int main() {
char str[100];
cout << "Enter a string: ";
cin >> str;
reverse_string(str);
cout << "Reversed: " << str << endl;
return 0;
}

Explanation:

 The string is reversed by swapping characters from both ends.

Task 9: Simple Calculator

Objective: Perform basic arithmetic operations.

cpp
Copy
#include <iostream>
using namespace std;

int main() {
int num1, num2;
char op;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "Enter operator (+, -, *, /): ";
cin >> op;

switch(op) {
case '+': cout << "Result: " << num1 + num2 << endl; break;
case '-': cout << "Result: " << num1 - num2 << endl; break;
case '*': cout << "Result: " << num1 * num2 << endl; break;
case '/':
if (num2 != 0) cout << "Result: " << num1 / num2 << endl;
else cout << "Error: Division by zero\n";
break;
default: cout << "Invalid operator\n"; break;
}
return 0;
}
Explanation:

 The program performs addition, subtraction, multiplication, or division based on the


operator input by the user.

Task 10: Number Patterns

Objective: Generate number patterns like pyramids or diamonds.

cpp
Copy
#include <iostream>
using namespace std;

void generate_pattern(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
cout << j << " ";
}
cout << endl;
}
}

int main() {
int n;
cout << "Enter number of rows: ";
cin >> n;
generate_pattern(n);
return 0;
}

You might also like