Chapter 8 - Let Us C
Any year is entered through the keyboard. Write a function to determine whether the year is a leap year or not.
#include <stdio.h>
int isLeap(int);
int main()
{
int year;
printf("Enter an year : ");
scanf("%d", &year);
if (isLeap(year))
{
printf("%d is a leap year.\n", year);
}
else
{
printf("%d is not a leap year.\n", year);
}
return 0;
}
int isLeap(int year)
{
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
return 1;
}
else
{
return 0;
}
}
Step 1: Include the `stdio.h` header file for standard input/output functions like `printf` and `scanf`.
Step 2: Define a function `isLeap` that takes an integer `year` as input and returns 1 if it's a leap year, 0 otherwise.
Step 3: The `main` function is the entry point of the program.
Step 4: Declare an integer variable `year` to store the year entered by the user.
Step 5: Prompt the user to enter a year and store it in the `year` variable using `scanf`.
Step 6: Call the `isLeap` function with the entered `year` as an argument. The function returns either 1 (true) or 0 (false).
Step 7: An `if` statement checks the return value of `isLeap(year)`.
Step 8: If `isLeap(year)` is true (1), print a message indicating that the entered year is a leap year.
Step 9: Otherwise (if `isLeap(year)` is false (0)), print a message indicating that the entered year is not a leap year.
Step 10: The program returns 0, indicating successful execution.
Step 11: The `isLeap` function checks if a year is a leap year using the following logic: a year is a leap year if it is divisible by 4 but not divisible by 100, unless it is also divisible by 400.
Step 13: Otherwise, the function returns 0.
Step 2: Define a function `isLeap` that takes an integer `year` as input and returns 1 if it's a leap year, 0 otherwise.
Step 3: The `main` function is the entry point of the program.
Step 4: Declare an integer variable `year` to store the year entered by the user.
Step 5: Prompt the user to enter a year and store it in the `year` variable using `scanf`.
Step 6: Call the `isLeap` function with the entered `year` as an argument. The function returns either 1 (true) or 0 (false).
Step 7: An `if` statement checks the return value of `isLeap(year)`.
Step 8: If `isLeap(year)` is true (1), print a message indicating that the entered year is a leap year.
Step 9: Otherwise (if `isLeap(year)` is false (0)), print a message indicating that the entered year is not a leap year.
Step 10: The program returns 0, indicating successful execution.
Step 11: The `isLeap` function checks if a year is a leap year using the following logic: a year is a leap year if it is divisible by 4 but not divisible by 100, unless it is also divisible by 400.
- `year % 4 == 0` checks divisibility by 4.
- `year % 100 != 0` checks if it's not divisible by 100.
- `year % 400 == 0` checks divisibility by 400.
Step 13: Otherwise, the function returns 0.
A positive integer is entered through the keyboard. Write a function to obtain the prime factors of this number.
#include <stdio.h>
void primeFactors(int num){
int i=2;
while(num>1){
if(num%i==0){
printf("%d ",i);
num /= i;
}
else{
i++;
}
}
}
int main(){
int num;
printf("Enter the number : ");
scanf("%d",&num);
primeFactors(num);
return 0;
}
Step 1: Include the `stdio.h` header file for standard input/output functions like `printf` and `scanf`.
Step 2: Define a function `primeFactors` that takes an integer `num` as input and prints its prime factors.
Step 3: Inside `primeFactors`, initialize an integer variable `i` to 2. This variable will be used to check for potential prime factors.
Step 4: A `while` loop continues as long as `num` is greater than 1. This loop iteratively finds and removes prime factors.
Step 5: Inside the `while` loop, an `if` statement checks if `num` is divisible by `i` (`num % i == 0`).
Step 6: If `num` is divisible by `i`, it means `i` is a prime factor.
Step 8: The `while` loop continues until `num` becomes 1, meaning all prime factors have been found.
Step 9: The `main` function is the entry point of the program.
Step 10: Declare an integer variable `num` to store the number entered by the user.
Step 11: Prompt the user to enter a number and store it in the `num` variable using `scanf`.
Step 12: Call the `primeFactors` function with the entered `num` as an argument.
Step 13: The program returns 0, indicating successful execution.
Step 2: Define a function `primeFactors` that takes an integer `num` as input and prints its prime factors.
Step 3: Inside `primeFactors`, initialize an integer variable `i` to 2. This variable will be used to check for potential prime factors.
Step 4: A `while` loop continues as long as `num` is greater than 1. This loop iteratively finds and removes prime factors.
Step 5: Inside the `while` loop, an `if` statement checks if `num` is divisible by `i` (`num % i == 0`).
Step 6: If `num` is divisible by `i`, it means `i` is a prime factor.
- Print the prime factor `i`.
- Divide `num` by `i` (`num /= i`) to remove the factor and continue finding other prime factors.
Step 8: The `while` loop continues until `num` becomes 1, meaning all prime factors have been found.
Step 9: The `main` function is the entry point of the program.
Step 10: Declare an integer variable `num` to store the number entered by the user.
Step 11: Prompt the user to enter a number and store it in the `num` variable using `scanf`.
Step 12: Call the `primeFactors` function with the entered `num` as an argument.
Step 13: The program returns 0, indicating successful execution.
Comments