0% found this document useful (0 votes)
29 views

Assignment: Function Calls

The document contains 5 programming problems involving function calls, class constructors and destructors, and reversing a 5 digit number. It provides the code to convert temperatures between Celsius and Fahrenheit using a function call by reference. It also includes code for determining if a year is a leap year and calculating the sum of digits in a number using classes.

Uploaded by

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

Assignment: Function Calls

The document contains 5 programming problems involving function calls, class constructors and destructors, and reversing a 5 digit number. It provides the code to convert temperatures between Celsius and Fahrenheit using a function call by reference. It also includes code for determining if a year is a leap year and calculating the sum of digits in a number using classes.

Uploaded by

Bhargavi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Assignment

FUNCTION CALLS
1. Program to convert temperature given in centigrade
into farenheit using call by reference .
by formula :=> F =(9/5) * (C + 32)

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
int ref(int *);
void main()
{ int C; float F;
clrscr();
cout<<"\n Enter temperature in Centigrades :=> ";
cin>>C;
F=ref(&C);
cout<<"\n Temperature in Farenheits is :=> "<<F;
getch();
}
 
int ref(int *c)
{
float f;
f =(9/5) * (*c + 32);
return f;
}
 
OUTPUT:

Enter temperature in Centigrades :=> 23


 
Temperature in Farenheits is :=> 55
 
 
 
 
 
2. Write a program for the call by reference to print
a year is leap year  

#include<iostream.h>
#include<conio.h>
void year(int *);
void main()
{
int yr;
clrscr();
cout<<"Enter a year ";
cin>>yr;
year(&yr);
getch();
}
 
void year(int *YR)

if( ( (*YR/100 && *YR%4) == 0 ) || (*YR/400 == 0 ) )
cout<<*YR<<" is a leap year !";
else
cout<<*YR<<" is not a leap year !";
}
OUTPUT:
Enter a year 2000
 
2000 is a leap year !
 
3. Write an OOP in C++ to read an integer number and
find the sum of all digits until it reduces to a single
digit using a constructor , default constructor ,
destructor , and inline member function.
Eg : n = 1256
Sum = 1 + 2 + 5 +6 = 14
Sum = 1 + 4 = 5  

#include<iostream.h>
#include<conio.h>
 
class Sum
{
unsigned long int No;
 
public : Sum() // default Constructor
{ No = 123; }
 
Sum(unsigned long int n) // Parametrized Constructor
{ No = n; }
  ~Sum() //Destructor
{ cout<<"\n Memory released ! \n"; }
//Inline member function
int inline Totals(unsigned long int);

  void display()
{
cout<<"\n Sum is :=> "<<Totals(No)<<endl;
}
  void getNumber()
{
cout<<"\n Enter a number :=>";
cin >> No;
}
};
int inline Sum :: Totals(unsigned long int n)
{
int total = 0;
while(n != 0)
{
total = total + n%10;
n = n / 10;
}
 
if(total > 9)
Totals(total);
else
return(total);
}
 
void main()
{
Sum S1(6789),S2;
 
clrscr();
cout<<"\n\nObject S1with Default number=6789";
S1.display();
cout<<"\n\nObject S2:\n";
S2.getNumber();
S2.display();
getch();
}
OUTPUT:
Object S1 with Default number=6789
Sum is :=> 3
Object S2:
Enter a number :=>913904
Sum is :=> 8
 
4. Write an OOP in C++ that prints the factorial of a
given number using a copy constructor and a
destructor member function.

#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
 
class Factorial
{
int No;
 
public : Factorial() // default Constructor
{
No = 5;
}
 
Factorial(int n) // Constructor
{
No = n;
}
 
Factorial(Factorial &F) // Copy Constructor
{
No = F.No;
}
~Factorial()
{
cout<<"\n Factorial Memory released ! \n";
}
 
unsigned long int fact(int);
 

 
void display()
{
cout<<"\n Factorial is :=> "<<fact(No)<<endl;
}
 
 
void getNumber()
{
cout<<"\n Enter a number for factorial :=>";
cin >> No;
}
};
 
unsigned long int Factorial :: fact(int n)
{
if(n==0)
{
return (1);
exit(0);
}
else
{
while(n > 0)
{
return (n * fact(n-1));
}
}
}
void main()
{
Factorial F1(3),F2;
 
clrscr();
 
F1.display();
 
F2.getNumber();
F2.display();
Factorial F3(F2);
 
F3.display();
 
getch();
}
 
OUTPUT:
Factorial is :=> 6
 
Enter a number for factorial :=>11
 
Factorial is :=> 39916800
 
Factorial is :=> 39916800
 
Factorial Memory released !
 
Factorial Memory released !
 
Factorial Memory released !
 
 
 
5. A five digit number is entered through keyboard.
Write a program to obtain the reversed number and to
determine whether the reverse number and original
number is equal or not.

#include<iostream.h>
#include<conio.h>
void main()
{
long int no,a,b,c,d,sum,temp;

clrscr();

cout<<"Enter a 5 digit number : ";


cin>>no;
temp = no;
a = (no % 10)*10000;
no = no / 10;
 
b = (no%10)*1000;
no=no/10;
 
c = (no%10)*100;
no=no/10;
 
d = (no%10)*10;
no=no/10;
//sum of digits
sum = a+b+c+d+no;
 
cout<<"Reverse of the number is : "<<sum;
 
if(temp == sum)
cout<<"\nOriginal & reverse numbers are same \n";
else
cout<<"\nOriginal & reverse numbers are not same \n";
getch();
}
OUTPUT:

Enter a 5 digit number : 12121


 
Reverse of the number is : 12121
Original & reverse numbers are same
 
 

You might also like