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

Computer Fundamentals Lab Report#5

The document contains a lab report by Junaid Ahmed that includes code for 3 tasks: 1) A program to check if a year is a leap year or not and runs indefinitely until the user inputs 0. 2) A program to calculate the factorial of a user-input number. 3) A program that takes user input of multiple numbers, stores them in an array, and sorts the array in ascending or descending order based on user choice.

Uploaded by

Paint2Luv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Computer Fundamentals Lab Report#5

The document contains a lab report by Junaid Ahmed that includes code for 3 tasks: 1) A program to check if a year is a leap year or not and runs indefinitely until the user inputs 0. 2) A program to calculate the factorial of a user-input number. 3) A program that takes user input of multiple numbers, stores them in an array, and sorts the array in ascending or descending order based on user choice.

Uploaded by

Paint2Luv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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