Let us C Chapter 2

[C] Attempt the following:

 (a) If cost price and selling price of an item is input through the keyboard, write a program to determine whether the seller has made profit or incurred loss. Also determine how much profit he made or loss he incurred. 

#include<stdio.h>
int main(){
    int sp, cp;
    printf("Enter the cost price: ");
    scanf("%d", &cp);
    printf("Enter the selling price:");
    scanf("%d", &sp);
    if (sp>cp)
    {
        printf("Seller is inProfit = %d", sp-cp);
    }
    else{ printf("Seller  is in Loss = %d", cp-sp);}
}
 

(b) Any integer is input through the keyboard. Write a program to find out whether it is an odd number or even number.

include<stdio.h>
int main(){
    int a;
    printf("Enter the intger: ");
    scanf("%d", &a);
    if (a%2==0)
    {
        printf("%d is an even integer.", a);
    }
    else{ printf("%d is an odd integer.", a);}
}

(c) Any year is input through the keyboard. Write a program to determine whether the year is a leap year or not. (Hint: Use the % (modulus) operator).

#include <stdio.h>
int main(){
    int y;
    printf("Enter the Year: ");
    scanf("%d", &y);
    if (y%4==0)
    {
        printf("%d ia a Leap Year.", y);
        }
    else{printf("%d ia not a Leap Year.", y);}

    return 0;}
 
   
   


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

#include <stdio.h>
int main(){
    printf("Find the day of 1st january in given year.\n");
    int y,day,d;
    printf("Enter the year ");
    scanf("%d", &y);
    day=365*y+(y-1)/4-(y-1)/100+(y-1)/400;
    d=day%7;//to find week
    if(d==1)
      {printf("Moday");}
    else if(d==2)
      {printf("Tuesday");}
    else if(d==3)
      {printf("Wednesday");}
    else if(d==4)
      {printf("Thursday");}
    else if(d==5)
      {printf("Friday");}
    else if(d==6)
      {printf("Saturday");}
    else if(d==0)
      {printf("Sunday");}
    else {printf("wrong statement.");}
    return 0;
    }

(e) 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.

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

(g) Write a program to check whether a triangle is valid or not, when the three angles of the triangle are entered through the keyboard. 

(h) A triangle is valid if the sum of all the three angles is equal to 180 degrees. Find the absolute value of a number entered through the keyboard.

(i) Given the length and breadth of a rectangle, write a program to find whether the area of the rectangle is greater than its perimeter. For example, the area of the rectangle with length = 5 and breadth = 4 is greater than its perimeter. 

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

(k) Given the coordinates (x, y) of a center of a circle and it’s radius, write a program which will determine whether a point lies inside the circle, on the circle or outside the circle. (Hint: Use sqrt( ) and pow( ) functions).

(l)Given a point (x, y), write a program to find out if it lies on the x-axis, y-axis or at the origin, viz. (0, 0). 

J.(a) Using conditional operators determine: 

(1) Whether the character entered through the keyboard is a lower case alphabet or not. 

#include <stdio.h>
int main(){
    char ch;
    printf("Enter the letter: ");
    scanf("%c", &ch);
    if (ch<='a' && ch>='z')
    { printf("%c is a lowercase letter", ch);}
    else {printf("%c is a uppercase letter", ch);}  
    return 0;}


(2) Whether a character entered through the keyboard is a special symbol or not. 

#include <stdio.h>
int main(){
    char ch;
    printf("Enter the character: ");
    scanf("%c", &ch);
    if(ch<='A' && ch>='Z' && ch<='a' && ch>='z'
      && ch<='0' && ch>='9')
    {printf("%c ia a not a special symbolic character", ch);}  
    else {printf("%c ia a special symbolic character", ch);}
    return 0;}


(b)Write a program using conditional operators to determine whether a year entered through the keyboard is a leap year or not. 

#include <stdio.h>
int main(){
    int y;
    printf("Enter the Year: ");
    scanf("%d", &y);
    if (y%4==0)
    {
        printf("%d ia a Leap Year.", y);
        }
    else{printf("%d ia not a Leap Year.", y);}

    return 0;}

(c) Write a program to find the greatest of the three numbers entered through the keyboard using conditional operators.

#include <stdio.h>

int main(){
    int a,b,c,d;
    printf("enter first number");
    scanf("%d", &a);
    printf("enter second number");
    scanf("%d", &b);
    printf("enter third number");
    scanf("%d", &c);
    if (a<b)
    { if (b<c)
    {printf("%d is a greatest number.", c;)}
else
{printf("%d is a greatest number.", b);}
    else{printf("%d is a greatest number."
, a);}
return 0;
}


Comments

Popular posts from this blog

Let us C Chapter 1.(H)

Functions and recrusion.

Quick calculator using HTML,CSS and Java Script.