0% found this document useful (0 votes)
22 views8 pages

Lab 4

Uploaded by

bM malik
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)
22 views8 pages

Lab 4

Uploaded by

bM malik
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/ 8

Lab Report No: 04

Object Oriented Programming

Submitted By:
[MOHAMMAD AWAIS MALIK]- {19-EE-057}
ALI – 19-EE-179
WASIF -19-EE-157
TALHA – 19-EE-093
[Section:C]

Submitted to:
Sir Jawwad Qammar

Dated:
Week 04
Department of Electrical Engineering,
HITEC University, Taxila

Lab Task No 01:

Solution:
Brief description (3-5 lines)
Calculate sum of natural numbers using recursive function.

The code
#include <stdio.h>
int addNumbers(int n);
int main() {
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);
printf("Sum = %d", addNumbers(num));
return 0;
}

int addNumbers(int n) {
if (n != 0)
return n + addNumbers(n - 1);
else
return n;
}

The results (Screenshot)

Page 1 of 8
Lab Task No 02:

Solution:
Brief description (3-5 lines)
Write a program to find factorial of any number by using recursive function.

The code
#include<stdio.h>
using namespace std;
long int multiplyNumbers(int n);
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}

long int multiplyNumbers(int n) {


if (n>=1)
return n*multiplyNumbers(n-1);
else
return 1;
}

The results (Screenshot)

Page 2 of 8
Lab Task No 03:

Solution:
Brief description (3-5 lines)
Create a structure called employee having four members, empId, empName, Designation and
Salary. Ask the user to fill in the data (using for loop) for 4 employees, store it in four
variables of type struct employee and display all the employees’ data.

The code

Page 3 of 8
#include<stdio.h>

struct Employee
{
int Id;
char Name[25];
long Salary;
};

int main()
{
int i;
struct Employee Emp[ 4 ]; //Statement 1

for(i=0;i<4;i++)
{

printf("\nEnter details of %d Employee",i+1);

printf("\n\tEnter Employee Id : ");


scanf("%d",&Emp[i].Id);

printf("\n\tEnter Employee Name : ");


scanf("%s",&Emp[i].Name);

printf("\n\tEnter Employee Salary : ");


scanf("%ld",&Emp[i].Salary);
}

printf("\nDetails of Employees");
for(i=0;i<4;i++)
printf("\n%d\t%s\t%d\t%ld",Emp[i].Id,Emp[i].Name,Emp[i].Salary);

The results (Screenshot)

Page 4 of 8
Lab Task No 04:

Solution:
Brief description (3-5 lines)
Store Information (name, roll and marks) of a Student Using Structure, compute the student
CGPA and display all the student particulars with CGPA.

The code
#include<iostream>
using namespace std;
struct Student

Page 5 of 8
{
int Roll;
char Name[25];
int Marks[3];
int Total;
float Avg;
};
int main()
{
int i;
Student S;
cout<< "\nEnter Student Roll : ";
cin>> S.Roll;
cout<< "\nEnter Student Name : ";
cin>> S.Name;
S.Total = 0;
for(i=0;i<3;i++)
{
cout<< "\nEnter Marks " << i+1 << " : ";
cin>> S.Marks[i];
S.Total = S.Total + S.Marks[i];
}
S.Avg = S.Total / 3;
cout<< "\nRoll : " << S.Roll;
cout<< "\nName : " << S.Name;
cout<< "\nTotal : " << S.Total;
cout<< "\nAverage : " << S.Avg;
system("pause");
}

The results (Screenshot)

Page 6 of 8
THE END

Page 7 of 8

You might also like