jeudi 4 novembre 2021

why is my program running both the if and else conditions?

I am a newbie to coding. its been like 2 to 3 weeks since I started learning c language. I have learnt using if and else statements from Youtube tutorials and made this small block of code :

#include <stdio.h>
#include <stdlib.h>
  float c,f;
main()
{
     int t=2;
     int o;
     printf("select any one option by entering '1' or '3'  \n 1. celcius to farenheit  \n 3. farenheit to celcius  \n enter any option ");
     scanf("%d",&o);
        if(t<o)
         { 
          printf(" \n enter the value of temperature in celcius ");
          scanf("%f",&c);
          f=(c*1.8)+32;
          printf(" \n the value of temperature of %f celcius in farenheit is %f ",c,f);
         }
        else(t>o);
         {
          printf("  \n enter the value of temperature in farenheit ");
          scanf("%f",&f);
          c=(f-32)/1.8;
          printf("  \n the value of temperature of %f farenheit in celcius is %f ",f,c);
         }
         return 0;
}

I have also tried this way

#include <stdio.h>
#include <stdlib.h>
 float c,f;
 
main()
{
     int o;
     printf("select any one option by entering 'a' or 'b'  \n 1. celcius to farenheit  \n 2. farenheit to celcius  \n enter any option ");
     scanf("%d",&o);
        if(o=1)
         { 
          printf(" \n enter the value of temperature in celcius ");
          scanf("%f",&c);
          f=(c*1.8)+32;
          printf(" \n the value of temperature of %f celcius in farenheit is %f ",c,f);
         }
        else(o=2);
         {
          printf("  \n enter the value of temperature in farenheit ");
          scanf("%f",&f);
          c=(f-32)/1.8;
          printf("  \n the value of temperature of %f farenheit in celcius is %f ",f,c);
         }
}

so the problem is this code is running both the conditions one after the other. so please critique this and also recommend me some good beginner c language self learning books or tutorials if you have any. thank you.

Aucun commentaire:

Enregistrer un commentaire