Write a program in C++ that makes a function called swap and it has two parameter and swaps the values of
the two parameters and prints them in the main function after the replacement using call by value and call by reference
example let x=10 and y= 60
output --> x=60 and y= 10
include <iostream>
using namespace std;
int swap (int &x , int &y );
int main()
{
int i = 10 , j=60; // calling by value // calling by referance paramters
swap(i,j);
cout <<"i becomes"<< i<<endl;
cout <<"j becomes"<<j<<endl;
}
int swap (int &x , int &y)
{
int temp ;
temp = x;
x=y;
y=temp;
return x,y;
}


write c++ program to display Star Triangle on screen which base is 10 The
pyramid should look like this
*
**









include <iostream>
using namespace std;
int main()
{ for (int line = 1 ; line <= 10 ; line ++)

{
for (int star = 1; star <= line ; star ++)
cout <<"*";
cout <<endl;
}
return 0 ;
}


Write a function called zeroSmaller() that is passed two int arguments by reference
and then sets the smaller of the two numbers to 0. Write a main() program to exercise
this function


include<iostream>
using namespace std;

void zero_small(int &a,int &b);

int main()
{
int x,y;
cout<<"Enter first number : ";
cin>>x;
cout<<"Enter second number : ";
cin>>y;
zero_small(x,y);
cout<<"First number is : "<<x;
cout<<"\nSecond number is : "<<y;

⎗
✓
return 0;

}

void zero_small(int &a, int &b)
{
if(a<b)
a=0;
else
b=0;
}


Raising a number n to a power p is the same as multiplying n by itself p times. Write a
function called power() that takes a double value for n and an int value for p, and
returns the result as a double value. Use a default argument of 2 for p.


include<iostream>
using namespace std;

double power(double,int=2);

int main()
{
int p;
double n,r;
cout << "Enter number : ";
cin >> n;
cout << "Enter exponent : ";
cin >> p;
r = power(n,p);
cout << "Result is " << r;

⎗
✓
return 0;

}

double power(double a, int b)
{
double x = 1;
for(int i = 1; i <= b; i++)
x = x * a;
return x;
}


Write a function called hms_to_secs() that takes three int values—for hours, minutes,
and seconds—as arguments(parameters), and returns the equivalent time in seconds .
Create a program that exercises this function by repeatedly obtaining a time value in
hours, minutes, and seconds from the user , calling the function, and
displaying the value of seconds it returns


include<iostream>
using namespace std;
int hms_to_sec(int hr,int min, int sec);
int main()
{
int hr,min,sec;
int result =hms_to_sec(hr,min,sec);
cout << "result = " << result;
return 0;
}

int hms_to_sec(int hr,int min, int sec)
{
int seconds =0;
cout << "please enter hr" << endl;
cin >> hr;
cout << "please enter min" << endl;
cin >> min;
cout << "please enter sec" << endl;
cin >> sec;

seconds = (hr6060)+(min*60)+sec;
return seconds;

}

write c++ program which insert 10 number then output how many of even number and how many of odd number

using namespace std;

int main()
{
int odd=0,even=0;
int num;
for (int i = 0; i < 10; i++) {
cin>>num;
if (num % 2 == 0){
even++;
cout << num << " is an even number\n";
}
else
{
cout<<num <<" is an odd number\n";
odd++;
}
}
cout<<"the odd number = "<<odd<<endl;
cout<<"the even number = "<<even<<endl;
}


Write a C++ program to accept 10 numbers from the user and calculate
the sum of the factorials of these numbers. ( Your program should check
for the negative numbers and zeros.)

int main()
{

int sum=0;
int num;
cout<<"enter number";
for (int i=0;i<10;i++){
cin>>num ;
if(num>0){
int fact=1;
for(int j=1;j<=num;j++){
fact=fact*j;
}
cout<<"fact= "<<fact<<endl;
sum+=fact;
}
else {
cout<<"negative or zero"<<endl;
}}
cout<<"sum = "<<sum<<endl;
}


Write a C++ program to accept 10 numbers and calculate the factorials of these numbers.
include<iostream>
using namespace std;
int main()
{
int number , fact =1;
for (number = 1 ; number <= 10 ; number ++)
{
fact=1;
for (int i =1 ; i <= number ;i++)
fact = fact *i ;
cout << "the factorial of "<<number <<"is"<< fact <<"\n";
}
}


/ Write a program that ask the user to enter 10 Employee
salaries and store them in array , then add a bonus of 10 % for each
employee and print out the average salary value.
/

