Write a program using nested If statement while the entered number is
positive and negative?
PROGRAM:
#include <stdio.h>
int main() {
float num;
printf("Enter a number: ");
scanf("%f", &num);
if (num > 0) {
printf("The number is positive.\n");
} else if (num < 0) {
printf("The number is negative.\n");
} else {
printf("The number is zero.\n");
}
OUTPUT:
Enter a number: 5
The number is positive.
Write a program using nested If statement conversion of feet into meters or
meter into feet.
PROGRAM:
#include <stdio.h>
int main() {
float value, result;
int choice;
printf("Choose conversion:\n");
printf("1. Feet to Meters\n");
printf("2. Meters to Feet\n");
printf("Enter your choice (1 or 2): ");
scanf("%d", &choice);
if (choice == 1 || choice == 2) {
printf("Enter the value to convert: ");
scanf("%f", &value);
if (choice == 1) {
result = value * 0.3048;
printf("%.2f feet = %.2f meters\n", value, result);
} else {
result = value / 0.3048;
printf("%.2f meters = %.2f feet\n", value, result);
}
} else {
printf("Invalid choice.\n");
}
}
OUTPUT:
Choose conversion:
1. Feet to Meters
2. Meters to Feet
Enter your choice (1 or 2): 2
Enter the value to convert: 10
10.00 meters = 32.81 feet
write a program using to find area of a
triangle and sphere.
Hint: Area of triangle t = ½ a* b Area of sphere s = 4/3π r³
PROGRAM:
Write a simple program using switch
Statement
PROGRAM:
#include <stdio.h>
int main() {
int choice;
printf("Enter a number (1 to 3): ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("You chose One\n");
break;
case 2:
printf("You chose Two\n");
break;
case 3:
printf("You chose Three\n");
break;
default:
printf("Invalid choice\n");
}
}
OUTPUT:
Enter a number (1 to 3): 2
You chose Two
Write different programs for if, if-else and switch statement.
PROGRAM:
#include <stdio.h>
int main() {
int num;
printf("Enter anumber: ");
scanf("%d", &num);
if (num > 0) {
printf("You entered a positive number.\n");
}
}
OUTPUT:
Enter anumber: 10
You entered a positive number.
ELSE:
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0) {
printf("Even number.\n");
} else {
printf("Odd number.\n");
}
}
OUTPUT:
Enter a number: 5
Odd number.
SWITCH:
#include <stdio.h>
int main() {
char grade;
printf("Enter your grade (A, B, C): ");
scanf(" %c", &grade);
switch (grade) {
case 'A':
printf("Excellent!\n");
break;
case 'B':
printf("Well done!\n");
break;
case 'C':
printf("You passed.\n");
break;
default:
printf("Invalid grade.\n");
}
return 0;
}
OUTPUT:
Enter your grade (A, B, C): C
You passed
Write a program using for loop statement to generate a multiplication table
from 2 to 10.
PROGRAM:
#include <stdio.h>
int main() {
int i, j;
for (i = 2; i <= 10; i++) {
printf("Multiplication Table for %d:\n", i);
for (j = 1; j <= 10; j++) {
printf("%d x %d = %d\n", i, j, i * j);
}
printf("\n");
}
}
OUTPUT:
Sary Table Print Hongy
Write a program to finding a factorial using while loop.
PROGRAM:
#include <stdio.h>
int main() {
int n, i = 1;
unsigned long long factorial = 1;
printf("Enter a positive integer: ");
scanf("%d", &n);
while (i <= n) {
factorial *= i;
i++;
}
printf("Factorial of %d = %llu\n", n, factorial);
}
Write different programs using FOR, While, Do-while loop
PROGRAM:
FOR
#include <stdio.h>
int main() {
int n;
unsigned long long factorial = 1;
printf("Enter a number: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
factorial *= i;
}
printf("Factorial = %llu\n", factorial);
}
WHILE
#include <stdio.h>
int main() {
int n, i = 1;
unsigned long long factorial = 1;
printf("Enter a number: ");
scanf("%d", &n);
if (n == 0) {
factorial = 1;
} else {
do {
factorial *= i;
i++;
} while (i <= n);
}
printf("Factorial = %llu\n", factorial);
}
Write four different ways to use function with or without arguments.
PROGRAM:
4. Four Ways to Use Functions in C
a. Function without argument and without return
#include <stdio.h>
void greet() {
printf("Hello, welcome!\n");
}
int main() {
greet();
return 0;
}
b. Function without argument but with return
#include <stdio.h>
int getNumber() {
return 10;
}
int main() {
int n = getNumber();
printf("Number is %d\n", n);
return 0;
}
c. Function with argument and without return
#include <stdio.h>
void printSquare(int n) {
printf("Square: %d\n", n * n);
}
int main() {
printSquare(5);
return 0;
}
d. Function with argument and with return
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
int sum = add(4, 6);
printf("Sum: %d\n", sum);
return 0;
}
Write a program using user defined function to find the factorial of inputted number
PROGRAM:
#include <stdio.h>
unsigned long long factorial(int n) {
unsigned long long fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
return fact;
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Factorial of %d = %llu\n", num, factorial(num));
return 0;
}