Computer Fundamentals Lab Report#5

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

LAB REPORT#5

Junaid Ahmed
BSEE-11807-2021
Sec-A
Dated:09-11-2021

Task#1:
Design a program to check if an year is a leap year or not and it must run for infinite time
until user stops it on a specific input.
Code:
#include <iostream>
using namespace std;
int main()
{
int year;
int check;
while (true)
{
cout << "Enter the year to check if it is a leap year or not or press 0
to end!\n";
cin >> year;
if (year==0)
{
break;
}
else
{
check = year % 4;
switch (check)
{
case 0:
cout << "This is a leap year\n";
break;
case 1:
cout << "This is not a leap year.Last year was a leap year\n";
break;
case 3:
cout << "This is not a leap year.Next year is a leap year\n";
break;
default:
cout << "This is not a leap year\n";
break;
}
}
}
return 0;

}} Result:
Task#2:

Make a program to print factorial of any entered number.

Code:
#include <iostream>
using namespace std;
int main()
{
int x, fact;
cout << "Enter any number to find its factorial!\n";
cin >> x;
fact = x;
while (x > 1)
{
x--;
fact = fact * x;
}
cout << "Factorial= " << fact << endl;
return 0;
}

Task#3
Write a program that can take different numbers as input from user and save them in an array and
sort the array according to user choice in ascending and descending numbers.

Code:
#include <iostream>
using namespace std;
int main()
{
int arr[100];
int n, i, j,x;
int temp;
cout << "Enter total number of elements to sort:\n";
cin >> n;
for (i = 0; i < n; i++)
{
cout << "Enter element [" << i + 1 << "]= ";
cin >> arr[i];
}
cout << "Unsorted elements:" << endl;
for (i = 0; i < n; i++)
cout << arr[i] << "\t";
cout << endl;
cout << "For ascending,press 1 and for desending,press 0\n";
cin >> x;
if (x == 0)
{
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (arr[i] < arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
cout << "Descending Order:" << endl;
for (i = 0; i < n; i++)
cout << arr[i] << "\t";
cout << endl;
}
else if (x == 1)
{
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
cout << "Ascending Order:" << endl;
for (i = 0; i < n; i++)
cout << arr[i] << "\t";
cout << endl;
}
return 0;
}

Result:

You might also like