0% found this document useful (0 votes)
2 views5 pages

Assignment 8 NEW

The document contains a series of programming assignments for a computer programming lab, focusing on C++ language. It includes tasks such as generating random numbers, calculating squares, and performing operations on user-inputted numbers. Each assignment is accompanied by code snippets demonstrating the required functionality.

Uploaded by

mussana shaykh
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)
2 views5 pages

Assignment 8 NEW

The document contains a series of programming assignments for a computer programming lab, focusing on C++ language. It includes tasks such as generating random numbers, calculating squares, and performing operations on user-inputted numbers. Each assignment is accompanied by code snippets demonstrating the required functionality.

Uploaded by

mussana shaykh
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
You are on page 1/ 5

American University of Ras Al Khaimah

Introduction to Computer Programming Lab CSCI 113


Lab Assignment 8
1. Try the following programs and check the output.

#include <iostream> #include <iostream>


#include <ctime> using namespace std;
using namespace std;
int cube(int n)
int main() {
{ return n*n*n;
srand(time(0)); }
for (int i=1; i<=10;i++) double cube(double n)
{ {
int random= rand()%6 +1; return n*n*n;
cout<< random <<'\t'; }
} int main()
system ("pause"); {
return 0; cout<< cube(4);
} cout<< endl;
cout<< cube(5.1);

system ("pause");
return 0;}

2. Write a program that has a function that output the numbers and their squares between 1 and 10 using
the following prototypes:
a. void squares (int x);
#include <iostream>
using namespace std;
void
squares (int x)
{
for (int i = x; i <= 10; i++)
cout << "The Square of" << i << "is" << (i * i) << endl;
}

int
main ()
{
int x = 1;
squares (x);

system ("pause");
return 0;

b. void squares (void);

#include <iostream>
using namespace std;
void
squares (void)
{
int x = 1;

for (int i = x; i <= 10; i++)


cout << "The Square of" << i << "is" << (i * i) << endl;

int
main ()
{

squares ();

system ("pause");
return 0;

3. Write a program that ask the user to input two double numbers: n1 and n2 (n1 must be less than n2).
Your program should include three functions to perform the following:
a. Output all odd numbers between n1 and n2.
b. Output the sum of all even numbers between n1 and n2.
c. Output the sum of the square of the odd numbers between n1 and n2.

#include <iostream>
#include <cmath>
using namespace std;

void
odd (double n1, double n2)
{

for (int i = ceil (n1); i <= floor (n2); i++)


{
if (i % 2 != 0)
cout << i;
}

void
SumE (double n1, double n2)
{

int d = 0;
for (int i = ceil (n1); i <= floor (n2); i++)
{
if (i % 2 == 0)
d = d + i;
}
cout << "Sum of Even Interger is " << d;
}

void
sumSqrOdd (double n1, double n2)
{
int o = 0;
int s = 0;

for (int i = ceil (n1); i <= floor (n2); i++)


{
if (i % 2 != 0)
o = i * i;
s = s + o;
}
cout << "sum of square odd numbers is" << s;
}

int
main ()
{
cout << "Enter 2 integers: " << endl;
double n1, n2;
cin >> n1 >> n2;
odd (n1, n2);
cout << endl;
SumE (n1, n2);
cout << endl;
sumSqrOdd (n1, n2);
cout << endl;
system ("pause");
return 0;
}
4. Write a program that has a function to find the sum of all the integers between 0 and x. x is an
integer entered by the user. Use the following prototypes: void sum (int x);
#include <iostream>
using namespace std;

void sum(int x)
{
int sum = 0;
for (int i = 0; i <= x; i++)
{
sum = sum + i;

}
] cout << sum;
}
int main()]
{
int x;
cin >> x;
sum(x);
cout << endl;
system("pause");
return 0;
}

You might also like