include<iostream>
using namespace std ;
int main()
{ float sum=0;
float salaries [10]={0}; // intialization array
for (int i=0 ; i<=9 ; i++)
{
cout << "enter number " <<"\n";
cin >> salaries[i] ; // block
}
for (int indx = 0 ; indx<= 9 ; indx++){
salaries [indx] += salaries[indx] * 0.10;
cout<<salaries[indx]<<" ";
}
for (int i =0 ; i<10 ;i++ ){
sum += salaries[i];}
cout << "\n the average is"<<sum/10;
}


write a C++ program to print number of zeros of a series of 10 numbers
entered by the user

include <iostream>
using namespace std;

int main()
{
int num;
int count=0;
cout<<"enter number"<<endl;
for(int i=1;i<10;i++)
{
cin>>num;
if(num==0){
count++;
}
}
cout<<"number of zeros number"<<count<<endl;
}


Write a program to print the following
x
x^2
x^3+5

1 1 6
2 4 13
3 9 32
4 16 69
5 25 13

include <iostream>

using namespace std;

int main()
{
for(int i=1;i<=5;i++)
{
int x=i;
cout<<x<<endl;
}
for(int j=1;j<=5;j++)
{
int expx=jj;
cout<<expx<<endl ;
}
for(int k=1;k<=5;k++)
{
int cupicx=(k
k*k)+5;
cout<<cupicx<<endl;
}
}


Write a program to display the following numbers
3 6 9 12 15 ……… .

include <iostream>

using namespace std;

int main()
{
int n;
cin>>n;
int sum=0;
for(int i=0;sum<=n;i++)
{
sum=sum+3;
cout<<sum<<endl;
}
}


Write a C++ program that reads in a umber and gives the shown output:
Enter a number : 8
the number 8 is devisable the numbers 1, 2, 4, 8
Enter a number : 45
the number 45 is devisable the numbers 1, 3, 5, 9, 15, 45

include <iostream>

using namespace std;

int main()
{
int num1,count=0;
cin>>num1;
for(int i=1;i<num1;i++)
{
if(num1%i==0)
{

⎗
✓
cout<<i<<endl;

}

}
}


Write a program to show the Power of any number you enter using a while-loop
include <iostream>

using namespace std;

int main()
{
int num1;
cin>>num1;
int x;
cin>>x;
int fact=1;
for (int j=1;j<=x;j++){
fact=fact*num1;
}
cout<<fact;
}


Write a program that gives the sum of squares of numbers from 10 to 20
include <iostream>

include <cmath>

using namespace std;
int main()
{
int sum=0;
for (int i=10;i<=20;i++){

⎗
✓
1
2
3
4
5
int y=pow(i,2);
sum=sum+y;
 cout<<"y= "<<y<<endl;
   }
   cout<<"sum=  "<<sum<<endl;

}

write two functions with same name sum. The function take two value then apply the
summation operation then return result.

solution

include <iostream>

using namespace std;
int sum (int a, int b)
{
return a+b;
}
double sum (double a, double b)
{
return a+b;
}
int main ()
{
cout<<"Summation of integer: "<<sum(4,8)<<endl;
cout<<"Summation of double: "<<sum(2.5,3.0)<<endl;
}


Write a program in C++ to print a triangle of consecutive numbers
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1

include<iostream>
using namespace std;
int main() {
int number;
cout<<"enter the number "<<endl;
cin>>number;
for (int j=number;j>0;j--){
for (int i=0;i<j;i++){
cout<<j<<" ";
}
cout<<endl;

⎗
✓
}

}

Create a program in C++ that will enter the students' scores and store them in an unknown array. And set a stop condition to enter (-1)

solution

include <iostream>
using namespace std;

int main()
{
int input;
int grade[100]={0};
int size=0;//put size to force array stop in last input
cout<<"enter student grade and put (-1)to stop"<<endl;
cin>>input;
for(int i=0;input!=-1;i++){
size++;
grade[i]=input;
cin>>input;
}
for(int i=0;i<size;i++){
cout<<grade[i]<<endl;
}

⎗
✓
}

