12 CONTOH PROGRAM DENGAN FUNGSI MODULAR C++
Selamat pagi sobat semua. bagi sahabat yang sedang belajar bahasa C/C++ berikut saya share contoh program C yang menggunakan fungsi modular didalamnya. soal ini dapat sobat gunakan dalam latihan.
Kasus 1
#include <iostream>
using namespace std;
int addition(int a, int b)
{
int r;
r=a+b;
return r;
}
int main ()
{
int z;
z=addition(5,3);
cout<<"The Result is "<<z;
}
Kasus 2
#include <iostream>
using namespace std;
int subtraction (int a, int b)
{
int r;
r=a-b;
return r;
}
int main ()
{
int x=5 , y=3 , z;
z=subtraction (7,2);
cout<<"The first result is "<<z<<'\n';
cout<<"The second result is "<<subtraction (7,2)<<'\n';
cout<<"The third result is "<<subtraction (x,y)<<'\n';
z=4+subtraction (x,y);
cout<<"The fourt result is "<<z<<'\n';
}
Kasus 3
#include <iostream>
using namespace std;
void printmessage ()
{
cout<<"I'm a function";
}
int main()
{
printmessage ();
}
Kasus 4
#include <iostream>
using namespace std;
int divide (int a, int b=2)
{
int r;
r=a/b;
return (r);
}
int main()
{
cout<<divide(12)<<'\n';
cout<<divide(20,4)<<'\n';
return 0;
}
Kasus 5
#include <iostream>
using namespace std;
//function declaration
int max (int num1, int num2);
int main()
{
//local variabel declaration
int a=100;
int b=200;
int ret;
//calling a function to get max value.
ret=max(a,b);
cout<<"Max value is : "<<ret<<endl;
return 0;
}
//function returning the max between two numbers
int max(int num1, int num2) {
//local variable declaration
int result;
if (num1 > num2)
result = num1;
else
result=num2;
return result;
}
Kasus 6
#include <iostream>
using namespace std;
int sum ( int a, int b=20)
{
int result;
result =a+b;
return (result);
}
int main ()
{
//local variable declaration:
int a = 100;
int b = 200;
int result;
//calling a function to add the values.
result=sum(a,b);
cout<<"Total value is : "<<result<<endl;
//calling a function again as follows.
result=sum(a);
cout<<"Total value is : "<<result<<endl;
return 0;
}
Kasus 7
#include <iostream>
using namespace std;
//function prototype (declaration)
int add(int, int);
int main ()
{
int num1, num2, sum;
cout<<"Enter two numbers to add : \n";
cin>>num1>>num2;
//function call
sum = add (num1, num2);
cout<<"Sum = "<<sum;
return 0;
}
//function definition
int add (int a, int b)
{
int add;
add=a+b;
//return statement
return add;
}
Kasus 8
#include <iostream>
using namespace std;
//function
bool checkifpositive(int x)
{
if (x>=0)
return true;
return false;
}
//procedure
void printifpositive (int x)
{
bool ispositive = checkifpositive(x);
if(ispositive)
cout<<"X is positive and its value is " << x << endl;
}
int main()
{
printifpositive(3);
printifpositive(-54);
printifpositive(710);
return 0;
}
Kasus 9
#include <iostream>
using namespace std;
void odd(int x);
void even(int x);
int main()
{
int i;
do {
cout<<"Please enter number (0 to exit) : ";
cin>>i;
odd(i);
return 0;
} while (i!=0);
return 0;
}
void odd(int x)
{
if ((x%2)!=0) cout <<"It is odd. /n";
else even (x);
}
void even (int x)
{
if((x%2)==0) cout<<"it is even. /n";
else odd (x);
}
Kasus 10
#include <iostream>
using namespace std;
long factorial (long a)
{
if (a>1)
return (a*factorial (a-1));
else
return 1;
}
int main()
{
long number =9;
cout<<number <<" ! = "<<factorial (number);
return 0;
}
Kasus 11
#include <iostream>
using namespace std;
int logika (int a, int b)
{
int i, sum=0, sum1=0, sum2=0;
if(a<b){
for (i=a; i<=b; i++){
sum=sum+i;
}
return sum;
}
else if(a>b){
for(i=a; i>=b; i--){
sum1=sum1+i;
}
return sum1;
}
else if (a==b){
sum2=a;
return sum2;
}
}
//int kurang (int a, int b)
int main()
{
int first, last, total;
cout<<"Masukkan nilai awal dan nilai akhir : \n";
cin>>first>>last;
total=logika(first, last);
cout<<"Jumlah dari semua nila pada rentan bilangan awal dan akhir : ";
cout<<total;
return 0;
}
Kasus 12
#include <iostream>
using namespace std;
bool valid(int x){
if(x>=15){
return true;
}
else{
return false;
}
}
void syarat (int x){
bool finalpositive;
finalpositive=valid(x);
if(finalpositive){
cout<<'\n'<<"===========================================================\n";
cout<<"Silahkan Masuk!";
}
else{
cout<<'\n'<<"===========================================================\n";
cout<<"DILARANG MASUK!";
}
}
int main(){
int umur, umurortu, selisih;
string namaanak, namaortu;
cout<<"Masukkan Nama Kamu dan Nama Orang Tuamu : \n";
cin>>namaanak>>namaortu;
cout<<"Masukkan Umur Kamu dan Umur Orang Tuamu : \n";
cin>>umur>>umurortu;
selisih = umurortu-umur;
syarat(selisih);
return 0;
}
Baca Juga :