0% found this document useful (0 votes)
11 views9 pages

Programming Tasks for Lab 6 Submission

The document provides instructions for a lab task that requires submitting a programming assignment by a specified deadline. It includes five programming tasks that involve basic arithmetic operations, recursion, factorial calculation, variable scope demonstration, and summing prime numbers in an array. The document also includes code snippets for each task, showcasing the implementation of the required functions.

Uploaded by

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

Programming Tasks for Lab 6 Submission

The document provides instructions for a lab task that requires submitting a programming assignment by a specified deadline. It includes five programming tasks that involve basic arithmetic operations, recursion, factorial calculation, variable scope demonstration, and summing prime numbers in an array. The document also includes code snippets for each task, showcasing the implementation of the required functions.

Uploaded by

Uchaash Barua
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Instructions: Please read carefully

 Please rename this file as only your ID number (e.g. 18-*****-[Link] or 18-*****-[Link]).
 Submit the file before 11:59pm on 13/11/2020 in the Portal Lab Performance section labeled Lab task 6. If you
cannot complete the full task, just upload what you have completed.

1. Write a program to add, subtract, multiply and divide two integers using user defined type function.
2. Write a program to calculate sum of first 50 natural numbers using recursive function.
3. Define a function named fact() to calculate factorial of a number n.
4. Write a program that illustrates use of local, global and static variables
5. Write a program to find the sum of the prime numbers in an array using user defined type function. Pass the
array and the size of the array using function argument.
Your code here:
1.
#include <iostream>
using namespace std;

int add(int x, int y){


return x+y;
}
int sub(int x, int y){
return x-y;
}
int mul(int x, int y){
return x*y;
}
float divison (int x,int y){
return x/y;
}

int main() {

float var1 =8,var2=4;

cout<<"add = "<<add(var1,var2)<<endl;
cout<<"sub = "<<sub(var1,var2)<<endl;
cout<<"mul = "<<mul(var1,var2)<<endl;
cout<<"Division ="<<divison (var1,var2)<<endl;
return 0;
}

2. #include <iostream>
using namespace std;

void fun(int count)


{
if(count<50)
{
cout<<"Recursive function "<<count<<" time"<<endl;
count++;
fun(count);
}
}

int main() {

fun(0);

return 0;
}

3. #include <iostream>
using namespace std;

int fact(int n)
{
if (n == 0 || n==1) {
return 1;
}
else {
return n * fact(n - 1);
}
}

int main()
{
int n;
cout<<"Enter the number :";
cin>>n;

cout << "Factorial of "<< n <<" = "<<fact(n);

4.
#include <iostream>

using namespace std;


int g=40;
int Value()
{
static int S=90;
cout<<"Static Variable S="<<S<<endl;

}
int main()
{
cout <<"Global Variable G="<<g<<endl;
int l=50;
cout <<"Local Variable L="<<l<<endl;
Value();
return 0;
}

5. #include<iostream>
using namespace std;

int sum_prime(int a[], int n) {


int sum = 0, c, i;

cout << "Prime number are : ";

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


{
c = 0;
for (int j = 2; j < a[i]; j++)
{
if (a[i] % j == 0)
{
c = 1;
break;
}
}
if (c == 0 && a[i] != 1 && a[i] != 2)
{
cout << a[i] << " ";
sum = sum + a[i];
}
}
cout <<endl<< "Sum of prime number is : " << sum;

}
int main()
{
int a[5], n;

cout << "Enter the number of elements : ";


cin >> n;

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


{
cin >> a[i];
}

sum_prime(a, n);

return 0;
}

Your whole Screenshot here: (Console Output):

1.
2.

3.
4.

5.

You might also like