Contoh program C++ dengan input, output dan percabangan IF. adapun programanya sebagai berikut:
Source code
#include <iostream> #include <conio.h> //#include <stdlib.h> using namespace std; main(){ string nama1,nama2,nama3,mk1,mk2,mk3; long int nim1,nim2,nim3; float na1,na2,na3; int a; char nh1,nh2,nh3; cout<<"-------------------------------------------\n"; cout<<"| |\n"; cout<<"| Latihan Program IF |\n"; cout<<"| |\n"; cout<<"-------------------------------------------\n"; cout<<"NIM : ";cin>>nim1; cout<<"Nama : ";cin>>nama1; cout<<"MK : ";cin>>mk1; cout<<"Nilai Angka : ";cin>>na1; cout<<"-------------------------------------------\n"; cout<<"NIM : ";cin>>nim2; cout<<"Nama : ";cin>>nama2; cout<<"MK : ";cin>>mk2; cout<<"Nilai Angka : ";cin>>na2; cout<<"-------------------------------------------\n"; cout<<"NIM : ";cin>>nim3; cout<<"Nama : ";cin>>nama3; cout<<"MK : ";cin>>mk3; cout<<"Nilai Angka : ";cin>>na3; cout<<"-------------------------------------------\n"; //Untuk Data 1 if (na1>=85 && na1<=100){ nh1='A'; cout<<"-------------------------------------------\n"; }else if(na1>=75 && na1<=84){ nh1='B'; cout<<"-------------------------------------------\n"; }else if(na1>=60 && na1<=74){ nh1='C'; cout<<"-------------------------------------------\n"; }else if(na1>=50 && na1<=59){ nh1='D'; cout<<"-------------------------------------------\n"; }else if(na1>=0 && na1<=49){ nh1='E'; cout<<"-------------------------------------------\n"; }else{ cout<<"===========================================\n"; cout<<"| Maaf Data yang anda input Salah |\n"; cout<<"===========================================\n"; } //Untuk Data 2 if (na2>=85 && na2<=100){ nh2='A'; cout<<"-------------------------------------------\n"; }else if(na2>=75 && na2<=84){ nh2='B'; cout<<"-------------------------------------------\n"; }else if(na2>=60 && na2<=74){ nh2='C'; cout<<"-------------------------------------------\n"; }else if(na2>=50 && na2<=59){ nh2='D'; cout<<"-------------------------------------------\n"; }else if(na2>=0 && na2<=49){ nh2='E'; cout<<"-------------------------------------------\n"; }else{ cout<<"===========================================\n"; cout<<"| Maaf Data yang anda input Salah |\n"; cout<<"===========================================\n"; } //Untuk Data 3 if (na3>=85 && na3<=100){ nh3='A'; cout<<"-------------------------------------------\n"; }else if(na3>=75 && na3<=84){ nh3='B'; cout<<"-------------------------------------------\n"; }else if(na3>=60 && na3<=74){ nh3='C'; cout<<"-------------------------------------------\n"; }else if(na3>=50 && na3<=59){ nh3='D'; cout<<"-------------------------------------------\n"; }else if(na3>=0 && na3<=49){ nh3='E'; cout<<"-------------------------------------------\n"; }else{ cout<<"===========================================\n"; cout<<"| Maaf Data yang anda input Salah |\n"; cout<<"===========================================\n"; } //system("cls"); cout<<"==========================================="<<endl; cout<<"| |"<<endl; cout<<"| Hasil Akhir Mahasiswa |"<<endl; cout<<"| |"<<endl; cout<<"==========================================="<<endl; cout<<"Nama : "<<nama1<<endl; cout<<"Hilai Huruf : "<<nh1<<endl; cout<<"-------------------------------------------\n"; cout<<"Nama : "<<nama2<<endl; cout<<"Hilai Huruf : "<<nh2<<endl; cout<<"-------------------------------------------\n"; cout<<"Nama : "<<nama3<<endl; cout<<"Hilai Huruf : "<<nh3<<endl; cout<<"-------------------------------------------\n"; }