You have an array with five values ​​{88, 85, 89, 93, 90, {
First print the array values, then calculate the sums and the mean and print them.
Note: the arithmetic mean is a decimal value

solution
include <iostream>
using namespace std;

int main()
{
const int size = 5;
int array[size] = {90, 93, 89, 87, 88};
int sum=0;
double average=0.0;
for(int i=0;i<size;i++){
cout<<i<<"--"<<array[i]<<endl;
}
for(int i=0;i<size;i++){
sum+=array[i];
}
average=sum/size;
// average= static_cast<double>(sum)/size;//تحويل ضمنى
cout <<"sum = "<<sum<<endl;
cout<<"average = "<<average<<endl;

}

write c++ function called (factorial) to calculate factorial by using recursion in the same function ,then recall function in main
n!=n*(n-1)! example 5!=120

include <iostream>
using namespace std;
int factorial(int n){
if(n1||n0)
return 1;
return n*factorial(n-1);
}
int main()
{
int num;
cin>>num;
for(int i=1;i<=num;i++){
cout<<i<<"! = "<<factorial(i)<<endl;
}

}

write c++ function called (increment) that take two parameter number and increment number +1 and returned it by reference
include <iostream>
using namespace std;
void increment(int &i,int &j);
int main(){
int number=0;
int number1=3;
increment(number,number1);
cout<<number<<" "<<number1<<endl;
}
void increment(int &i,int &j){
i++;
j++;

}

write function in c++ to swap two variable by using call by reference

include <iostream>
using namespace std;
int swap(int &x,int &y){ //call by referenceتقوم باخذ المعاملات كمرجع
int temp=x;
x=y;
y=temp;
return x,y;
}
int main(){
int x=10;
int y=20;
swap(x,y);
cout<<"x= "<<x<<" "<<"y= "<<y<<endl;
}


write program by c++ to print array={1,5,4,3,2} in function and sort its index and print it after sorting

include <iostream>
using namespace std;
void printarray(int arr[],int size){
for(int i=0;i<size;i++){
cout<<arr[i]<<" ";
}
cout<<endl;
}
int swap(int &x,int &y){
int temp=x;
x=y;
y=temp;
return x,y;//call by value
}

int main(){
const int size=5;
int array[]={1,5,4,3,2};
printarray(array,size);
for(int i=0;i<size;i++){
for(int j=0;j<size-1;j++){//note size-1 because i compaire between j , j+1
if(array[j]>array[j+1]){
swap(array[j],array[j+1]);
}
}
}
printarray(array,size);
}


write a C++ program to print number of zeros of a series of 10 numbers
entered by the user

include <iostream>
using namespace std;

int main()
{
int num;
int count=0;
cout<<"enter number"<<endl;
for(int i=1;i<10;i++)
{
cin>>num;
if(num==0){
count++;
}
}
cout<<"number of zeros number"<<count<<endl;
}


write c++ program which insert 10 number from user then output how many of even number and how many of odd number

using namespace std;

int main()
{
int odd=0,even=0;
int num;
for (int i = 0; i < 10; i++) {
cin>>num;
if (num % 2 == 0){
even++;
cout << num << " is an even number\n";
}
else
{
cout<<num <<" is an odd number\n";
odd++;
}
}
cout<<"the odd number = "<<odd<<endl;
cout<<"the even number = "<<even<<endl;
}


Write a program in C++ to find the factorial of a number?

include <iostream>

using namespace std;

int main()
{
int num;
cin>>num;
int fact=1;
for (int i=1;i<=num;i++){
fact=fact*i;
}
cout<<fact<<endl;
}


Write a program in C++ to display the cube of the number up to given an
integer?
include <iostream>

using namespace std;

int main()
{
int num;
cin>>num;
int cube;
for (int i=1;i<=num;i++){
cube=iii;
}
cout<<"cube number is: "<< cube <<endl;

}

Write a program in C++ that creates a random array whose numbers are between 0 and 19

include <iostream>
include <cstdlib>
include <ctime>
using namespace std;

int main()
{
srand(time(0));
const int size=10;
int grade[size]={0};
for(int i=0;i<size;i++){
grade[i]=rand()%20;
cout<<grade[i]<<endl;
}


Create a repetition array to calculate the number of repetitions for all numbers from 0 to 20. Then print each number and the number of repetitions next to it, and then
By printing the most repeating number as follows:

include <iostream>
using namespace std;
int main()
{
const int size1=21;
const int size = 40;
int vales[size] = {0,1,1,1,2,3,4,5,1,8,2,3,3,1,2,4,6,7,8,9,6,9,8,7,11,12,15,14,15,14,16,16,14,17,18,19,20,14,15,16};
int freqarray[size1]={0};
for(int i=0;i<size;i++){
freqarray[vales[i]]++;
}
for(int i=0;i<size1;i++){
cout<<i<<"\t number of repitation = "<<freqarray[i]<<endl;
}
}


}

Edit

Pub: 31 Dec 2021 16:29 UTC

Edit: 31 Dec 2021 16:34 UTC

Views: 314