Chapter 3 - Let Us C

A five-digit number is entered through the keyboard. Write a program to obtain the reversed number and to determine whether the original and reversed numbers are equal or not.


#include <stdio.h>
int 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");
    }

    return 0;
}
    
    
Step 1: Declare integer variables `n`, `num`, and `rev`. `rev` is initialized to 0.
Step 2: Prompt the user to enter a 5-digit number and store it in `n`.
Step 3: Store the original number `n` in `num` for later comparison.
Step 4: Reverse the number digit by digit:
• Extract the last digit of `n` using the modulo operator (`% 10`).
• Add the extracted digit to `rev` after multiplying `rev` by 10 (this shifts the existing digits to the left).
• Remove the last digit from `n` by integer division by 10.
Step 5: Repeat Step 4 five times (since it's a 5-digit number).
Step 6: Compare the original number `num` with the reversed number `rev`.
Step 7: Print whether the original and reversed numbers are equal or not.

If ages of Ram, Shyam and Ajay are input through the keyboard, write a program to determine the youngest of the three.


#include <stdio.h>
int main()
{
    int ram, shyam, ajay;
    printf("Enter the age of Ram : ");
    scanf("%d", &ram);
    printf("Enter the age of Shyam : ");
    scanf("%d", ­am);
    printf("Enter the age of Ajay : ");
    scanf("%d", &ajay);

    if (ram < shyam)
    {
        if (ram < ajay)
        {
            printf("Ram is the youngest");
        }
        else
        {
            printf("Ajay is the youngest");
        }
    }
    else
    {
        if (ajay > shyam)
        {
            printf("Shyam is the youngest");
        }
        else
        {
            printf("Ajay is the youngest");
        }
    }
    return 0;
}

        
    
    
Step 1: Declare integer variables `ram`, `shyam`, and `ajay` to store the ages.
Step 2: Prompt the user to enter Ram's age and store it in the `ram` variable.
Step 3: Prompt the user to enter Shyam's age and store it in the `shyam` variable.
Step 4: Prompt the user to enter Ajay's age and store it in the `ajay` variable.
Step 5: Use nested `if` statements to determine the youngest:
• Check if Ram's age is less than Shyam's age.
• If true, check if Ram's age is also less than Ajay's age.
• If true, Ram is the youngest.
• Otherwise, Ajay is the youngest.
• Otherwise (Ram is not younger than Shyam), check if Ajay's age is greater than Shyam's age.
• If true, Shyam is the youngest.
• Otherwise, Ajay is the youngest.
Step 6: Print the name of the youngest person.

Write a program to check whether a triangle is valid or not, if three angles of the triangle are entered through the keyboard. A triangle is valid if the sum of all the three angles is equal to 180 degrees.


 #include <stdio.h>
 int main(){
    
    float a,b,c;
    printf("Enter the angles of the triangle : ");
    scanf("%f%f%f",&a,&b,&c);

    if(a+b+c == 180){
        printf("The triangle is valid");
    }
    else{
        printf("The triangle is not valid");
    }

    return 0;
}
     
    
    

Step 1: Declare three floating-point variables `a`, `b`, and `c` to store the angles of the triangle.
Step 2: Prompt the user to enter the three angles of the triangle and store them in `a`, `b`, and `c` respectively using `scanf`.
Step 3: Check if the sum of the three angles (`a + b + c`) is equal to 180.
Step 4: If the sum is equal to 180, print "The triangle is valid".
Step 5: Otherwise (if the sum is not equal to 180), print "The triangle is not valid".
Step 6: The program returns 0, indicating successful execution.

Write a program to find the absolute value of a number entered through the keyboard.


#include <stdio.h>
int 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);
    }

    return 0;
} 

        
    
Step 1: Declare an integer variable `num` to store the number entered by the user.
Step 2: Prompt the user to enter a number and store it in the `num` variable using `scanf`.
Step 3: Check if the number `num` is greater than or equal to 0.
Step 4: If `num` is greater than or equal to 0, print the absolute value as the number itself (since it's already non-negative).
Step 5: Otherwise (if `num` is less than 0), print the absolute value by negating the number (multiplying it by -1).
Step 6: The program returns 0, indicating successful execution.

Given the length and breadth of a rectangle, write a program to find whether the area of the rectangle is greater than its perimeter.


#include <stdio.h>
int main(){
    float a,b;
    printf("Enter the length and breadth of the rectangle : ");
    scanf("%f%f",&a,&b);

    if(2*(a+b) < a*b){
        printf("The area of rectangle is greater than its perimeter");
    }
    else{
        printf("The area of rectangle is not greater than its perimeter");
    }
    return 0;
}


Step 1: Declare two floating-point variables `a` and `b` to store the length and breadth of the rectangle.
Step 2: Prompt the user to enter the length and breadth of the rectangle and store them in `a` and `b` respectively using `scanf`.
Step 3: Calculate the perimeter of the rectangle as `2 * (a + b)`.
Step 4: Calculate the area of the rectangle as `a * b`.
Step 5: Check if the area of the rectangle is greater than its perimeter using the condition `2 * (a + b) < a * b`.
Step 6: If the condition in Step 5 is true, print "The area of rectangle is greater than its perimeter".
Step 7: Otherwise (if the condition is false), print "The area of rectangle is not greater than its perimeter".
Step 8: The program returns 0, indicating successful execution.

Given three points (x1, y1), (x2, y2) and (x3, y3), write a program to check if the three points fall on one straight line.


#include <stdio.h>
int 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 fall on one straight line");
    }
    else{
        printf("Given three points does not fall on one straight line");
    }
    return 0;
}


        
    
