Header Ads Widget

Converter codes in C

 1. Temperature Converter


1. Convert Celsius to Fahrenheit 


#include<stdio.h>

int main()
{
    printf("\n\n\t\tAnonymous\n\n\n");
    
    float celsius, fahrenheit;
    printf("\n\nEnter temperature in Celsius: ");
    scanf("%f", &celsius);
    
    fahrenheit = (1.8*celsius) + 32;
    
    printf("\n\n\nTemperature in Fahrenheit is: %f ", fahrenheit);
    return 0;
}


Output :-


Anonymous

Enter temperature in Celsius: 61



Temperature in Fahrenheit is: 141.800003 


2. Convert Fahrenheit to Celsius


#include<stdio.h>
void main()
{
    printf("\n\n\t\tAnonymous\n\n\n");
    float celsius,fahrenheit;
 
    // Reads temperature in fahrenheit
    printf("\nEnter temperature in Fahrenheit:");
    scanf("%f",&fahrenheit);
 
    // Fahrenheit to celsius conversion formula
    celsius=(fahrenheit - 32)*5/9;
    // Print the result
    printf("\nCelsius = %.3f",celsius); //.3f means correct to 3 decimal places
}


Output :-


Anonymous



Enter temperature in Fahrenheit:58

Celsius = 14.444




2. Convert Meter into feet


#include<stdio.h>

void main()

{

    float m,f;

    printf("Meter : ");

    scanf("%f",&m);

    f = 3.2808399 * m;

    printf("Feet: %f",f);

}


Output :-


Meter : 25
Feet: 82.020996



3. Convert Feet into Meter


#include<stdio.h>

void main()

{

    float m,f;

    printf("Feet : ");

    scanf("%f",&f);

    m = f / 3.2808399;

    printf("Meter: %f",m);

}



Output :-



Feet : 25
Meter: 7.620000



4. Convert Kilometer into Meter, Centimeter 



/* C Program to Convert Kilometer to Meter Centimeter and Millimeter */
 
#include <stdio.h>
 
int main()
{
  float Millimeter, Centimeter, Meter, Kilometer;
 
  printf("\n Please Enter the Length in Kilometers (km)  :  ");
  scanf("%f", &Kilometer);
  
  Meter = Kilometer * 1000.0;
  Centimeter = Kilometer * 100000.0;
  Millimeter = Kilometer * 1000000.0;
 
    printf("\n %.2f Kilometers  = %.2f Meters", Kilometer, Meter);
    printf("\n %.2f Kilometers  = %.2f Centimeters", Kilometer, Centimeter);
    printf("\n %.2f Kilometers  = %.2f Millimeters", Kilometer, Millimeter);
  
    return 0;
}


Output :-



Please Enter the Length in Kilometers (km)  :  25

 25.00 Kilometers  = 25000.00 Meters
 25.00 Kilometers  = 2500000.00 Centimeters
 25.00 Kilometers  = 25000000.00 Millimeters


5. Convert Decimal to Binary


#include <stdio.h>
#include <math.h>

long decimalToBinary(int decimalnum)
{
    long binarynum = 0;
    int rem, temp = 1;

    while (decimalnum!=0)
    {
        rem = decimalnum%2;
        decimalnum = decimalnum / 2;
        binarynum = binarynum + rem*temp;
        temp = temp * 10;
    }
    return binarynum;
}

int main()
{
    int decimalnum;
    printf("Enter a Decimal Number: ");
    scanf("%d", &decimalnum);
    printf("Equivalent Binary Number is: %ld", decimalToBinary(decimalnum));
    return 0;
}


Output :-



Enter a Decimal Number: 524
Equivalent Binary Number is: 1000001100



6. Convert Binary to Decimal


#include <stdio.h>
#include <math.h>
int binaryToDecimal(long binarynum)
{
    int decimalnum = 0, temp = 0, remainder;
    while (binarynum!=0)
    {
        remainder = binarynum % 10;
        binarynum = binarynum / 10;
        decimalnum = decimalnum + remainder*pow(2,temp);
        temp++;
    }
    return decimalnum;
}

int main()
{
    long binarynum;
    printf("Enter a binary number: ");
    scanf("%ld", &binarynum);

    printf("Equivalent decimal number is: %d", binaryToDecimal(binarynum));
    return 0;
}


Output :-



Enter a binary number: 1010110
Equivalent decimal number is: 86



7. Convert Decimal to Octal


#include <stdio.h>
#include <math.h>
/* This function converts the decimal number "decimalnum"
 * to the equivalent octal number
 */
int decimalToOctal(int decimalnum)
{
    int octalnum = 0, temp = 1;

    while (decimalnum != 0)
    {
    octalnum = octalnum + (decimalnum % 8) * temp;
    decimalnum = decimalnum / 8;
        temp = temp * 10;
    }

    return octalnum;
}
int main()
{
    int decimalnum;

    printf("Enter a Decimal Number: ");
    scanf("%d", &decimalnum);

    printf("Equivalent Octal Number: %d", decimalToOctal(decimalnum));

    return 0;
}


Output :-


Enter a Decimal Number: 86
Equivalent Octal Number: 126



8. Convert Octal to Decimal


#include <stdio.h>
#include <math.h>
/* This function converts the octal number "octalnum" to the
 * decimal number and returns it.
 */
