Let Us C / Chapter 2 (The Decision Control Structure)

                                  Exercise [C]


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

Solution:


#include<stdio.h>
#include<conio.h>
main() {

int cp,sp,rslt;
clrscr();

printf("Please enter the cost price of the item: \n");
scanf("%d",&cp);

printf("Please enter the selling price of the item: \n");
scanf("%d",&sp);

if(cp>sp) {
rslt=cp-sp;
printf("\nSeller has incurred LOSS of %d rupees.\n",rslt);
}

if(cp<sp)
{
rslt=sp-cp;
printf("\nSeller has made PROFIT of %d rupees.\n",rslt);
}

getch();
return 0;

}

------------------------------------------------------------------------------------------------------------

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

Solution:


#include<stdio.h>
#include<conio.h>

main() {

int i;
clrscr();

printf("Please enter the number: \n");
scanf("%d",&i);

if(i%2==0)
printf("\nThe number is EVEN.\n");

else
printf("\nThe number is ODD.\n");

getch();
return 0;

}

------------------------------------------------------------------------------------------------------------

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

Solution:


#include<stdio.h>
#include<conio.h>

main() {

int yr;
clrscr();

printf("Please enter the year: \n");
scanf("%d",&yr);

if(yr%4==0)
printf("\nThe year is a LEAP YEAR.\n");

else
printf("\nThe Year is NOT A LEAP YEAR.\n");

getch();
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.

Solution:


#include<stdio.h>
#include<conio.h>

main() {

int month=1,year,a,day=1;

clrscr();

printf("Enter year: ");
scanf("%d",&year);

/* this is a general purpose formula to calculate first day */

a=(14-month)/12;
year=year-a;
month=month+12*a-2;

day=(day+year+(year/4)-(year/100)+(year/400)+((31*month)/12)) % 7;


/* determining the position to print the first day in the calender */

printf("\n\nFirst day is ");

if(day==0)
printf("Sunday");

else if(day==1)
printf("Monday");

else if(day==2)
printf("Tuesday");

else if(day==3)
printf("Wednesday");

else if(day==4)
printf("Thursday");

else if(day==5)
printf("Friday");

else if(day==6)
printf("Saturday");


getch();
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.

Solution:


#include<stdio.h>
#include<conio.h>

main() {

long i,d1,d2,d3,d4,d5,a,b,c,d,n_num;
clrscr();

printf("Please enter a five digit number: \n");
scanf("%ld",&i);


d1=i/10000;
a=i%10000;

d2=a/1000;
b=a%1000;

d3=b/100;
c=b%100;

d4=c/10;
d=c%10;

d5=d/1;

n_num=(d5*10000)+(d4*1000)+(d3*100)+(d2*10)+(d1*1);

printf("\nThe REVERSED NUMBER is %ld \n",n_num);

if(n_num==i){
printf("\nboth numbers are SAME.\n");
}
else {
printf("\nboth number are NOT SAME.\n");
}


getch();
return 0;

}

------------------------------------------------------------------------------------------------------------

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

Solution:


#include<stdio.h>
#include<conio.h>

main() {

int ram,shyam,ajay;
clrscr();

printf("Please enter the age of RAM: ");
scanf("%d",&ram);

printf("\nPlease enter the age of SHYAM: ");
scanf("%d",&shyam);

printf("\nPlease enter the age or AJAY: ");
scanf("%d",&ajay);


if(ram<shyam)
{
if(ram<ajay) {
printf("\nRam is the YOUNGEST.\n");
}
 }
 if(shyam<ram)
{
if(shyam<ajay) {
printf("\nShyam is the YOUNGEST.\n");
}
 }

if(ajay<ram) {
if(ajay<shyam) {
printf("\nAjay is the YOUNGEST.\n");
}
 }


getch();
return 0;

}

------------------------------------------------------------------------------------------------------------

(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. A triangle is valid if the sum of all the three angles is equal to 180 degrees.

Solution:


#include<stdio.h>
#include<conio.h>
main() {

int a1,a2,a3;
clrscr();

printf("Please enter the first angle: \n");
scanf("%d",&a1);

printf("\nPlease enter the second angle: \n");
scanf("%d",&a2);

printf("\nPlease enter the third angle: \n");
scanf("%d",&a3);

if(a1+a2+a3==180) {
printf("\nThe triangle is VALID.\n");
}

else {
printf("\nThe triangle is NOT VALID.\n");
}


getch();
return 0;

}

------------------------------------------------------------------------------------------------------------

(h) Find the absolute value of a number entered through the keyboard.

Solution:


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main() {

int i,av;
clrscr();

printf("Please enter any number:  ");
scanf("%d",&i);

av=abs(i);

 /* abs(); is a standard function to calculate absolute value.\
this function has been defined in <stdlib.h> library. */

printf("Absolute value = %d",av);


getch();
return 0;

}

------------------------------------------------------------------------------------------------------------

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

Solution:


#include<stdio.h>
#include<conio.h>
main() {

int l,b,area,peri;
clrscr();

printf("Please enter the length and bredth of a rectangle: \n");
scanf("%d%d",&l,&b);

area=l*b;
peri=2*(l+b);

if(area>peri) {
printf("\nThe Area of Rectangle is GREATER then it's perimeter.\n");
}

else {
printf("\nThe area of Rectangle is NOT GREATER then it's perimeter.\n");
}


getch();
return 0;

}

------------------------------------------------------------------------------------------------------------

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

Solution:


#include<stdio.h>
#include<conio.h>
main() {

int x1,y1,x2,y2,x3,y3,curve1,curve2;
clrscr();

printf("Please enter the values of Three points:  ");
printf("\n(x1,y1):\n");
scanf("%d%d",&x1,&y1);
printf("\n(x2,y2):\n");
scanf("%d%d",&x2,&y2);
printf("\n(x3,y3):\n");
scanf("%d%d",&x3,&y3);

curve1=(x2-x1)/(y2-y1);
curve2=(x3-x2)/(y3-y2);

if(curve1==curve2)
printf("\nAll three points fall on one straight line.\n");
else
printf("\nThese three points do not fall on one straight line.\n");


getch();
return 0;

}

------------------------------------------------------------------------------------------------------------

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

Solution:


#include<stdio.h>
#include<conio.h>
#include<math.h>
main() {

int x,y,x_,y_,r,point;
clrscr();

printf("Please enter the coordinates of center: \n");
printf("(x,y): ");
scanf("%d%d",&x,&y);
printf("\nPlease enter the radius of circle: \n");
scanf("%d",&r);
printf("\nPlease enter the point to check: \n");
printf("(x`,y`): ");
scanf("%d%d",&x_,&y_);


point=pow(x_,2)+pow(y_,2)+pow(x,2)+pow(y,2)-(2*x_*x)-(2*y_*y)-pow(r,2);

if(point<0)
printf("\nThe point lies inside the circle.\n");

if(point>0)
printf("\nThe point lies outside the circle.\n");

if(point==0)
printf("\nThe point lies on the circle.\n");

getch();
return 0;

}

------------------------------------------------------------------------------------------------------------

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

Solution:


#include<stdio.h>
#include<conio.h>
main() {

int x,y;
clrscr();

printf("Please enter points\n");
printf("(x,y): \n");
scanf("%d%d",&x,&y);

if(x==0&&y!=0) {
printf("\nThe point lies on Y AXIS.\n");
}

else if(x!=0&&y==0) {
printf("\nThe point lies on X AXIS.\n");
}

else if(x==0&&y==0) {
printf("\nThe point lies at the origin.\n");
}

else {
printf("\nThe point doesn't lie on any axis or at origin.\n");
}


getch();
return 0;

}

___________________________________________________________________

                       Exercise [F]



(a) Any year is entered through the keyboard, write a program to determine whether the year is leap or not. Use the logical operators && and ||.

Solution:


#include<stdio.h>
#include<conio.h>
main() {

int yr;
clrscr();

printf("Please enter the year: ");
scanf("%d",&yr);

if(yr%4==0 || !((yr%4)>0) )
printf("\nThe is a LEAP YEAR.\n");

else
printf("\nThis is NOT A LEAP YEAR.\n");



getch();
return 0;

}

------------------------------------------------------------------------------------------------------------

(b) Any character is entered through the keyboard, write a program to determine whether the character entered is a capital letter, a small case letter, a digit or a special symbol.
The following table shows the range of ASCII values for various characters.

The following table shows the range of ASCII values for various characters.
Characters

Characters            ASCII values

A – Z                       65 - 90
a – z                         97 - 122
0 – 9                        48 - 57
special symbols         0 - 47, 58 - 64, 91 - 96, 123 - 127


Solution:


#include<stdio.h>
#include<conio.h>
main() {

char ch;
clrscr();

printf("Please enter any character:");
scanf("%c",&ch);

if(ch>=65&&ch<=90)
printf("\nThe character you entered is a CAPITAL LETTER.\n");

if(ch>=97&&ch<=122)
printf("\nThe character you entered is a SMALL LETTER.\n");

if(ch>=48&&ch<=57)
printf("\nThe character you entered is a DIGIT.\n");

if(ch>=0&&ch<=47 || ch>=58&&ch<=64 || ch>=91&&ch<=96 || ch>=123&&ch<=127)
printf("\nThe character you entered is a SPECIAL SYMBOL.\n");


getch();

return 0;

}

------------------------------------------------------------------------------------------------------------

(c) An Insurance company follows following rules to calculate premium.
(1) If a person’s health is excellent and the person is between 25 and 35 years of age and lives in a city and is a male then the premium is Rs. 4 per thousand and his policy amount cannot exceed Rs. 2 lakhs.
(2) If a person satisfies all the above conditions except that the sex is female then the premium is Rs. 3 per thousand and her policy amount cannot exceed Rs. 1 lakh.
(3) If a person’s health is poor and the person is between 25 and 35 years of age and lives in a village and is a male then the premium is Rs. 6 per thousand and his policy cannot exceed Rs. 10,000.
(4) In all other cases the person is not insured.

Write a program to output whether the person should be insured or not, his/her premium rate and maximum amount for which he/she can be insured.


Solution:


#include<stdio.h>
#include<conio.h>
main() {

int age,premium,p_a,hlt,rsdnc,sex;
long amount;
clrscr();

printf("Please tell the HEALTH of the person\n(1 for excellent or 2 for poor) :\n");
scanf("%d",&hlt);

printf("Please tell the AGE of the person: \n");
scanf("%d",&age);

printf("Please tell the residence\n(1 for city or 2 for village) : \n");
scanf("%d",&rsdnc);

printf("Please tell you sex\n(1 for male or 2 for female) : \n");
scanf("%d",&sex);



if(hlt==1 && age>=25&&age<=35 && rsdnc==1 && sex==1) {
premium=4;
amount=200000;

printf("\nThis person is insured.\n");
printf("Premium rate = %d rupees per thousand.\n",premium);
printf("Person can be insured for maximum amount of %ld rupees.\n",amount);
}

else if(hlt==1||hlt==2 && age>=25&&age<=35 && rsdnc==1||rsdnc==2 && sex==2) {
premium=3;
amount=100000;

printf("\nThis person is insured.\n");
printf("Premium rate = %d rupees per thousand.\n",premium);
printf("Person can be insured for maximum amount of %ld rupees.\n",amount);
}

else if(hlt==2 && age>=25&&age<=35 && rsdnc==2 && sex==1) {
premium=6;
amount=10000;

printf("\nThis person is insured.\n");
printf("Premium rate is = %d rupees per thousand.\n",premium);
printf("Person can be insured for maximum amount of %ld rupees.\n",amount);
}

else {
printf("\nThis person cannot be insured.\n");
}

getch();
return 0;

}

------------------------------------------------------------------------------------------------------------

(d) A certain grade of steel is graded according to the following conditions:
(i) Hardness must be greater than 50
(ii) Carbon content must be less than 0.7
(iii) Tensile strength must be greater than 5600

The grades are as follows:

Grade is 10 if all three conditions are met
Grade is 9 if conditions (i) and (ii) are met
Grade is 8 if conditions (ii) and (iii) are met
Grade is 7 if conditions (i) and (iii) are met
Grade is 6 if only one condition is met
Grade is 5 if none of the conditions are met

Write a program, which will require the user to give values of hardness, carbon content and tensile strength of the steel under consideration and output the grade of the steel.

Solution:


#include<stdio.h>
#include<conio.h>
main() {

int hdn,tnsl,hardness=50,tensile_strength=5600;

float c,carbon_content=0.7;

clrscr();

printf("Please enter the Hardness: \n");
scanf("%d",&hdn);

printf("\nPlease enter the Carbon content: \n");
scanf("%f",&c);

printf("\nPlease enter the Tensile Strength: \n");
scanf("%d",&tnsl);



if(hdn>hardness && c<carbon_content && tnsl>tensile_strength)
printf("\nThe steel has been Graded 10.\n");

else if(hdn>hardness && c<carbon_content)
printf("\nThe steel has been Graded 9.\n");

else if(c<carbon_content && tnsl>tensile_strength)
printf("\nThe steel has been Graded 8.\n");

else if(hdn>hardness && tnsl>tensile_strength)
printf("\nThe steel has been Graded 7.\n");

else if(hdn>hardness || c<carbon_content || tnsl>tensile_strength)
printf("\nThe steel has been Graded 6.\n");

else
printf("\nThe steel has been Graded 5.\n");


getch();

return 0;

}

------------------------------------------------------------------------------------------------------------

(e) A library charges a fine for every book returned late. For first 5 days the fine is 50 paise, for 6-10 days fine is one rupee and above 10 days fine is 5 rupees. If you return the book after 30 days your membership will be cancelled. Write a program to accept the number of days the member is late to return the book and display the fine or the appropriate message.

Solution:


#include<stdio.h>
#include<conio.h>
main() {
int days;
clrscr();

printf("Please enter the days of being late in returning the book:\n");
scanf("%d",&days);


if(days<=5)
printf("\nYou have been fined for 50 paise.\n");

if(days>=6&&days<=10)
printf("\nYou have been fined for 1 rupee.\n");

if(days>10&&days<30)
printf("\nYou have been fined for 5 rupee.\n");

if(days>=30)
printf("\nYour membership has been cancelled.\n");

getch();
return 0;

}

------------------------------------------------------------------------------------------------------------

(f) If the three sides of a triangle are entered through the keyboard, write a program to check whether the triangle is valid or not. The triangle is valid if the sum of two sides is greater than the largest of the three sides.

Solution:


#include<stdio.h>
#include<conio.h>
main() {

int s1,s2,s3,ls,ss1,ss2;
clrscr();

printf("Please enter the three sides of a triangle: \n");
scanf("%d%d%d",&s1,&s2,&s3);


if(s1>s2&&s1>s3){
ls=s1;
ss1=s2;
ss2=s3;
}
if(s2>s1&&s2>s3) {
ls=s2;
ss1=s1;
ss2=s3;
}
if(s3>s1&&s3>s2) {
ls=s3;
ss1=s1;
ss2=s2;
}

if((ss1+ss2)>ls) {
printf("\nThe Triangle is VALID.\n");
}
else {
printf("The triangle is NOT VALID.\n");
}

getch();
return 0;

}

------------------------------------------------------------------------------------------------------------

(g) If the three sides of a triangle are entered through the keyboard, write a program to check whether the triangle is isosceles, equilateral, scalene or right angled triangle.

Solution:


#include<stdio.h>
#include<conio.h>

main() {

int s1,s2,s3,largest,s_1,s_2;
clrscr();

printf("Please enter the three sides of a triangle: \n");
scanf("%d %d %d",&s1,&s2,&s3);


if(s1==s2&&s1!=s3||s1==s3&&s1!=s2 || s2==s3&&s2!=s1||s2==s1&&s2!=s3 ||s3==s1&&s3!=s2||s3==s2&&s3!=s1){
printf("The Triangle is ISOSCELES.\n");
}

if(s1==s2&&s2==s3 || s2==s1&&s2==s3 || s3==s1&&s3==s2){
printf("The Triangle is EQUILATERAL.\n");
}

if(s1!=s2&&s2!=s3 || s2!=s1&&s1!=s3 || s3!=s1&&s1!=s2){
printf("The Triangle is SCALENE.\n");
}

if(s1>s2&&s1>s3){
largest=s1;
s_1=s2;
s2=s3;
}

if(s2>s1&&s2>s3){
largest=s2;
s_1=s1;
s_2=s3;
}

if(s3>s1&&s3>s2){
largest=s3;
s_1=s1;
s_2=s2;
}

if(pow(largest,2)==pow(s_1,2)+pow(s_2,2))
printf("The Triangle is RIGHT ANGLED.\n");

getch();
return 0;
}

------------------------------------------------------------------------------------------------------------

(h) In a company, worker efficiency is determined on the basis of the time required for a worker to complete a particular job. If the time taken by the worker is between 2 – 3 hours, then the worker is said to be highly efficient. If the time required by the worker is between 3 – 4 hours, then the worker is ordered to improve speed. If the time taken is between 4 – 5 hours, the worker is given training to improve his speed, and if the time taken by the worker is more than 5 hours, then the worker has to leave the company. If the time taken by the worker is input through the keyboard, find the efficiency of the worker.

Solution:


#include<stdio.h>
#include<conio.h>
main() {

int hrs;
clrscr();

printf("Please enter the hours of completing job: ");
scanf("%d",&hrs);

if(hrs>=2 && hrs<=3)
printf("\nThe worker is HIGHLY EFFICIENT.\n");

if(hrs>=3 && hrs<=4)
printf("\nThe worker is ordered to IMPROVE SPEED.\n");

if(hrs>=4 && hrs<= 5)
printf("\nThe worker should be given TRAINING TO IMPROVE SPEED.\n");

if(hrs>5)
printf("\nThe worker should be asked to leave the company.\n");

else
printf("The worker is HIGHLY EFFICIENT.\n");


getch();
return 0;

}

------------------------------------------------------------------------------------------------------------

(i) A university has the following rules for a student to qualify for a degree with A as the main subject and B as the subsidiary subject:
 (a) He should get 55 percent or more in A and 45 percent or more in B.
 (b) If he gets than 55 percent in A he should get 55 percent or more in B. However, he should get at least 45 percent in A.
 (c) If he gets less than 45 percent in B and 65 percent or more in A he is allowed to reappear in an examination in B to qualify.
 (d) In all other cases he is declared to have failed.

Write a program to receive marks in A and B and Output whether the student has passed, failed or is allowed to reappear in B.

Solution:


#include<stdio.h>
#include<conio.h>
main() {

int a,b;
clrscr();

printf("Please enter the marks in subject A: \n");
scanf("%d",&a);

printf("\nPlease enter the marks in subject B: \n");
scanf("%d",&b);



if(a>=55 && b>=45) {
printf("Student is qualified for the degree.\n");
 }

else if(a==55 && b>=55 || a==55 && b>=45)  {
printf("Student is qualified for the degree.\n");
 }

else if(a>=65 && b<45) {
printf("Student is allowed to reappear in the exam of B to qualify.\n");
 }

else {
printf("Student has failed.\n");
 }



getch();
return 0;

}

------------------------------------------------------------------------------------------------------------

(j) The policy followed by a company to process customer orders is given by the following rules:

(a) If a customer order is less than or equal to that in stock and has credit is OK, supply has requirement.
(b) If has credit is not OK do not supply. Send him intimation.
(c) If has credit is Ok but the item in stock is less than has order, supply what is in stock. Intimate to him data the balance will be shipped.

Write a C program to implement the company policy.

Solution:


#include<stdio.h>
#include<conio.h>
main() {

int stock,credit,order;
clrscr();

printf("Please enter the stock available: ");
scanf("%d",&stock);
printf("\nPlease enter the order: ");
scanf("%d",&order);
printf("\nPlease enter the credit: ");
scanf("%d",&credit);

if(credit!=0&&order<=stock) {
printf("\nSupply\n");
}

else if(credit!=0&&order>stock) {
printf("\nAvailable items will be supplied.\n");
}

else {
printf("\nNo supply.\n");
}

getch();
return 0;

}
______________________________________________________________________
                   

                              Exercise [J]


(a) Using conditional operators determine:
   
(1) Whether the character entered through the keyboard is a lower case alphabet or not.
(2) Whether a character entered through the keyboard is a special symbol or not.

Solution:


#include<stdio.h>
#include<conio.h>

char main() {

char ch;
clrscr();


printf("Please enter a character: \n");
scanf("%c",&ch);



if(ch>=97 && ch<=122) {
printf("\n[A] This character is a SMALL CASE alphabet.\n");
}

else {
printf("\n[A] This character is NOT A SMALL CASE alphabet.\n");
}


if(ch>=0&&ch<=47||ch>=58&&ch<=64||ch>=91&&ch<=96||ch>=123&&ch<=127){
printf("\n[B] This character is a SPECIAL SYMBOL.\n");
}

else{
printf("\n[B] This character is NOT A SPECIAL SYMBOL.\n");
}



getch();
return 0;

}

------------------------------------------------------------------------------------------------------------

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

Solution:


#include<stdio.h>
#include<conio.h>
main () {

int yr;
clrscr();

printf("Please enter the year: \n");
scanf("%d",&yr);


if(yr%4!=0)
printf("\nThis is NOT A LEAP YEAR.\n");

else
printf("\nThis is a LEAP YEAR.\n");


getch();
return 0;
}

------------------------------------------------------------------------------------------------------------

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

Solution:


#include<stdio.h>
#include<conio.h>

main() {

int n1,n2,n3;
clrscr();

printf("\nPlease enter 3 numbers: \n");
scanf("%d %d %d",&n1,&n2,&n3);


if(n1>n2 && n1>n3) {
printf("\n%d is the greatest number of the three numbers.\n",n1);
}

else if(n2>n1 && n2>n3) {
printf("\n%d is the greatest number of the three numbers.\n",n2);
}

else if(n3>n2 && n3>n1) {
printf("\n%d is the greatest number of the three numbers.\n",n3);
}

else {
printf("\nwrong input!\n");
}


getch();
return 0;

}

_______________________________________________________________________

15 comments:

  1. what about the other exercises?? lyk find the output and point out errors? can u tell me where to find solutions to those problems?

    ReplyDelete
    Replies
    1. you can do them yourself, code is already written in the book, you just have to either retype it on compiler or read it as usual and find the error, I solved only the part which was confusing.

      Delete
  2. thanks alot bro.. they saved alot of time.. plus the codes are concise and easy.

    ReplyDelete
  3. in(d),,,unable to get output as grade 5;my program is=
    int main()
    {
    int hd,ts;//hd=hardness,cc=carbon content,ts=tensile strength.
    float cc;
    cout<<"enter hardness,carbon contnt,and tesile strength of steel";
    cin>>hd>>cc>>ts;
    if(hd>50&&cc<0.7&&ts>5600)
    cout<<"grade 10 hai";
    else if(hd>50&&cc<0.7)
    cout<<"grade 9";
    else if(cc<0.7&&ts>5600)
    cout<<"grade 8";
    else if(hd>50&&ts>5600)
    cout<<"grade 7";
    else if((hd>50)||(cc<07)||(ts>5600))
    cout<<"grade 6";
    else if(!(hd>50)&&!(cc<0.7)&&!(ts>5600))
    cout<<"grade 5";
    return 0;

    ReplyDelete
    Replies
    1. It's because you have used unnecessary conditional statement. You can simply put that last statement in a simple "else" block. Because if none of the conditions are true, it means they are obviously not any of those, defined above.

      Delete
  4. #include
    void main()
    {
    int num,rev,a1,a2,a3,a4,a5,b1,b2,b3,b4,b5;
    printf("Enter the five-digit number\n");
    scanf("%d",&num);
    a1=num%10;
    b1=a1*10000;
    num=num/10;
    a2=num%10;
    b2=a2*1000;
    num=num/10;
    a3=num%10;
    b3=a3*100;
    num=num/10;
    a4=num%10;
    b4=a4*10;
    num=num/10;
    a5=num%10;
    b5=a5*1;
    rev=b1+b2+b3+b4+b5;
    printf("The reversed number is:%d\n",rev);
    if(rev==num)
    {
    printf("Same\n");
    }
    else
    {
    printf("Not Same\n");
    }
    }
    not getting the desired result...if the number entered is same its showing 'not same' and if the number entered is different than its reversed order its showing 'same'.Could any1 please verify this code and post a solution to this problem.Thanks in advance :)

    ReplyDelete
  5. #include
    void main()
    {
    int num,rev,a1,a2,a3,a4,a5,b1,b2,b3,b4,b5;
    printf("Enter the five-digit number\n");
    scanf("%d",&num);
    a1=num%10;
    b1=a1*10000;
    num=num/10;
    a2=num%10;
    b2=a2*1000;
    num=num/10;
    a3=num%10;
    b3=a3*100;
    num=num/10;
    a4=num%10;
    b4=a4*10;
    num=num/10;
    a5=num%10;
    b5=a5*1;
    rev=b1+b2+b3+b4+b5;
    printf("The reversed number is:%d\n",rev);
    if(rev==num)
    {
    printf("Same\n");
    }
    else
    {
    printf("Not Same\n");
    }
    }
    not getting the desired result...if the number entered is same its showing 'not same' and if the number entered is different than its reversed order its showing 'same'.Could any1 please verify this code and post a solution to this problem.Thanks in advance :)

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. sir how will we find output in form of %d ?

    ReplyDelete
  8. sir how will we find output in form of %d ?

    ReplyDelete
  9. Exercise [J] (1)
    "Using the Conditional operator"

    int main()
    {
    char alph;
    printf("Enter the Character: ");
    scanf("%c",&alph);
    alph>=97?(alph<=122?printf("Lower Case Alphabet"):printf("Not Lower Case Alphabet")):printf("Not Lower Case Alphabet");
    }

    ReplyDelete
  10. Exercise [J] (2) is Also WRONG!!
    "Using Conditional Operator"

    int main()
    {
    char ch;
    printf("Enter Any Character: ");
    scanf("%c",&ch);

    ch>=0?ch<=47?printf("\nYou Entered Special Character\n"):
    (ch>=58?ch<=64?printf("\nYou Entered Special Character\n"):
    (ch>=91?ch<=96?printf("\nYou Entered Special Character\n"):
    (ch>=123?ch<=127?printf("\nYou Entered Special Character\n"):
    printf("\nYou NOT Entered Special Character\n"):
    printf("\nYou NOT Entered Special Character\n")):
    printf("\nYou NOT Entered Special Character\n")):
    printf("\nYou NOT Entered Special Character\n")):
    printf("\nYou NOT Entered Special Character\n");

    }

    ReplyDelete

Let Us C / Chapter 7 (The C Pre-processor)

                               Exercise [C] (a) Write down macro definitions for the following: 1. To test whether a character ente...