Article: C Program to Convert Kelvin to Celsius

You're reading C Program to Convert Kelvin to Celsius, last updated on 2022-06-01 06:53:56

2022-04-06

In this post, we will learn how to convert temperature from Kelvin to degree Celsius using the C Programming language.

In this post, we will learn how to convert temperature from Kelvin to degree Celsius using the C Programming language.

Kelvin and Celsius are two temperature scales used to measure the temperature of an object.

The below program asks the user to enter the temperature in Kelvin, then it converts the temperature to degree Celsius using the conversion factor: 1 Kelvin = -272.15 degree Celsius

So, without further ado, let’s begin this tutorial.  

// C Program to Convert Kelvin to Celsius
#include 
int main(){
    float kel, cel;
    
    // Taking input
    printf("Enter the temperature in Kelvin: ");
    scanf("%f", &kel);
    
    // Kelvin to celsius conversion
    cel = kel - 273.15;
    
    // Displaying output
    printf("%.2f Kelvin = %.2f Celsius", kel, cel);
    
    return 0;
}

Output

Enter the temperature in Kelvin: 346
346.00 Kelvin = 72.85 Celsius

How Does This Program Work?

    float kel, cel;

In the above program, we have declared two floating data type variables named kel and cel.

    // Taking input
    printf("Enter the temperature in Kelvin: ");
    scanf("%f", &kel);

Then, the user is asked to enter the temperature in Kelvin

    // Kelvin to celsius conversion
    cel = kel - 273.15;

We convert the temperature from Kelvin to degree Celsius using the conversion factor: 1 Kelvin = -272.15 degree Celsius.

    // Displaying output
    printf("%.2f Kelvin = %.2f Celsius", kel, cel);

The equivalent temperature in degree Celsius is printed on the screen using the printf() function.

Conclusion

I hope after going through this post, you understand how to convert temperature from Kelvin to degree Celsius using the C Programming language.

If you have any doubts regarding the tutorial, then contact us in the comment section. We will be delighted to assist you.

C++ Program to Convert Celsius to Kelvin

29-04-2022 · To convert the temperature from degree Celsius to Kelvin, we add 273.15 to the Celsius scale. This gives us the temperature in the Kelvin scale. The below program asks the user to enter the temperature in degree Celsius, then it converts it into Kelvin using the formula: Kelvin = Celsius + 273.15. So, without further ado, let’s begin this post.

29-04-2022

In this post, we will learn how to convert temperature from degree Celsius to Kelvin using the C Programming language.

To convert the temperature from degree Celsius to Kelvin, we add 273.15 to the Celsius scale. This gives us the temperature in the Kelvin scale.

The below program asks the user to enter the temperature in degree Celsius, then it converts it into Kelvin using the formula: Kelvin = Celsius 273.15.

So, without further ado, let’s begin this post.

// C++ Program to Convert Celsius to Kelvin
#include 
using namespace std;

int main(){
    float celsius, kelvin;
    
    // Asking for input
    cout << "Enter the temperature in Celsius: ";
    cin >> celsius;
    
    // Celsius to kelvin
    kelvin = celsius + 273.15;
    
    // Display Output
    cout << "The temperature in the Kelvin scale: " << kelvin << endl;
    return 0;
}

Output

Enter the temperature in Celsius: 42.8
The temperature in the Kelvin scale: 315.95

How Does This Program Work?

    float celsius, kelvin;

In this program, we have declared two floating data type variables named celsius and kelvin.

    // Asking for input
    cout << "Enter the temperature in Celsius: ";
    cin >> celsius;

The user is asked to enter the temperature in degree Celsius. The value gets stored in the celsius named variable.

    // Celsius to kelvin
    kelvin = celsius + 273.15;

The temperature is converted from degree Celsius to Kelvin using the formula: Kelvin = Celsius 273.15. The temperature in Kelvin scale gets stored in the kelvin named variable.

    // Display Output
    cout << "The temperature in Kelvin scale: " << kelvin << endl;

Finally, the temperature in the Kelvin scale is displayed on the screen using the cout statement.

Conclusion

I hope after going through this post, you understand how to convert temperature in degree Celsius to Kelvin using the C Programming language.

If you have any doubt regarding the program, then contact us in the comment section. We will be delighted to assist you.

