Lab task 9
Lab task 9
Task : 1
Code :
#include<iostream>
using namespace std;
int Sum(int a, int b);
int Multiply(int a, int b);
float division(int a, int b);
int Subtract(int a, int b);
int main()
{
int x, y;
char ch;
cout << "Enter your numbers gradually : ";
cin >> x >> y;
cout << "Select u'r operation among + , - , * , / ";
cin >> ch;
switch (ch)
{
case '+' :
cout << "sum of two numbers is : " << Sum(x, y) << endl;
break;
case '-' :
cout << "subtraction of two numbers is : " << Subtract(x, y) << endl;
break;
case '*' :
cout << "Multiplication of two numbers is : " << Multiply(x, y)<<endl;
break;
case '/' :
cout << "Division of two numbers : " << division(x, y) << endl;
break;
default :
cout << "Invalid input "<<endl;
break;
}
}
int Sum(int a, int b)
{
return a + b;
}
int Multiply(int a, int b)
{
return a * b;
}
float division(int a, int b)
{
if (b == 0)
{
cout << "Invalid input ";
}
else
{
return a / b;
}
}
int Subtract(int a, int b)
{
return a - b;
}
OUTPUT:
Task : 2
Code :
#include<iostream>
using namespace std;
int Sum_digits(int n);
int main()
{
int x, res;
cout << " Enter value : ";
cin >> x;
res = Sum_digits(x);
cout << "Sum of digits are : " << res;
}
int Sum_digits(int n)
{
if (n == 1)
{
return 1;
}
else
{
return (n % 10) + Sum_digits(n / 10);
}
}
OUTPUT:
Task : 3
Code :
#include<iostream>
using namespace std;
int main() {
int arr_1[20];
int n ;
float sum = 0, average;
cout << "Enter total number of students : ";
cin >> n;
for (int i = 0; i < n; i++)
{
cout << "Enter number of student no : " << i + 1 << " ";
cin >> arr_1[i];
}
for (int i = 0; i < n; i++)
{
sum = sum + arr_1[i];
}
cout << "Sum of all numbers is : " << sum << endl;
average = sum / n;
cout << "Average of all numbers is : " << average;
}
OUTPUT:
Task : 4
Code :
#include<iostream>
using namespace std;
int main() {
int arr_1[5];
OUTPUT:
TASK : 5
CODE :
#include<iostream>
using namespace std;
void Swap(int &a, int &b);
int main() {
int arr_1[5];
cout << "Enter number gradually : " << endl;
for (int i = 0; i < 5; i++)
{
cin >> arr_1[i];
}
for (int i = 0; i < 5; i++)
{
for (int j = i; j < 5; j++)
{
if (arr_1[i] < arr_1[j])
{
Swap(arr_1[i], arr_1[j]);
}
}
}
cout << "Now array in decreasing order : ";
for (int i = 0; i < 5; i++)
{
cout << arr_1[i]<<" ";
}
}
void Swap(int &a, int &b)
{
int temp;
temp = a;
a = b;
b = temp;
return;
}
OUTPUT: