Day 4 of Programming in C
Take 5 digit integer from the keyboard. Reverse the integer and check whether they are same or not.
#include <stdio.h>
void main(){
int n,num,rev = 0;
printf("Enter a 5 digit number : ");
scanf("%d",&n);
num = n;
rev = (rev*10)+(n%10);
n = n/10;
rev = (rev*10)+(n%10);
n = n/10;
rev = (rev*10)+(n%10);
n = n/10;
rev = (rev*10)+(n%10);
n = n/10;
rev = (rev*10)+(n%10);
n = n/10;
if(num == rev){
printf("The original number is equal to the reversed number");
}
else{
printf("The original number is not equal to the reversed number");
}
}
Step 1: Declare variables `n` for input, `num` to store original number, and `rev` for reversed number.
Step 2: Take user input for 5-digit number.
Step 3: Store original number in `num`.
Step 4: Create reversed number using modulo and division operations.
Step 5: Compare original number with reversed number to check if they're equal.
Step 2: Take user input for 5-digit number.
Step 3: Store original number in `num`.
Step 4: Create reversed number using modulo and division operations.
Step 5: Compare original number with reversed number to check if they're equal.
Find the absolute of a number.
#include <stdio.h>
void main(){
int num;
printf("Enter a number : ");
scanf("%d",&num);
if(num>=0){
printf("The absolute value of %d is %d",num,num);
}
else{
printf("The absolute value of %d is %d",num,-num);
}
}
Step 1: Declare integer variable `num`.
Step 2: Take user input for the number.
Step 3: Check if number is positive (≥0):
• If positive, print the same number
• If negative, multiply by -1 and print
Step 4: Display the absolute value with original number.
Step 2: Take user input for the number.
Step 3: Check if number is positive (≥0):
• If positive, print the same number
• If negative, multiply by -1 and print
Step 4: Display the absolute value with original number.
Consider the 3 coordinates (x1,y1),(x2,y2),(x3,y3).Check these will form a triangle or not.
#include <stdio.h>
void main(){
int x1,x2,x3,y1,y2,y3,area;
printf("Enter x1 and y1 : ");
scanf("%d%d",&x1,&y1);
printf("Enter x2 and y2 : ");
scanf("%d%d",&x2,&y2);
printf("Enter x3 and y3 : ");
scanf("%d%d",&x3,&y3);
area = (x1*(y2-y3))+(x2*(y3-y1))+(x3*(y1-y2));
if(area == 0){
printf("Given three points will not form a triangle");
}
else{
printf("Given three points will form a triangle");
}
}
Step 1: Declare variables for coordinates (x1,y1), (x2,y2), (x3,y3).
Step 2: Take user input for all coordinates.
Step 3: Calculate area using formula: x1(y2-y3) + x2(y3-y1) + x3(y1-y2).
Step 4: Check if area equals 0:
• If area = 0, points are collinear (no triangle)
• If area ≠ 0, points form a triangle
Step 2: Take user input for all coordinates.
Step 3: Calculate area using formula: x1(y2-y3) + x2(y3-y1) + x3(y1-y2).
Step 4: Check if area equals 0:
• If area = 0, points are collinear (no triangle)
• If area ≠ 0, points form a triangle
Given coordinate (x,y), which is the centre of a circle and r be the radius.Check whether a given point is inside, outside or on the circle.
#include <stdio.h>
void main(){
int x,y,r,x1,y1,u;
printf("Enter the coordinates of centre of circle : ");
scanf("%d%d",&x,&y);
printf("Enter the radius of circle : ");
scanf("%d",&r);
printf("Enter the coordinates of the point : ");
scanf("%d%d",&x1,&y1);
u = (x-x1)*(x-x1) + (y-y1)*(y-y1) - r*r;
if(u==0){
printf("The point lies on the circle");
}
else if(u < 0){
printf("The point lies inside the circle");
}
else{
printf("The point lies outside the circle");
}
}
Step 1: Declare variables for circle center (x,y), radius (r), and point coordinates (x1,y1).
Step 2: Take user input for circle parameters and point coordinates.
Step 3: Calculate equation: (x-x1)² + (y-y1)² - r²
Step 4: Check result (u):
• If u = 0: point is on circle
• If u < 0: point is inside circle
• If u > 0: point is outside circle
Step 2: Take user input for circle parameters and point coordinates.
Step 3: Calculate equation: (x-x1)² + (y-y1)² - r²
Step 4: Check result (u):
• If u = 0: point is on circle
• If u < 0: point is inside circle
• If u > 0: point is outside circle
The body mass index is defined as the ratio of the weight(kg) to the square of height(m).Write a program that receives weight and height and reports the BMI status of that person.
| BMI Status | Range |
|---|---|
| Starvation | <15 |
| Anorexic | 15.1 to 17.5 |
| Underweight | 17.6 to 18.5 |
| Ideal | 18.6 to 24.9 |
| Overweight | 25 to 25.9 |
| Extreme Obesity | 30 to 39.9 |
| Morbidly Obese | >=40 |
#include <stdio.h>
void main(){
float w,h,bmi;
int category;
printf("Enter your weight in kg and height in m : ");
scanf("%f%f",&w,&h);
bmi = w/(h*h);
category = (bmi>0 && bmi<=15)?0:
(bmi>15 && bmi<=17.5)?1:
(bmi>17.5 && bmi<=18.5)?2:
(bmi>18.5 && bmi<=24.9)?3:
(bmi>24.9 && bmi<=29.9)?4:
(bmi>29.9 && bmi<=39.9)?5:
(bmi>39.9)?6:-1;
switch (category)
{
case 0:
printf("BMI = %.1f and Status = Starvation",bmi);
break;
case 1:
printf("BMI = %.1f and Status = Anorexic",bmi);
break;
case 2:
printf("BMI = %.1f and Status = Underweight",bmi);
break;
case 3:
printf("BMI = %.1f and Status = Ideal",bmi);
break;
case 4:
printf("BMI = %.1f and Status = Overweight",bmi);
break;
case 5:
printf("BMI = %.1f and Status = Obese",bmi);
break;
case 6:
printf("BMI = %.1f and Status = Morbidly Obese",bmi);
break;
default:
printf("You have entered wrong values");
break;
}
}
Step 1: Declare variables for weight (w), height (h), BMI, and category.
Step 2: Take user input for weight and height.
Step 3: Calculate BMI using formula: weight/(height²).
Step 4: Use nested ternary operators to determine BMI category (0-6).
Step 5: Use switch statement to print appropriate BMI status based on category:
• Categories include: Starvation, Anorexic, Underweight, Ideal, Overweight, Obese, Morbidly Obese
Step 2: Take user input for weight and height.
Step 3: Calculate BMI using formula: weight/(height²).
Step 4: Use nested ternary operators to determine BMI category (0-6).
Step 5: Use switch statement to print appropriate BMI status based on category:
• Categories include: Starvation, Anorexic, Underweight, Ideal, Overweight, Obese, Morbidly Obese
Comments