Also Read:

C++ Program to Find Prime Factors of a Number

05-12-2021 · Enter any number: 42 2 is a prime factor of 42 3 is a prime factor of 42 7 is a prime factor of 42 Conclusion. I hope after going through this post, you understand how to find the prime factors of a number using C++ Programming language. If you have any doubts regarding the program, feel free to contact us in the comment section.

05-12-2021

In this post, we will learn how to find the prime factors of a number using C Programming language.

Suppose the user enters a number 18, then it’s prime factors will be 2 and 3. We will find the prime factors using the following methods:

  1. Using While Loop
  2. Using for Loop

So, without further ado, let’s begin this tutorial.

// C++ Program to Find Prime Factors of a Number
#include 
using namespace std;

int main(){
    int num, i = 1, j, count;
    
    // Asking for input
    cout << "Enter any number to find prime factors: ";
    cin >> num;
    
    while (i <= num){
        count = 0;
        if (num % i == 0){
            j = 1;
            while (j <= i){
                if (i % j == 0){
                    count++;
                }
                j++;
            }
            if (count == 2){
                cout << i << " is a prime factor of " << num << endl;
            }
        }
        i++;
    }
    return 0;
}

Output

Enter any number to find prime factors: 15
3 is a prime factor of 15
5 is a prime factor of 15

How Does This Program Work ?

    int num, i = 1, j, count;

In this program, we have declared four int data type variables named num, i, j and count.

    // Asking for input
    cout << "Enter any number to find prime factors: ";
    cin >> num;