long octalToDecimal(int octalnum)
{
    int decimalnum = 0, temp = 0;

    while(octalnum != 0)
    {
        decimalnum = decimalnum + (octalnum%10) * pow(8,temp);
        temp++;
        octalnum = octalnum / 10;
    }

    return decimalnum;
}
int main()
{
    int octalnum;

    printf("Enter an octal number: ");
    scanf("%d", &octalnum);

    printf("Equivalent decimal number is: %ld", octalToDecimal(octalnum));

    return 0;
}


Output :-



Enter an octal number: 126
Equivalent decimal number is: 86



9. Convert Binary to Octal


#include <stdio.h>
#include <math.h>
//This function converts binary number to octal number
int binaryToOctal(long binarynum)
{
    int octalnum = 0, decimalnum = 0, i = 0;

    /* This while loop converts binary number "binarynum" to the
     * decimal number "decimalnum"
     */
    while(binarynum != 0)
    {
        decimalnum = decimalnum + (binarynum%10) * pow(2,i);
        i++;
        binarynum = binarynum / 10;
    }

    //i is re-initialized
    i = 1;

    /* This loop converts the decimal number "decimalnum" to the octal
     * number "octalnum"
     */
    while (decimalnum != 0)
    {
        octalnum = octalnum + (decimalnum % 8) * i;
        decimalnum = decimalnum / 8;
        i = i * 10;
    }

    //Returning the octal number that we got from binary number
    return octalnum;
}
int main()
{
    long binarynum;

    printf("Enter a binary number: ");
    scanf("%ld", &binarynum);

    // calling the function here
    printf("Equivalent octal value: %d", binaryToOctal(binarynum));

    return 0;
}


Output :-


Enter a binary number: 1010110
Equivalent octal value: 126



10. Convert Octal to Binary


#include <stdio.h>
#include <math.h>
//This function converts octal number to binary number
long octalToBinary(int octalnum)
{
    int decimalnum = 0, i = 0;
    long binarynum = 0;

    /* This loop converts octal number "octalnum" to the
     * decimal number "decimalnum"
     */
    while(octalnum != 0)
    {
decimalnum = decimalnum + (octalnum%10) * pow(8,i);
i++;
octalnum = octalnum / 10;
    }

    //i is re-initialized
    i = 1;

    /* This loop converts the decimal number "decimalnum" to the binary
     * number "binarynum"
     */
    while (decimalnum != 0)
    {
binarynum = binarynum + (decimalnum % 2) * i;
decimalnum = decimalnum / 2;
i = i * 10;
    }

    //Returning the binary number that we got from octal number
    return binarynum;
}
int main()
{
    int octalnum;

    printf("Enter an octal number: ");
    scanf("%d", &octalnum);

    //Calling the function octaltoBinary
    printf("Equivalent binary number is: %ld", octalToBinary(octalnum));

    return 0;
}


Output :-


Enter an octal number: 126
Equivalent binary number is: 1010110




11. Convert Days to Weeks, Years and Days



/* C Program to Convert Days to Years Weeks and Days */
 
#include <stdio.h>
 
int main()
{
  int NoOfDays, years, weeks, days;
 
  printf("\n Please Enter the Number of days  :  ");
  scanf("%d", &NoOfDays);
  
  years = NoOfDays / 365;
  weeks = (NoOfDays % 365) / 7;
  days = (NoOfDays % 365) % 7;
 
 
    printf("\n Years  = %d", years);
    printf("\n Weeks  = %d", weeks);
    printf("\n Days   = %d", days);
  
    return 0;
}


Output :-



Please Enter the Number of days  :  500
Years  = 1
 Weeks  = 19
 Days   = 2




12. Convert Lowercase string to Uppercase



/* C program to Convert String to Uppercase without using strupr() */

#include <stdio.h>
#include <string.h>
 
int main()
{
  char Str1[100];
  int i;
 
  printf("\n Please Enter a String that you want to Convert into Uppercase :  ");
  gets(Str1);
 
  for (i = 0; Str1[i]!='\0'; i++)
  {
  if(Str1[i] >= 'a' && Str1[i] <= 'z')
  {
  Str1[i] = Str1[i] -32;
}
  }

  printf("\n The given String in Upper Case = %s", Str1);
 
  return 0;
}


Output :-



Please Enter a String that you want to Convert into Uppercase :  hello anonymous!

The given String in Upper Case = HELLO ANONYMOUS!



13. Convert Uppercase string to Lowercase



/* C program to Convert String to Lowercase without using strlwr() */

#include <stdio.h>
#include <string.h>
 
int main()
{
  char Str1[100];
  int i;
 
  printf("\n Please Enter a String to Convert into Lowercase :  ");
  gets(Str1);
 
  for (i = 0; Str1[i]!='\0'; i++)
  {
  if(Str1[i] >= 'A' && Str1[i] <= 'Z')
  {
  Str1[i] = Str1[i] + 32;
}
  }

  printf("\n The given String in Lower Case = %s", Str1);
 
  return 0;
}


Output :-



Please Enter a String to Convert into Lowercase :  HELLO ANONYMOUS!

 The given String in Lower Case = hello anonymous!










Post a Comment

0 Comments