CP LAB Assignment 1
CP LAB Assignment 1
LAB ASSINGMENT
MAHRUKH AGHA
230101065
AERO 22-A
PROGRAM # 1
Write a C++ program that takes 7 integers as input from the user, stores them in an array,
and then prints the product of all odd numbers in the array.
SOLUTION:
#include <iostream>
int main() {
cout << " Please enter the odd numbers whose product you want." <<endl;
int i;
cout<< "Enter the value of " << i+1 << " :" ;
cin>> numbers[i];
int j;
cout<< " The product of odd numbers is:" << product << endl;
return 0;
OUTPUT:
SOLUTION:
#include <iostream>
using namespace std;
int main ()
{
int a;
cout << "Enter the number you would like to check if it is even or not: ";
cin >> a;
void checkEvenOdd (int);
checkEvenOdd (a);
}
OUTPUT:
SOLUTION:
#include <iostream>
using namespace std;
int main ()
{
int numbers [6];
cout <<"Please enter the values and this program will add the values greater than 10." << endl;
for (int i = 0; i < 6; i++)
{
cout << "Enter the value of " << i + 1 ;
cin >> numbers[i];
}
int sum = 0;
for (int i = 0; i < 6; i++)
{
if (numbers[i] > 10)
sum = sum + numbers[i];
}
}
cout << "The sum of all numbers greater than 10 in the entered values is: " << sum; //sum of all numbers that
are greater than 10
}
OUTPUT:
Please enter the values and this program will add the values greater than 10
Enter the value of 1 : 1
Enter the value of 2 : 10
Enter the value of 3 : 30
Enter the value of 4 60
Enter the value of 5 : 40
Enter the value of 6 : 5
The sum of all numbers greater than 10 in the entered values is: 130
PROGRAM # 4:
Design a C++ program that calculates and prints the nth term of the Fibonacci sequence using a function.
The program should take 'n' as input from the user and display the result.
SOLUTION:
#include <iostream>
using namespace std;
int main ()
{
int n;
cout<<"This program will take first two values of Fibonacci Series as 0 and 1. "<<endl;
cout << "Enter the number of term of Fibonacci Series you want to print: ";
cin>>a;
void FSterm(int);
FSterm(n);
}
OUTPUT:
This program will take first two values of Fibonacci Series as 0 and 1.
Enter the number of term of Fibonacci Series you want to print: 6
The 6 term is 5.
PROGRAM # 5:
Write a C++ program that initializes an array of 8 integers. Use a loop to find and print the index of the
first occurrence of the maximum value in the array.
SOLUTION:
#include <iostream>
using namespace std;
int main ()
{
int number [8];
cout <<"Enter 8 values of array to check which of them is larger and at which position the lager number is."<<
endl;
for (int i = 0; i < 8; i++)
{
cout << "Enter the value of :" << i + 1 << ;
cin >> array[i];
}
int max = 0, id = 0;
for (int i = 0; i < 8; i++)
{
if (number[i] > max)
{
max = number[i];
id = i;
}
}
cout << "Maximum value in the array is: " << max << endl <<"The position of max is: " << id + 1; //Output
max and its position
}
OUTPUT:
Enter values of array to check which of them is larger and at which position the lager number is.
Enter the value of 1 : 60
Enter the value of 2 : 2
Enter the value of 3 : 40
Enter the value of 4 : 59
Enter the value of 5 : 90
Enter the value of 6 : 3
Enter the value of 7 : 6
Enter the value of 8 : 6
Maximum value in the array is: 90
The position of max is: 5
PROGRAM # 6:
Create a function in C++ called doubleArray that takes an array of integers and its size as parameters
and doubles each element in the array. In the main program, take an array from the user, call the
function, and print the modified array.
SOLUTION:
#include <iostream>
using namespace std;
int main ()
{
int a;
cout<<"Enter the size of array you want to double: ";
cin>>a;
int number1[a];
for (int i=0; i<a; i++)
{
cout<<"Enter the value of: "<<i+1;
cin>>number1[i];
}
}
OUTPUT:
Enter the size of array you want to double: 3
Enter the value of 1 : 5
Enter the value of 2 : 5
Enter the value of 3 : 6
The value of 1 after double is: 10
The value of 2 after double is: 10
The value of 3 after double is:12
PROGRAM # 7
Write a C++ program that initializes an array of 10 integers representing daily temperatures. Determine
and print the average temperature for the week. If the average is above 25, print "Hot week," otherwise
print "Mild week."
SOLUTION:
#include <iostream>
using namespace std;
int main ()
{
int number [10];
float a;
cout<<"Enter recorded temperature(Celsius) over the 10 days to check the average and if the week was hot or
cold."<<endl;
for (int i=0; i<10; i++)
{
cout<<"Enter the temperature of "<<i+1<<" day: ";
cin>>array[i]; //Storing them in array
}
OUTPUT:
Enter recorded temperature(Celsius) over the 10 days to check the average and if the week was hot or cold.
Enter the temperature of 1 day: 3
Enter the temperature of 2 days: 6
Enter the temperature of 3 days: 46
Enter the temperature of 4 days: 55
Enter the temperature of 5 days: 34
Enter the temperature of 6 days: 2
Enter the temperature of 7 days: 5
Enter the temperature of 8 days: 4
Enter the temperature of 9 days: 34
Enter the temperature of 10 days: 23
Average temperature over the 10 days is: 21
The week was mild.
PROGRAM # 8:
Design a C++ program that defines a function called printPattern to print the following pattern:
1
121
12321
1234321
Use this function to print the pattern up to 4 rows.
SOLUTION:
#include<iostream>
using namespace std;
int main ()
{
int a;
a=4;
void printPattern(int);
printPattern(a);
}
}
cout<<endl;
break;
case 1:
for (int i=0; i<1; i++)
{
for (int j=0; j<2; j++)
{
cout<<number[j];
}
for (int j=0; j<1; j++)
{
cout<<number[j];
}
}
cout<<endl;
break;
case 2:
for (int i=0; i<1; i++)
{
for (int j=0; j<3; j++)
{
cout<<number[j];
}
for (int j=1; j>=0; j--)
{
cout<<number[j];
}
}
cout<<endl;
break;
case 3:
for (int i=0; i<1; i++)
{
for (int j=0; j<a; j++)
{
cout<<number[j];
}
for (int j=2; j>=0; j--)
{
cout<<number[j];
}
}
cout<<endl;
break;
}
}
}
OUTPUT:
1
121
12321
1234321
PROGRAM # 9
Create a C++ program that defines a function checkDigit to check if a given character is a digit or not.
Take a character as input from the user and use the function to print whether it is a digit or a non-digit.
SOLUTION:
#include <iostream>
using namespace std;
int main ()
{
int a;
cout<<"Enter the character you want to check if it is a digit or not: ";
cin>>a;
}
void checkDigit (int b)
{
char a;
if (b == a)
{
}
else
{
}
}
OUTPUT 1:
Enter the character you want to check if it is a digit or not: 5
The character is a digit.
OUTPUT 2:
Enter the character you want to check if it is a digit or not: U
The character is not a digit.
PROGRAM # 10:
Write a C++ program that initializes two arrays of the same size with integer values. Create a function
called addArrays that takes these two arrays as parameters, adds corresponding elements, and stores the
result in a new array. Print the resulting array.
SOLUTION:
#include <iostream>
using namespace std;
int main ()
{
int a;
cout<<"Enter size of both arrays you want to add: ";
cin>>a;
int number1[a];
int number2[a];
for (int i=0; i<a; i++)
{
cout<<"Enter the value of: "<<i+1;
cin>>number1[i];
}
for (int i=0; i<a; i++)
{
cout<<"Enter the value of "<<i+1;
cin>>number2[i];
}
OUTPUT:
Enter size of both arrays you want to add: 3
Enter the value of 1 of first array: 4
Enter the value of 2 of first array: 5
Enter the value of 3 of first array: 6
Enter the value of 1 of second array: 7
Enter the value of 2 of second array: 8
Enter the value of 3 of second array: 9
The 1 value of third number after addition is: 11
The 2 value of third number after addition is: 13
The 3 value of third number after addition is: 15