Then, the user is asked to enter any number to find its prime factors.

    while (i <= num){
        count = 0;
        if (num % i == 0){
            j = 1;
            while (j <= i){
                if (i % j == 0){
                    count++;
                }
                j++;
            }

We have used a while loop to find the factors of the entered number.

            if (count == 2){
                cout << i << " is a prime factor of " << num << endl;
            }

We display the prime factors of the entered number with the help of the cout statement.

C Program to Find Prime Factors of a Number Using For Loop

// C++ Program to Find Prime Factors of a Number
#include 
using namespace std;

int main(){
    int num, i, j, count;
    
    // Asking for input
    cout << "Enter any number: ";
    cin >> num;
    
    for (i = 1; i <= num; i++){
        count = 0;
        if (num % i == 0){
            for (j = 1; j <= i; j++){
                if (i % j == 0){
                    count++;
                }
            }
        }
            if (count == 2){
                cout << i << " is a prime factor of " << num << endl;
            }
        }
    return 0;
}

Output

Enter any number: 42
2 is a prime factor of 42
3 is a prime factor of 42
7 is a prime factor of 42

Conclusion

I hope after going through this post, you understand how to find the prime factors of a number using C Programming language.

If you have any doubts regarding the program, feel free to contact us in the comment section. We will be delighted to assist you.

Also Read:

C++ Program to Convert Fahrenheit to Celsius

21-04-2022 · We convert the temperature from degree Fahrenheit to degree Celsius using the formula: C = (F – 32) x 5/9. // Display output cout << "Temperature in degree Celsius: " << celsius << endl; After that, the temperature in degree Celsius is displayed on the screen using the cout statement.

21-04-2022

In this post, we will learn how to convert Fahrenheit to Celsius using the C Programming language.

The formula used to convert temperature from degree Fahrenheit to degree Celsius is:

where C is the temperature in Celsius and F is the temperature in Fahrenheit.

We will convert the temperature from Fahrenheit to Celsius using the above conversion formula.

So, without further ado, let’s begin this tutorial.

C Program

// C++ Program to Convert Fahrenheit to Celsius
#include 
using  namespace std;

int main(){
    float fahren, celsius;
    
    // Asking for input
    cout << "Enter the temperature in degree Fahrenheit: ";
    cin >> fahren;
    
    // Fahrenheit to celsius 
    celsius = (fahren - 32) * 5/9;
    
    // Display output
    cout << "The temperature in degree Celsius: " << celsius << endl;
    return 0;
}

Output

Enter the temperature in degree Fahrenheit: 101.7
The temperature in degree Celsius: 38.7222

How Does This Program Work?

    float fahren, celsius;

In this program, we have declared two floating data type variables named fahren and celsius.

    // Asking for input
    cout << "Enter the temperature in degree Fahrenheit: ";
    cin >> fahren;

Then, the user is asked to enter the temperature in degree Fahrenheit.

    // Fahrenheit to celsius 
    celsius = (fahren - 32) * 5/9;

We convert the temperature from degree Fahrenheit to degree Celsius using the formula: C = (F – 32) x 5/9.

    // Display output
    cout << "Temperature in degree Celsius: " << celsius << endl;

After that, the temperature in degree Celsius is displayed on the screen using the cout statement.

Conclusion

I hope after going through this post, you understand how to convert Fahrenheit to Celsius using the C Programming language.

If you have any doubt regarding the program, then contact us in the comment section. We will be delighted to assist you.

Also Read:

C Program to Calculate Perimeter of Rhombus

11-04-2022 · In this post, we will learn how to calculate the perimeter of a rhombus using the C Programming language.. A rhombus is a quadrilateral whose four sides all have the same length. The opposite sides of the rhombus are parallel in nature. Since all the sides of the rhombus have equal length, then its perimeter will be 4 * length of one side.. We will use the following approaches to find …

11-04-2022

In this post, we will learn how to calculate the perimeter of a rhombus using the C Programming language.

A rhombus is a quadrilateral whose four sides all have the same length. The opposite sides of the rhombus are parallel in nature.

Since all the sides of the rhombus have equal length, then its perimeter will be 4 * length of one side.

We will use the following approaches to find the perimeter of the rhombus:

  1. Using Standard Method
  2. Using Functions
  3. Using Pointers

So, without further ado, let’s begin this tutorial.

// C Program to Calculate Perimeter of Rhombus
#include 
int main(){
    float side, perimeter;
    
    // Asking for input
    printf("Enter the side of the rhombus: ");
    scanf("%f", &side);
    
    // Calculating perimeter of rhombus
    perimeter = 4 * side;
    
    // Display output
    printf("The Perimeter of the rhombus = %.2f", perimeter);
    return 0;
}

Output

Enter the side of the rhombus: 7.2
The perimeter of the rhombus = 28.80

How Does This Program Work?

    float side, perimeter;

In this program, we have declared two floating data type variables named side and perimeter.

    // Asking for input
    printf("Enter the side of the rhombus: ");
    scanf("%f", &side);

The user is asked to enter the length of the side of the rhombus. The entered value gets stored in the side named variable.

    // Calculating perimeter of rhombus
    perimeter = 4 * side;

The perimeter of the rhombus is calculated using the formula: perimeter = 4 x Length of Side of Rhombus.

    // Display output
    printf("Perimeter of the rhombus = %.2f", perimeter);

The perimeter of the rhombus is printed on the screen using the printf() function. We have used %.2f format specifier to limit the perimeter to 2 decimal places.

C Program to Calculate Perimeter of Rhombus Using Functions

// C Program to Calculate Perimeter of Rhombus Using Functions
#include 
float rhombusPerimeter(float s){
    return 4 * s;
}

int main(){
    float side, result;
    
    // Asking for input
    printf("Enter the side of the rhombus: ");
    scanf("%f", &side);
    
    // Calling out custom function
    result = rhombusPerimeter(side);
    
    // Display output
    printf("The Perimeter of the rhombus = %.1f", result);
    return 0;
}

Output

Enter the side of the rhombus: 5.4
The perimeter of the rhombus = 21.6

C Program to Calculate Perimeter of Rhombus Using Pointers

// C Program to Calculate Perimeter of Rhombus Using Pointers
#include 
void perimeter(float *s, float *p){
    *p = (4 * (*s));
}

int main(){
    float s, p;
    
    // Asking for input
    printf("Enter the length of the side: ");
    scanf("%f", &s);
    
    perimeter(&s, &p);
    
    // Display output
    printf("The Perimeter of the rhombus is = %.2f", p);
    return 0;
}

Output

Enter the length of the side: 12.1
The perimeter of the rhombus is = 48.40

Conclusion

I hope after going through this post, you understand how to calculate the perimeter of a rhombus using the C Programming language.

If you have any doubts regarding the program, then contact us in the comment section. We will be delighted to assist you.

Also Read:

C++ Program to Convert Celsius to Fahrenheit

19-04-2022 · In this post, we will learn how to convert Celsius to Fahrenheit using the C++ Programming language.. The below program asks the user to enter the temperature in degree Celsius, then it converts the temperature into degree Fahrenheit using the formula: F = (C x 9/5) + 32, where C is the temperature in degree Celsius.

19-04-2022

In this post, we will learn how to convert Celsius to Fahrenheit using the C Programming language.

The below program asks the user to enter the temperature in degree Celsius, then it converts the temperature into degree Fahrenheit using the formula: F = (C x 9/5) 32, where C is the temperature in degree Celsius.

So, without further ado, let’s begin this tutorial. 

// C++ Program to Convert Celsius to Fahrenheit
#include 
using namespace std;

int main(){
    float celsius, fahrenheit;
    
    // Asking for input
    cout << "Enter the temperature in Celsius: ";
    cin >> celsius;
    
    // Celsius to fahrenheit
    fahrenheit = (celsius * 9.0) / 5.0 + 32;
    
    // Display result
    cout << "The temperature in degree Fahrenheit: " << fahrenheit << endl;
    
    return 0;
}

Output

Enter the temperature in Celsius: 40.6
The temperature in degree Fahrenheit: 105.08

How Does This Program Work?

    float celsius, fahrenheit;

In this program, we have declared two floating data type variables named celsius and fahrenheit.

    // Asking for input
    cout << "Enter the temperature in Celsius: ";
    cin >> celsius;

Then, the user is asked to enter the temperature in degree Celsius.

    // Celsius to fahrenheit
    fahrenheit = (celsius * 9.0) / 5.0 + 32;

The temperature is converted from degree Celsius to degree Fahrenheit using the conversion formula: F = (C x 9/5) 32.

    // Display result
    cout << "The temperature in degree Fahrenheit: " << fahrenheit << endl;

Finally, the equivalent temperature in degree Fahrenheit is printed on the screen using the cout statement. 

Conclusion

I hope after going through this post, you understand how to convert Celsius to Fahrenheit using the C Programming language.

If you have any doubt regarding the program, then contact us in the comment section. We will be delighted to assist you.

Also Read:

Java Int to String

30-05-2021 · Hello coders, today we are going to solve Java Int to String HackerRank Solution.. Problem. You are given an integer n, you have to convert it into a string.. Please complete the partially completed code in the editor. If your code successfully converts n into a string s the code will print “Good job”.Otherwise it will print “Wrong answer”.

30-05-2021

Hello coders, today we are going to solve Java Int to String HackerRank Solution.

Java Int to String

Problem

You are given an integer n, you have to convert it into a string.

Please complete the partially completed code in the editor. If your code successfully converts n into a string s the code will print “Good job”. Otherwise it will print “Wrong answer”.

n can range between -100 to 100 inclusive.

Sample Input

 100

Sample Output

 Good job

Solution – Java Int to String

import java.util.*;
import java.security.*;
public class Solution {
 public static void main(String[] args) {

  DoNotTerminate.forbidExit();

  try {
   Scanner in = new Scanner(System.in);
   int n = in .nextInt();
   in.close();
   //String s=???; Complete this line below
   //Write your code here

   String s = "" ;  
   if(n>=-100 && n<=100)
   {
        s = Integer.toString(n);
   }

   if (n == Integer.parseInt(s)) {
    System.out.println("Good job");
   } else {
    System.out.println("Wrong answer.");
   }
  } catch (DoNotTerminate.ExitTrappedException e) {
   System.out.println("Unsuccessful Termination!!");
  }
 }
}

//The following class will prevent you from terminating the code using exit(0)!
class DoNotTerminate {

 public static class ExitTrappedException extends SecurityException {

  private static final long serialVersionUID = 1;
 }

 public static void forbidExit() {
  final SecurityManager securityManager = new SecurityManager() {
   @Override
   public void checkPermission(Permission permission) {
    if (permission.getName().contains("exitVM")) {
     throw new ExitTrappedException();
    }
   }
  };
  System.setSecurityManager(securityManager);
 }
}

Disclaimer: The above Problem ( Java Int to String ) is generated by Hacker Rank but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

Related posts