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

Functions and Parameter Passing: Program 1

The document discusses three programs that use functions and pass by reference in C++. Program 1 converts time from 24-hour to 12-hour notation using a function. Program 2 validates date input using a function. Program 3 counts even, odd and zero digits in a number using a function.

Uploaded by

naseer ALMTIRY
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Functions and Parameter Passing: Program 1

The document discusses three programs that use functions and pass by reference in C++. Program 1 converts time from 24-hour to 12-hour notation using a function. Program 2 validates date input using a function. Program 3 counts even, odd and zero digits in a number using a function.

Uploaded by

naseer ALMTIRY
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

LAB

Functions and
Parameter Passing

K E Y WO RD S :

formal parameter, call by value, call by reference, local variable, global variable, static variable

L AB E XE RC I SE :

Program 1:
Write a program to convert the time from 24-hour notation to 12-hour notation. Your program must
contain a function that converts the time from 24-hour notation to 12-hour notation. For 12-hour time
notation, your program must display AM or PM.

Use pass by reference method.

#include<iostream>
#include<math.h>
#include<string.h>
#include<stdio.h>
using namespace std;

void convert24to12(int &hours, int minutes, int &ampm)


{
if(hours>12)
{
hours=hours-12;
ampm=1;
}
else
{
if(hours==12)
{
hours=12;
ampm=1;
}
if(hours==0)
{
hours=12;
ampm=0;
}
}
}
int main()
{
int h,m, ampm=0;

cout<<"Enter Time in 24-hour notation (H M) : "<<endl;


cin>>h>>m;

cout<<"Time in 24-hour notation : "<<h<<":"<<m<<endl;

convert24to12(h, m, ampm);

if(ampm==0)
cout<<"Time in 12-hour notation : "<<h<<":"<<m<<" A.M"<<endl;
else
cout<<"Time in 12-hour notation : "<<h<<":"<<m<<" P.M"<<endl;

system("PAUSE");
return 0;
}

Sample Output: Program 1

Program 2:
Write a function that accept integer values for day, month and year and return them in valid format. If day
is above 30, then it convert day to 30. If month is above 12 then it convert month to 12. Similarly if day
or month is below 1 then it converts them to 1. Finally it ensures that the years are between 2000 and
2015. If all inputs are valid then it returns true else it returns false. Write a program that prompts user to
input day, month and year. The program then validates them using the function written above and
displays validate if all the input are valid. Otherwise the program displays invalid and the corrected values
of input returned by the function.

Use pass by reference method.

#include<iostream>
#include<math.h>
#include<string.h>
#include<stdio.h>
using namespace std;
int validateDate(int &d, int &m, int &y)
{
int error=0;

if(d>30)
{
d=30;
error=1;
}
if(d<1)
{
d=1;
error=1;
}
if(m>12)
{
m=12;
error=1;
}
if(m<1)
{
m=1;
error=1;
}
if(y>2014)
{
y=2014;
error=1;
}
if(y<2000)
{
y=2000;
error=1;
}
return error;
}

int main()
{
int d,m,y;
cout<<"Enter Date between 1/1/2000 to 30/12/2014 in format (D M Y) :
"<<endl;
cin>>d>>m>>y;
cout<<"Date Entered : "<<d<<"/"<<m<<"/"<<y<<endl;
int isvalid = validateDate(d, m, y);
if(isvalid==0)
cout<<"You entered Valid Date : "<<d<<"/"<<m<<"/"<<y<<endl;
else
{
cout<<"You entered InValid Date !"<<endl<<"Corrected Date is :
"<<d<<"/"<<m<<"/"<<y<<endl;
}

system("PAUSE");
return 0;
}
Sample Output: Program 2
Program 3:
Write a function that takes as parameter an integer (as a long value) and returns the number of odd, even,
and zero digits. Also write a program to test your function.

Use pass by reference method.

#include<iostream>
#include<math.h>
#include<string.h>
#include<stdio.h>
using namespace std;

void checkDigits(long int no, int &evens, int &odds, int &zeros)
{
while(no>0)
{
int digit = no%10;
if(digit==0)
{
zeros++;
}
else if(digit%2==0)
{
evens++;
}
else
{
odds++;
}
no=no/10;
}
}

int main()
{
long int no;
int evens=0,odds=0,zeros=0;

cout<<"Enter any no :"<<endl;


cin>>no;

checkDigits(no, evens, odds, zeros);

cout<<"No of Even Digits : "<<evens<<endl;


cout<<"No of Odd Digits : "<<odds<<endl;
cout<<"No of Zero Digits : "<<zeros<<endl;

system("PAUSE");
return 0;
}

Sample Output: Program

You might also like