Algorithm & programming part II

Alhamdulillah, we meet again in this 2nd meeting.

In this case, I;m gonna tell you about Branching. Branching is a way to make decision when it is true or false. You can determine what should be done by program when it’s true, and when it’s false.There are 3 types of branching in pascal and C

type pascal C
1 If [….condition…] then [statement…] If (…condition…) statement….
2 If [….condition…] thenbegin[statement…][statement…]

end;

If (…condition…){statement….statement….

}

3 Case [var] ofbegin1 :….;…..;

end;

Swith (var){case 1:….;……………..;

}

program to determine odd and even

pascal


program number;
uses crt;
var
a,b:integer;
begin
write('please input a number :');read(a);
b=a mod 2;
if b=0 then
writeln('the number is genap');
else
writeln('the number is ….');
end.

C

#include <stdio.h.>
int main(){
int a,b;
printf(“input a number :”);scanf(”%d”,&a);
b=a%2;
if (b==0)
printf(“the number you entered is genap\n”);
else
printf(“the number you entered is ganjil”);
}

program to determine maximum and minimum value

Pascal

program maxmin;
uses crt;
var
number1,number2,number3:integer;
begin

write('input 1st number :');read(number1);
write('input 2nd number :');read(number2);
write('input 3rd number :');read(number3);

if (number1>number2) and (number1>number3) then
 writeln('1st number is the maximum :',number1)
else if (number2>number1) and (number2>number3) then
 writeln('2nd number is the maximum :',number2)

else if (number3>number1) and (number3>number2) then
 writeln('3rd number is the maximum :',number3)
else
writeln('there is same number');
end.

C

#include <stdio.h>

int main(){
int number1,number2,number3;
 printf("number1 = ");scanf("%d",&number1);
 printf("number2 = ");scanf("%d",&number2);
 printf("number3 = ");scanf("%d",&number3);

 if (number1>number2 && number1>number3)
 printf("max=number1 :%i\n",number1);
 else if (number2>number1 && number2>number3)
 printf("max=number2 :%i\n",number2);
 else if (number3>number2 && number3>number1)
 printf("max=number3 :%i\n",number3);
 else
 printf("there is same number");
}

program to solve 3 or more conditions

Pascal

 

program month;
uses crt;
var
number:integer;
begin

write('input a number :');read(number);
case number of
1:writeln('january');
2:writeln('february');
3:writeln('march');
4:writeln('april');
5:writeln('mei');
6:writeln('june');
7:writeln('july');
8:writeln('august');
9:writeln('september');
10:writeln('october');
11:writeln('november');
12:writeln('december');
else
writeln('out of range');
end;

end.

C

#include <stdio.h>

int main(){
int number;
 printf("number = ");scanf("%d",&number);
 switch (number){
 case 1:printf("january");break;
 case 2:printf("february");break;
 case 3:printf("march");break;
 case 4:printf("april");break;
 case 5:printf("mei");break;
 case 6:printf("june");break;
 case 7:printf("july");break;
 case 8:printf("august");break;
 case 9:printf("september");break;
 case 10:printf("october");break;
 case 11:printf("november");break;
 case 12:printf("december");break;
 default :printf("out of range");break;
 }
 printf("\n");
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: