CFP Lab Report 5

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Computer fundamentals and

Programming

Lab report: 5

Understanding:
Following are the main takeouts from the lab:
● Understanding the syntax of while loop and its usage to form useful programs
● Comprehending the syntax of for loop and its usage to solve mathematical problems.
● Use of “array “ to store multiple values in the same variable and learn how it is displayed
and taken as input from the user.

Tasks:
1. Write a program that takes continuous input of marsk from the user using a while loop
and assigns grades and terminates only when the user gives a specific input.
2. Write a program that can take different numbers as input from the user and save them in
an array and sort the array according to user choice in ascending and descending
numbers.
3. Write a program that takes a number input from the user and prints the factorial of a
given number.

Programs:

Question: 1
Design a program that can take continuous input of years from user using while loop and
check whether the inputted year is a leap year or not and also terminates the program on a
specific input.

Code:
#include <iostream>
using namespace std;
int main()
{
int marks, y = 1,i;
cout << "This program assigns grade according to the input marks and allows
grading of multiple students.\n";
while (y == 1)
{
cout << "Input marks: ";
cin >> marks;
if (90 < marks && marks <= 100)
{
cout << "Grade A+.\n";
}
else if (80 < marks && marks <= 90)
{
cout << "Grade A.\n";
}
else if (70 < marks && marks <= 80)
{
cout << "Grade B.\n ";
}
else if (60 < marks && marks <= 70)
{
cout << "Grade C.\n ";
}
else if (50 < marks && marks <= 60)
{
cout << "Grade D.\n ";
}
else
{
cout << "Grade F.\n ";
}
cout << "Type 1 to find grade of another student or type 2 to exit the program.";
cin >> i;
switch (i) {
case 1:
y = 1;
break;
case 2:
y = 2;
break;
}
}
return 0;
}

Output:

Description:
The code uses switch case to assign the grade according to the marks input by the user however
the concept of loops have also been introduced in this code that allows the user to assign the
grade several time until he ends the loop the program will ask the user if he wants to continue.

Question: 2
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[20];
int n,i,j;
int temp;
cout<<"Enter total number of elements to read: ";
cin>>n;
if(n<0 || n>20)
{
cout<<"Input valid range!!!"<<endl;
return -1;
}

for(i=0;i<n;i++)
{
cout<<"Enter element ["<<i+1<<"] ";
cin>>arr[i];
}
cout<<"Unsorted Array elements:"<<endl;
for(i=0;i<n;i++)
cout<<arr[i]<<"\t";
cout<<endl;

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<<"Sorted (Ascending Order) Array elements:"<<endl;
for(i=0;i<n;i++)
cout<<arr[i]<<"\t";
cout<<endl;
return 0;
}

Output:

Description:
The code is used to print the ascending and descending order of the values in an array. An array
allows to store various values in a single variable and then the code uses nested loops and if else
statements to print the ascending or descending order of the array.

Question: 3
Write a program that take a number input from user and print the factorial of given number

Code:
#include<iostream>
using namespace std;
int main() {
int num,factorial=1;
cout<<" Enter Number To Find Its Factorial: ";
cin>>num;
for (int a=1;a<=num;a++) {
factorial=factorial*a;
}
cout<<"Factorial of Given Number is ="<<factorial<<endl;
return 0;
}

Output:

Description:
The program uses for loop to find the factorial of an input number. Since for loop is
known about how many times it would run there would be a variable I which is initialized the
concept of increament has also been introduced.

You might also like