Function Main
Declare Integer num
Output "Enter the Binary number: "
Input num
Output "The decimal number is:"
Output BinaryToDecimal(num)
End
Function BinaryToDecimal (Integer num)
Declare Integer rem
Declare Integer sum
Declare Integer power
Assign sum = 0
Assign power = 0
While num > 0
Assign rem = num MOD 10
Assign num = num / 10
Assign sum = sum + rem * POW(2, power)
Assign power = power + 1
End
Return Integer sum
Function POW (Integer Base, Integer Exponent)
Declare Integer Result
If Exponent = 0
Assign Result = 1
Else
Assign Result = Base * POW(Base, Exponent -1)
End
Return Integer Result
#include <stdio.h>
int BinaryToDecimal(int num);
int POW(int Base, int Exponent);
int main() {
int num;
printf("Enter the Binary number: \n");
scanf("%d", &num);
printf("The Decimal number is:\n");
printf("%d\n", BinaryToDecimal(num));
return 0;
}
int BinaryToDecimal(int num) {
int rem;
int sum;
int power;
sum = 0;
power = 0;
while (num > 0) {
rem = num % 10;
num = (int) ((double) num / 10);
sum = sum + rem * POW(2, power);
power = power + 1;
}
return sum;
}
int POW(int Base, int Exponent) {
int Result;
if (Exponent == 0) {
Result = 1;
} else {
Result = Base * POW(Base, Exponent - 1);
}
return Result;
}
Function Main
Declare Integer i, j, r
Output "Input number of rows : "
Input r
For i = 0 to r
For j = 1 to r-i
Output " "
End
For j = 1 to 2*i-1
Output "*"
End
Output ""
End
For i = r-1 to 1 decreasing
For j = 1 to r-i
Output " "
End
For j = 1 to 2*i-1
Output "*"
End
Output ""
End
End
Function Main
Declare Integer i, j, space, rows, k
Output "Input number of rows : "
Input rows
Assign space = rows+4-1
For i = 1 to rows
For k = space to 1 decreasing
Output " "
End
For j = 1 to i
Output "* "
End
Output ""
Assign space = space -1
End
End
Function Main
float var = 23.564327;
Declaring pointer variables upto level_4
Initializing pointer variables
ptr1 = &var;
ptr2 = &ptr1;
ptr3 = &ptr2;
ptr4 = &ptr3;
Output values
"Value of var = %f\n", var);
"Value of var using level-1"
" pointer = %f\n",
*ptr1);
"Value of var using level-2"
" pointer = %f\n",
**ptr2);
"Value of var using level-3"
" pointer = %f\n",
***ptr3);
"Value of var using level-4"
" pointer = %f\n",
****ptr4);
End
Value of var = 23.564327
Value of var using level-1 pointer = 23.564327
Value of var using level-2 pointer = 23.564327
Value of var using level-3 pointer = 23.564327
Value of var using level-4 pointer = 23.564327
Function Main
Declare Integer A
Declare Integer B
Declare Integer res
Output "Insert 2 numbers to calculate GCD"
Output "First number: "
Input A
Output "Second number"
Input B
Assign res = GCD(a,b)
Output res
End
Function GCD (Integer a, Integer b)
Declare Integer res
If a = 0
Assign res = b
Else
Assign res = GCD(b MOD a, a)
End
Return Integer res
#include <stdio.h>
int GCD(int a, int b);
int main() {
int a;
int b;
int res;
printf("Insert 2 numbers to calculate GCD:\n");
printf("First number: \n");
scanf("%d", &a);
printf("Second number:\n");
scanf("%d", &b);
res = GCD(a, b);
printf("%d\n", res);
return 0;
}
int GCD(int a, int b) {
int res;
if (a == 0) {
res = b;
} else {
res = GCD(b % a, a);
}
return res;
}
Function Main
Declare Integer fact1
Declare Integer fact2
Output " First factor"
Input fact1
Output "Second factor"
Input fact2
Output multiply(fact1, fact2)
End
Function multiply (Integer x, Integer y)
Declare Integer result
If y = 0
Assign result = 0
End
If y > 0
Assign result = x + multiply(x, y-1)
End
If y < 0
Assign result = -multiply(x, -y)
End
Return Integer result
#include <stdio.h>
int multiply(int x, int y);
int main() {
int fact1;
int fact2;
printf("First factor:\n");
scanf("%d", &fact1);
printf("Second factor:\n");
scanf("%d", &fact2);
printf("%d\n", multiply(fact1, fact2));
return 0;
}
int multiply(int x, int y) {
int result;
if (y == 0) {
result = 0;
}
if (y > 0) {
result = x + multiply(x, y - 1);
}
if (y < 0) {
result = (int) (-multiply(x, (int) (-y)));
}
return result;
}
Function Main
Declare Integer n1, n2
Output "This is a call by value add(2, 30):"
Output add(2, 3)
Output "Input first number (n1):"
Input n1
Output "Input second number(n2):"
Input n2
Output "This is a call by reference add(n1, n2):"
Output add(n1, n2)
End
Function add (Integer first, Integer second)
Declare Integer result
Assign result = first + second
Return Integer result
#include <stdio.h>
int add(int first, int second);
int main() {
int n1, n2;
printf("This is a call by value add(2, 3):\n");
printf("%d\n", add(2, 3));
printf("Input first number(n1):\n");
scanf("%d", &n1);
printf("Input second number(n2):\n");
scanf("%d", &n2);
printf("This is a call by reference add(n1, n2):\n");
printf("%d\n", add(n1, n2));
return 0;
}
int add(int first, int second) {
int result;
result = first + second;
return result;
}
Function Main
Declare Real turns
Output "Input number"
Input turns
Output ToString(deg2rad(turns)) & " radians"
Output ToString(rad2deg(turns)) & " grades"
End
Function deg2rad (Real x)
Declare Real r
Assign r = x*pi/180
Return Real r
Function rad2deg (Integer x)
Declare Real r
Assign r = 180*x/pi
Return Real r
Declare integer variable m
Declare integer pointer variable ab
Assign 29 to m
Print the address of m and its value
Print the address of ab and the content it points to
Assign the address of m to ab
Print the address of ab and the content it points to
Assign 34 to m
Print the address of ab and the content it points to
Assign 7 to the content of ab
Print the value of m
#include <stdio.h>
int main()
{
//! showMemory()
int* ab;
int m;
m=29;
printf(" Address of m : %p\n",&m);
printf(" Value of m : %d\n\n",m);
ab=&m;
printf(" Address of pointer ab : %p\n",ab);
printf(" Content of pointer ab : %d\n\n",*ab);
m=34;
printf(" Address of pointer ab : %p\n",ab);
printf(" Content of pointer ab : %d\n\n",*ab);
*ab=7;
printf(" Value of m : %d\n\n",m);
return 0;
}
Address of m : 0x7ffeeed0d9ec
Value of m : 29
Address of pointer ab : 0x7ffeeed0d9ec
Content of pointer ab : 29
Address of pointer ab : 0x7ffeeed0d9ec
Content of pointer ab : 34
Value of m : 7