Step 1: Declare integer variables `x1`, `x2`, `x3`, `y1`, `y2`, `y3`, and `area`.
Step 2: Prompt the user to enter the coordinates `x1` and `y1` and store them in the respective variables using `scanf`.
Step 3: Prompt the user to enter the coordinates `x2` and `y2` and store them in the respective variables using `scanf`.
Step 4: Prompt the user to enter the coordinates `x3` and `y3` and store them in the respective variables using `scanf`.
Step 5: Calculate the area of the triangle formed by the three points using the formula: `area = (x1*(y2-y3))+(x2*(y3-y1))+(x3*(y1-y2))`. This formula effectively checks if the points are collinear; if the area is 0, the points are on the same line.
Step 6: Check if the calculated `area` is equal to 0.
Step 7: If `area` is 0, print "Given three points fall on one straight line".
Step 8: Otherwise (if `area` is not 0), print "Given three points does not fall on one straight line".
Step 9: The program returns 0, indicating successful execution.

Given the coordinates (x, y) of center of a circle and its radius, write a program that will determine whether a point lies inside the circle, on the circle or outside the circle.


#include <stdio.h>
int 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");
    }
    return 0;
}


        
    
Step 1: Declare integer variables `x`, `y`, `r`, `x1`, `y1`, and `u`.
Step 2: Prompt the user to enter the coordinates of the center of the circle (`x`, `y`) and store them using `scanf`.
Step 3: Prompt the user to enter the radius of the circle (`r`) and store it using `scanf`.
Step 4: Prompt the user to enter the coordinates of the point (`x1`, `y1`) and store them using `scanf`.
Step 5: Calculate the value of `u` using the formula: `u = (x-x1)*(x-x1) + (y-y1)*(y-y1) - r*r`. This formula represents the difference between the square of the distance between the point and the center of the circle, and the square of the radius.
Step 6: Check if `u` is equal to 0.
Step 7: If `u` is 0, print "The point lies on the circle".
Step 8: Otherwise, check if `u` is less than 0.
Step 9: If `u` is less than 0, print "The point lies inside the circle".
Step 10: Otherwise (if `u` is greater than 0), print "The point lies outside the circle".
Step 11: The program returns 0, indicating successful execution.

Given a point (x, y), write a program to find out if it lies on X-axis, Yaxis or origin.


#include <stdio.h>
int main(){
    int x, y;
    printf("Enter the point : ");
    scanf("%d%d", &x, &y);

    if (x == 0)
    {
        if (y == 0)
        {
            printf("The point lies on the origin");
        }
        else
        {
            printf("The point lies on the Y-axis");
        }
    }
    else
    {
        if (y == 0)
        {
            printf("The point lies on the X-axis");
        }
        else
        {
            printf("The point does not lie on the origin,X-axis or Y-axis");
        }
    }

    return 0;
}


        
    
Step 1: Declare two integer variables `x` and `y` to store the coordinates of the point.
Step 2: Prompt the user to enter the coordinates of the point and store them in `x` and `y` using `scanf`.
Step 3: Check if `x` is equal to 0.
Step 4: If `x` is 0, check if `y` is also equal to 0.
Step 5: If both `x` and `y` are 0, print "The point lies on the origin".
Step 6: Otherwise (if `x` is 0 but `y` is not), print "The point lies on the Y-axis".
Step 7: Otherwise (if `x` is not 0), check if `y` is equal to 0.
Step 8: If `y` is 0, print "The point lies on the X-axis".
Step 9: Otherwise (if both `x` and `y` are not 0), print "The point does not lie on the origin,X-axis or Y-axis".
Step 10: The program returns 0, indicating successful execution.

According to Gregorian calendar, it was Monday on the date 01/01/01. If any year is input through the keyboard write a program to find out what is the day on 1st January of this year after this date.


#include <stdio.h>
int main(){
        int year, days, day;
    printf("\nEnter a year after 2001 : ");
    scanf("%d", &year);
    if (year >= 2001)
    {
        year = (year - 2001);
        days = (year * 365) + (year / 4) - (year/100) + (year/400);
        day = days % 7;
    }
    else
    {
        printf("Please enter a year given between the range");
    }
    if (day == 0)
        printf("Monday\n");
    else if (day == 1)
        printf("Tuesday\n");
    else if (day == 2)
        printf("Wednesday\n");
    else if (day == 3)
        printf("Thursday\n");
    else if (day == 4)
        printf("Friday\n");
    else if (day == 5)
        printf("Saturday\n");
    else
        printf("Sunday\n");
    return 0;
}


        
    
Step 1: Declare integer variables `year`, `days`, and `day`.
Step 2: Prompt the user to enter a year after 2001 and store it in the `year` variable using `scanf`.
Step 3: Check if the entered `year` is greater than or equal to 2001.
Step 4: If the year is within the valid range (>=2001):
• Calculate the number of years since 2001 by subtracting 2001 from the entered `year`.
• Calculate the total number of days since January 1, 2001, considering leap years. The formula used is `days = (year * 365) + (year / 4) - (year/100) + (year/400);` This accounts for the extra day in leap years (divisible by 4), except for century years (divisible by 100) unless they are also divisible by 400.
• Calculate the day of the week (0 for Monday, 1 for Tuesday, ..., 6 for Sunday) by taking the remainder of the total days divided by 7: `day = days % 7;`.
Step 5: If the year is not within the valid range, print "Please enter a year given between the range".
Step 6: Use a series of `if-else if` statements to determine the day of the week based on the value of `day`.
Step 7: Print the corresponding day of the week (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, or Sunday) based on the calculated `day` value.
Step 8: The program returns 0, indicating successful execution.

Comments

Popular Posts