You are on page 1of 16

INTRO TO COMPUTER PROGRAMMING

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>

using namespace std;

int main() {

int numbers[] = {1,2,3,4,5,6,7};

int product =1;

cout << " Please enter the odd numbers whose product you want." <<endl;

int i;

for (i=0; i<7; i++)

cout<< "Enter the value of " << i+1 << " :" ;

cin>> numbers[i];

int j;

for ( j = 0; j<7; j++) {


if (numbers[j] %2 !=0){

product= product * numbers[j];

cout<< " The product of odd numbers is:" << product << endl;

return 0;

OUTPUT:

Please enter the odd numbers whose product you want.


Enter the value of 1 :1
Enter the value of 2 :2
Enter the value of 3 :3
Enter the value of 4 :4
Enter the value of 5 :5
Enter the value of 6 :6
Enter the value of 7 :7
The product of odd numbers is:105
PROGRAM # 2:
Create a function in C++ called checkEvenOdd that takes an integer as a parameter and returns true if
the number is even and false if it's odd. In your main program, take user input and use the function to
check if it's even. Print an appropriate message.

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);
}

void checkEvenOdd (int x)


{
if (x % 2 == 0)
{
cout << "True" << endl << "The number you entered was Even.";
}
else
cout << "False" << endl << "The number you entered was Odd.";
}

OUTPUT:

Enter the number you would like to check if it is even or not: 6


True
The number you entered was Even.
PROGRAM # 3:
Write a C++ program that initializes an array of 6 integers. Print the sum of all elements greater than 10
in the array.

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);
}

void FSterm (int a)


{
int x=0, y=1, z;
if(a==1)
{cout<<x;}
if(a==2)
{cout<<y;}
if(a>1) //Condition to output nth term input by user
{
for (int i=2; i<a; i++) //Loop to calculate the nth term
{
z=x+y;
x=y;
y=z;
}
cout<<"The "<<a<<" term is "<<z<<endl; //Output nth term
}
}

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];
}

void doubleArray (int, int []);


doubleArray (a, number1);
}

void doubleArray (int b, int number1[])


{
int number[b];
for (int i=0; i<b; i++)
{
number[i]=number1[i]*2;
cout<<"The value of "<<i+1<<" after double is:"<<number[i]; //Output array
cout<<endl;
}

}
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
}

int avg=0; //Initializing Average temperature


for (int i=0; i<10; i++) //Loop to calculate the Average temperature
{
avg=avg+array[i];
}
a=avg/10; //Average temperature
cout<<"Average temperature over the 10 days is: "<<a<<endl;

if (a<25) //Condition to check if the week was hot or mild


{
cout<<"The week was mild.";
}
else cout<<"The week was hot";
}

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);
}

void printPattern (int a)


{
int array[a];
number [0] =1;
number [1] =2;
number[2] =3;
number [3] =4;
for (int i = 0; i<a; i++)
{
switch (i)
{
case 0:
for (int i=0; i<1; i++)
{
cout<<number[i];

}
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);


checkDigit(a);

}
void checkDigit (int b)
{
char a;
if (b == a)
{

cout<<"The character is not a digit.";

}
else
{

cout<<"The character is a digit.";

}
}

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];
}

void addArray (int, int [], int []);


addArray (a, number1, number2);
}

void addArray (int b, number1[], int number2[])


{
int number3[b];

for (int i=0; i<b; i++)


{
number3[i]=number1[i]+number2[i];
cout<<"The "<<i+1<<" value of third number after addition is: "<<number3[i]<<endl;
}
}

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

You might also like