Lab 4
Lab 4
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
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;
}
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;
}
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("\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);
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");
}
Page 6 of 8
THE END
Page 7 of 8