C PROGRAM
17.10.2024
1. Calculate a cube of a number with a separate function cube.
PROGRAM:
#include<stdio.h>
int cube(int);
int main(){
int a,b;
printf("enter a number:");
scanf("%d",&a);
b=cube(a);
printf("cube of a given number is:%d",b);
}
int cube(int a){
int a3;
a3=a*a*a;
return a3;
}
OUTPUT:
enter a number:2
cube of a given number is:8
2. Write a C program to display greet message based on the time given as
input. For example if time is 8, display “Good Morning” if 16:30 display as Good
Evening”.
PROGRAM:
#include<stdio.h>
int main(){
int time;
printf("enter the time: ");
scanf("%d",&time);
if (5<time && time<12){
printf("Good morning");
}
else if (12<=time && time<17){
printf("Good afternoon");
}
else if (17<=time && time<21){
printf("Good evening");
}
else if (21<=time && time<24){
printf("Good night");
if (0<=time && time<5)
printf("Good night");
}
return 0;
}
OUTPUT:
enter the time: 21
Good night
3. Write a C program to convert distance from km to meters, feet, inches, and
centimeters
PROGRAM:
#include <stdio.h>
int meter(int km);
int feet(int km);
int inches(int km);
int centi(int km);
int main() {
int km, c, m, f, i, s;
printf("Enter the distance in km: ");
scanf("%d", &km);
printf("Enter the choice:\n");
printf("1. Convert into meters\n");
printf("2. Convert into feet\n");
printf("3. Convert into inches\n");
printf("4. Convert into centimeters\n");
scanf("%d", &c);
switch (c) {
case 1:
m = meter(km);
printf("%d meters\n", m);
break;
case 2:
f = feet(km);
printf("%d feet\n", f);
break;
case 3:
i = inches(km);
printf("%d inches\n", i);
break;
case 4:
s = centi(km);
printf("%d centimeters\n", s);
break;
default:
printf("Enter the correct choice: \n");
break;
}
printf("End\n");
return 0;
}
int meter(int km) {
return km * 1000;
}
int feet(int km) {
return km * 3280;
}
int inches(int km) {
return km * 39370;
}
int centi(int km) {
return km * 100000;
}
OUTPUT:
Enter the distance in km: 2
Enter the choice:
1. Convert into meters
2. Convert into feet
3. Convert into inches
4. Convert into centimeters
2
6560 feet
End
4. Write a C program to print the given pattern when the row size is given. The
following example shows the pattern when the row size is 5.
Input: 5
Output:
ABCDE
ABCD
ABC
AB
A
PROGRAM:
#include <stdio.h>
void pattern(int row);
int main() {
int rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
pattern(rows);
return 0;
}
void pattern(int row) {
int i, j;
char l;
for (i = 1; i <= row; i++) {
l = 'A';
for (j = 1; j <= row - i + 1; j++) {
printf("%c ", l);
l++;
}
printf("\n");
}
}
OUTPUT;
Enter the number of rows: 6
ABCDEF
ABCDE
ABCD
ABC
AB
A
5. Write a complete C program that computes C(n, r), the number of subsets of
a set of size n which contains r elements. The formula is: C(n, r) = n!/(r!(n-r)!).
Make sure you write a factorial function to compute the value
PROGRAM:
#include <stdio.h>
int factorial(int num) {
int result = 1;
int i;
for (i = 1; i <= num; i++) {
result *= i;
}
return result;
}
int combination(int n, int r) {
return factorial(n) / (factorial(r) * factorial(n - r));
}
int main() {
int n, r;
printf("Enter the value of n: ");
scanf("%d", &n);
printf("Enter the value of r: ");
scanf("%d", &r);
printf("C(%d, %d) = %lld\n", n, r, combination(n, r));
return 0;
}
OUTPUT:
Enter the value of n: 5
Enter the value of r: 5
C(5, 5) = 1