Chapter 10 - Let Us C
A positive integer is entered through the keyboard, write a function to find the binary equivalent of this number without using recursion.
#include <stdio.h>
long int bin_f(int dec){
long int bin = 0;
int rem,place = 1;
while(dec){
bin += (dec%2)*place;
dec /= 2;
place *= 10;
}
return bin;
}
int main(){
int num;
long int bin;
printf("Enter your number : ");
scanf("%d",&num);
bin = bin_f(num);
printf("The binary equivalent of %d = %ld",num,bin);
return 0;
}
Include the `stdio.h` header file.
This header provides standard input/output functions, allowing the program to interact with the user via `printf` (for output) and `scanf` (for input).
Define the function `bin_f`.
This function takes an integer `dec` (decimal number) as input and returns its binary equivalent as a `long int`.
It uses an iterative approach to convert the decimal number to binary.
Inside the `bin_f` function, initialize `bin` to 0, `rem` to store the remainder, and `place` to 1.
`bin` will store the resulting binary number.
`place` is used to build the binary number digit by digit, representing powers of 10.
Enter a `while` loop that continues as long as `dec` is not zero.
This loop iteratively converts the decimal number to binary.
Inside the loop, calculate the remainder when `dec` is divided by 2 (`dec % 2`).
This remainder (0 or 1) represents the current binary digit.
Add the remainder multiplied by `place` to `bin`.
`bin += (dec % 2) * place;` This effectively adds the current binary digit to the correct position in the `bin` variable.
Divide `dec` by 2 (`dec /= 2`).
This shifts the decimal number to the right, preparing for the next binary digit.
Multiply `place` by 10 (`place *= 10`).
This shifts the position for the next binary digit to the left (effectively multiplying by powers of 10).
The `while` loop continues until `dec` becomes 0, at which point all binary digits have been extracted.
Step 10:Return the `bin` value, which now contains the binary equivalent of the input decimal number.
Step 11:The `main` function is the program's entry point.
Step 12:Declare an integer variable `num` to store the decimal number entered by the user, and a `long int` variable `bin` to store the resulting binary number.
Step 13:Prompt the user to enter a number using `printf` and store it in `num` using `scanf`.
Step 14:Call the `bin_f` function with `num` as an argument and store the returned binary value in `bin`.
Step 15:Print the binary equivalent of the entered decimal number using `printf`.
Step 16:Return 0 from `main`, indicating successful program execution.
A positive integer is entered through the keyboard, write a function to find the binary equivalent of this number using recursion.
#include <stdio.h>
long int bin_f(int dec){
if(dec == 0){
return 0;
}
else{
return ((dec%2) + 10*bin_f(dec/2));
}
}
int main(){
int num;
long int bin;
printf("Enter your number : ");
scanf("%d",&num);
bin = bin_f(num);
printf("The binary equivalent of %d = %ld",num,bin);
return 0;
}
Include the `stdio.h` header file.
This header provides standard input/output functions, allowing the program to interact with the user via `printf` (for output) and `scanf` (for input).
Define the recursive function `bin_f`.
This function takes an integer `dec` (decimal number) as input and returns its binary equivalent as a `long int`.
It uses recursion to convert the decimal number to binary.
Inside the `bin_f` function, check for the base case: if `dec` is 0, return 0.
`if (dec == 0)` checks if the decimal number is 0.
`return 0;` terminates the recursion, as the binary equivalent of 0 is 0.
If `dec` is not 0, recursively calculate the binary equivalent.
`return ((dec % 2) + 10 * bin_f(dec / 2));`
`dec % 2` calculates the remainder when `dec` is divided by 2, which gives the least significant binary digit (0 or 1).
`dec / 2` performs integer division, effectively shifting the decimal number to the right (removing the least significant digit).
`bin_f(dec / 2)` recursively calls the `bin_f` function with the shifted decimal number.
`10 * bin_f(dec / 2)` multiplies the result of the recursive call by 10, effectively shifting the previously calculated binary digits to the left (making space for the next digit).
The least significant digit (`dec % 2`) is then added to this shifted value, building the binary number digit by digit from right to left.
The `main` function is the program's entry point.
Step 6:Declare an integer variable `num` to store the decimal number entered by the user, and a `long int` variable `bin` to store the resulting binary number.
Step 7:Prompt the user to enter a number using `printf` and store it in `num` using `scanf`.
Step 8:Call the `bin_f` function with `num` as an argument and store the returned binary value in `bin`.
Step 9:Print the binary equivalent of the entered decimal number using `printf`.
Step 10:Return 0 from `main`, indicating successful program execution.
Write a recursive function to obtain the sum of first 25 natural numbers.
#include <stdio.h>
int nat_sum(int lim)
{
if (lim == 1)
{
return 1;
}
else
{
return (lim + nat_sum(lim - 1));
}
}
int main()
{
int sum;
sum = nat_sum(25);
printf("The sum of first 25 natural number is %d", sum);
return 0;
}
Include the `stdio.h` header file.
This header provides standard input/output functions, allowing the program to interact with the user via `printf` (for output).
Define the recursive function `nat_sum`.
This function takes an integer `lim` as input, representing the upper limit of the natural numbers to sum.
It returns the sum of natural numbers from 1 to `lim`.
Inside the `nat_sum` function, check for the base case: if `lim` is 1, return 1.
`if (lim == 1)` checks if there is only one number to sum.
`return 1;` terminates the recursion, as the sum of the first 1 natural number is 1.
If `lim` is not 1, recursively calculate the sum.
`return (lim + nat_sum(lim - 1));`
This line adds the current limit `lim` to the sum of the natural numbers from 1 to `lim - 1`, which is calculated recursively.
`nat_sum(lim - 1)` makes a recursive call, reducing the limit by 1 in each call until the base case is reached.
The `main` function is the program's entry point.
Step 6:Declare an integer variable `sum` to store the calculated sum.
Step 7:Call the `nat_sum` function with the argument 25 and store the returned sum in the `sum` variable.
`sum = nat_sum(25);` This calculates the sum of the first 25 natural numbers.
Print the calculated sum using `printf`.
Step 9:Return 0 from `main`, indicating successful program execution.
There are three pegs labeled A, B and C. Four disks are placed on
peg A. The bottom-most disk is largest, and disks go on decreasing
in size with the topmost disk being smallest. The objective of the
game is to move the disks from peg A to peg C, using peg B as an
auxiliary peg. The rules of the game are as follows:
(1) Only one disk may be moved at a time, and it must be the top
disk on one of the pegs.
(2) A larger disk should never be placed on the top of a smaller
disk.
Write a program to print out the sequence in which the disks should
be moved such that all disks on peg A are finally transferred to peg
C.
#include <stdio.h>
void peg(int n, char source, char auxiliary, char destination)
{
if (n == 1)
{
printf("Move disk 1 from %c to %c\n", source, destination);
return;
}
peg(n-1,source,destination,auxiliary);
printf("Move disk %d from %c to %c\n",n,source,destination);
peg(n-1,auxiliary,source,destination);
}
int main()
{
peg(4,'A','B','C');
return 0;
}
This header provides standard input/output functions, allowing the program to interact with the user via `printf` (for output) and `scanf` (for input).
This function simulates the Tower of Hanoi puzzle.
It takes four arguments: `n` (number of disks), `source` (source peg), `auxiliary` (auxiliary peg), and `destination` (destination peg).
`if (n == 1)` checks if there is only one disk to move.
`printf("Move disk 1 from %c to %c\n", source, destination);` prints the move instruction.
`return;` terminates the recursive call for this base case.
`peg(n-1, source, destination, auxiliary);`
This moves the top `n-1` disks from the source peg to the auxiliary peg, using the destination peg as the temporary peg.
`printf("Move disk %d from %c to %c\n", n, source, destination);`
This moves the `n`th (largest) disk from the source peg to the destination peg.
`peg(n-1, auxiliary, source, destination);`
This moves the `n-1` disks from the auxiliary peg to the destination peg, using the source peg as the temporary peg.
The `main` function is the program's entry point.
Step 8: Call the `peg` function with the arguments 4, 'A', 'B', and 'C'.`peg(4, 'A', 'B', 'C');`
This initiates the Tower of Hanoi puzzle with 4 disks, source peg 'A', auxiliary peg 'B', and destination peg 'C'.
Return 0 from `main`, indicating successful program execution.
Comments