Write a C program to print even numbers ranging from M to N (including M and N values).
- Declare two integer variables to store the values of M and N.
- Use the printf function to prompt the user to enter the values of M and N.
- Use the scanf function to read the values of M and N from the user.
- Use a loop (for or while) to iterate from M to N.
- Inside the loop, check if the current number is even.
- If the current number is even, print it.
- Continue the loop until you have iterated through all numbers from M to N.
#include <stdio.h>
int main() {
int M, N, i;
printf("Enter the starting number (M): ");
scanf("%d", &M);
printf("Enter the ending number (N): ");
scanf("%d", &N);
printf("Even numbers from %d to %d are:\n", M, N);
for (i = M; i <= N; i++) {
if (i % 2 == 0)
printf("%d ", i);
}
printf("\n");
return 0;
}
Thus the program to print even numbers ranging from M to N (including M and N values) has been executed successfully
Write a C program to print the given triangular pattern using loop.
- Declare a variable to store the number of rows in the triangle.
- Use the printf function to prompt the user to enter the number of rows.
- Use a loop (for or while) to iterate through each row.
- Inside the loop, use another loop to print the desired number of asterisks for each row.
- Continue the loop until you have printed the entire triangular pattern.
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++) {
for (j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
Thus the program to print the given triangular pattern using loop has been executed successfully
Write a C program to perform addition and subtraction of two numbers using functions (with argument and without return type).
- Declare two functions, one for addition and one for subtraction. Both functions should take two integer arguments.
- Inside the addition & subtraction function, add & subtract the two numbers and print the result.
- In the main function, declare two integer variables and read their values from the user.
- Call the addition and subtraction functions, passing the two numbers as arguments.
#include <stdio.h>
void add(int a, int b);
void subtract(int a, int b);
int main() {
int num1, num2;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
add(num1, num2);
subtract(num1, num2);
return 0;
}
void add(int a, int b) {
int sum = a + b;
printf("Addition = %d\n", sum);
}
void subtract(int a, int b) {
int diff = a - b;
printf("Subtraction = %d\n", diff);
}
Thus the program to perform addition and subtraction of two numbers using functions has been executed successfully
Write a c program to find the sum of odd digits using for loop
- Declare variables to store the input number and the sum of odd digits.
- Initialize the sum of odd digits to 0.
- Use a for loop to iterate through each digit of the input number.
- Inside the loop, extract the rightmost digit of the number (using the modulo operator % and division by 10).
- If the digit is odd, add it to the sum of odd digits.
- Print the sum of odd digits.
#include <stdio.h>
int main() {
int num, temp, digit, sum = 0;
printf("Enter a number: ");
scanf("%d", &num);
for (temp = num; temp != 0; temp /= 10) {
digit = temp % 10;
if (digit % 2 != 0)
sum += digit;
}
printf("Sum of odd digits in %d is: %d\n", num, sum);
return 0;
}
Thus the program to find the sum of odd digits using for loop has been executed successfully.
To write a C program that calculates the factorial of a given number using a user-defined function.
- Start
- Declare the function fact().
- In the main() function, call the fact() function.
- In fact() function: a. Declare variables i, N, and fact (initialized to 1). b. Read an integer N from the user. c. Use a for loop from 1 to N: i. Multiply fact by i in each iteration. d. After the loop, print the factorial value.
- End
#include <stdio.h>
long factorial(int n); // Function prototype
int main() {
int num;
long fact;
printf("Enter a number: ");
scanf("%d", &num);
if (num < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
fact = factorial(num);
printf("Factorial of %d is: %ld\n", num, fact);
}
return 0;
}
long factorial(int n) {
long result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
The program correctly computes the factorial of a given number using a separate function and displays the result.




