C Programming - CodingTute https://codingtute.com/category/c-programming/ Learn to code in an easier way Fri, 28 Jul 2023 18:04:20 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.2 https://codingtute.com/wp-content/uploads/2021/06/cropped-codingtute_favicon-32x32.png C Programming - CodingTute https://codingtute.com/category/c-programming/ 32 32 187525380 C Program to Display Prime Numbers Between Two Numbers https://codingtute.com/c-program-to-display-prime-numbers-between-two-numbers/ Fri, 28 Jul 2023 18:04:14 +0000 https://codingtute.com/?p=4948 In this tutorial, you will learn how to display prime numbers between two numbers using C program. Also Read: Check a Number is Prime or Not C Program to Display Prime Numbers Beeeen Two Numbers To write a C program to display the prime numbers between two intervals, you can use a function that takes ... Read more

The post C Program to Display Prime Numbers Between Two Numbers appeared first on CodingTute.

]]>
In this tutorial, you will learn how to display prime numbers between two numbers using C program.

Also Read: Check a Number is Prime or Not

C Program to Display Prime Numbers Beeeen Two Numbers

To write a C program to display the prime numbers between two intervals, you can use a function that takes in two integers as arguments and prints out all of the prime numbers within the specified range. Here is an example of how you can implement this function:

#include <stdbool.h>
#include <math.h>

bool is_prime(int n) {
    if (n <= 1) {
        return false;
    }
    for (int i = 2; i <= sqrt(n); i++) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}

void display_primes(int lower, int upper) {
    for (int i = lower; i <= upper; i++) {
        if (is_prime(i)) {
            printf("%d\n", i);
        }
    }
}

int main() {
    int lower, upper;
    printf("Enter the lower bound: ");
    scanf("%d", &lower);
    printf("Enter the upper bound: ");
    scanf("%d", &upper);
    printf("The prime numbers between %d and %d are:\n", lower, upper);
    display_primes(lower, upper);
    return 0;
}
C

Output

Enter the lower bound: 10
Enter the upper bound: 56
The prime numbers between 10 and 56 are:
11
13
17
19
23
29
31
37
41
43
47
53

Explanation

The display_primes() function uses a loop to iterate through the integers from the lower bound to the upper bound, and for each integer it calls the is_prime() function to determine if it is a prime number. If the number is prime, it is printed out.

The is_prime() function uses a loop to iterate through the integers from 2 to the square root of the number, and checks if the number is divisible by any of these integers using the modulus operator (%). If the number is divisible by any of these integers, the function returns false because it is not prime.

If the loop completes without finding a divisor, the function returns true because the number is prime. This process continues until all of the integers in the specified range have been checked, and the prime numbers are printed out.

You can find more C Programming Examples here.

Follow us on Facebook, YouTube, Instagram, and Twitter for more exciting content and the latest updates.

The post C Program to Display Prime Numbers Between Two Numbers appeared first on CodingTute.

]]>
4948
C Program to Check Armstrong Number https://codingtute.com/armstrong-number-in-c/ Fri, 21 Jul 2023 18:22:19 +0000 https://codingtute.com/?p=4950 In this article, you will learn to check a given number is an Armstrong number or not using the c program. What is an Armstrong Number? An Armstrong number is a number that is equal to the sum of the cubes of its digits. For example, 371 is an Armstrong number because 3^3 + 7^3 ... Read more

The post C Program to Check Armstrong Number appeared first on CodingTute.

]]>
In this article, you will learn to check a given number is an Armstrong number or not using the c program.

What is an Armstrong Number?

An Armstrong number is a number that is equal to the sum of the cubes of its digits. For example, 371 is an Armstrong number because 3^3 + 7^3 + 1^3 = 371. In this tutorial, you will learn how to check a number is Armstrong or not.

Armstrong Number in C

Here is a C program that checks whether a number is an Armstrong number:

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

#include <stdbool.h>

bool is_armstrong(int n) {
    int original_n = n;
    int sum = 0;
    while (n > 0) {
        int digit = n % 10;
        sum += pow(digit, 3);
        n /= 10;
    }
    return sum == original_n;
}

int main() {
    int n;
    printf("Enter a number: ");
    scanf("%d", &n);
    if (is_armstrong(n)) {
        printf("%d is an Armstrong number\n", n);
    } else {
        printf("%d is not an Armstrong number\n", n);
    }
    return 0;
}
Run Program

Output

Enter a number: 371
371 is an Armstrong number

Explanation

The is_armstrong() function uses a loop to iterate through the digits of the number, and calculates the sum of the cubes of the digits. It then checks if the sum is equal to the original number, and returns a boolean value indicating whether the number is an Armstrong number or not.

To check if a number is an Armstrong number, the function first stores a copy of the original number in a separate variable. It then initializes a variable sum to 0, which will be used to store the sum of the cubes of the digits.

The function then enters a loop that continues until the number becomes 0. Inside the loop, it extracts the last digit of the number using the modulus operator (%), and adds the cube of this digit to the sum.

It then divides the number by 10 to remove the last digit. When the loop completes, the function checks if the sum is equal to the original number, and returns true if it is, or false if it is not. This process determines whether the number is an Armstrong number or not.

You can find more C Programming Examples here.

Follow us on Facebook, YouTube, Instagram, and Twitter for more exciting content and the latest updates.

The post C Program to Check Armstrong Number appeared first on CodingTute.

]]>
4950
C Program to Convert Celsius to Fahrenheit https://codingtute.com/c-program-to-convert-celsius-to-fahrenheit/ Sat, 08 Jul 2023 16:25:01 +0000 https://codingtute.com/?p=5393 In this article, we will discuss how to write a C program to convert temperature from Celsius to Fahrenheit. We will provide a step-by-step explanation and include code examples to make it easy for beginners to understand the concept. So, let’s get started! Understanding the Conversion Formula Before we dive into the code, let’s understand ... Read more

The post C Program to Convert Celsius to Fahrenheit appeared first on CodingTute.

]]>
In this article, we will discuss how to write a C program to convert temperature from Celsius to Fahrenheit. We will provide a step-by-step explanation and include code examples to make it easy for beginners to understand the concept. So, let’s get started!

Understanding the Conversion Formula

Before we dive into the code, let’s understand the formula used to convert temperature from Celsius to Fahrenheit. The formula is as follows:

F = (C * 9/5) + 32

Where:

  • F represents the temperature in Fahrenheit.
  • C represents the temperature in Celsius.

To convert Celsius to Fahrenheit, we follow the following steps:

  1. Multiply the temperature in Celsius by 9/5.
  2. Add 32 to the result obtained in step 1.

Let’s understand this formula with a few examples:

Example 1:
Suppose we have a temperature of 25 degrees Celsius. We can convert it to Fahrenheit using the formula:

F = (25 * 9/5) + 32

Calculating the above expression:

F = (45/5) + 32
F = 9 + 32
F = 41

Therefore, 25 degrees Celsius is equivalent to 41 degrees Fahrenheit.

Example 2:
Let’s take another example. Consider a temperature of 0 degrees Celsius. Using the formula, we have:

F = (0 * 9/5) + 32

Calculating the above expression:

F = 0 + 32
F = 32

Hence, 0 degrees Celsius is equal to 32 degrees Fahrenheit.

Example 3:
Now, let’s consider a negative temperature, say -10 degrees Celsius. Applying the formula:

F = (-10 * 9/5) + 32

Calculating the above expression:

F = (-90/5) + 32
F = -18 + 32
F = 14

Thus, -10 degrees Celsius is equal to 14 degrees Fahrenheit.

Also read: C Programming Examples

Convert Celsius to Fahrenheit using C Program

Now that we understand the formula and its usage, let’s write a C program to convert Celsius to Fahrenheit. Here’s the code:

#include <stdio.h>

int main() {
    float celsius, fahrenheit;

    printf("Enter temperature in Celsius: ");
    scanf("%f", &celsius);

    fahrenheit = (celsius * 9/5) + 32;

    printf("Temperature in Fahrenheit: %.2f\n", fahrenheit);

    return 0;
}

Explanation of the Code

Let’s break down the code and explain each part:

  1. We start by including the necessary header file, <stdio.h>, which contains the functions for input and output operations.
  2. Next, we define the main function, which is the entry point of our program.
  3. Inside the main function, we declare two variables of type float: celsius and fahrenheit. These variables will store the temperature values.
  4. We use the printf function to display a prompt asking the user to enter the temperature in Celsius.
  5. Then, we use the scanf function to read the input from the user and store it in the celsius variable.
  6. Next, we apply the conversion formula (celsius * 9/5) + 32 to calculate the temperature in Fahrenheit. The result is stored in the fahrenheit variable.
  7. Finally, we use the printf function to display the converted temperature in Fahrenheit. The format specifier %.2f is used to display the result with two decimal places.
  8. The program ends with the return 0 statement, indicating successful execution.

Testing the Program

To test the program, you can compile and run it. Enter a temperature value in Celsius when prompted, and the program will display the converted temperature in Fahrenheit.

Here are a few examples of program execution:

Example 1:

Enter temperature in Celsius: 25
Temperature in Fahrenheit: 77.00

Example 2:

Enter temperature in Celsius: 0
Temperature in Fahrenheit: 32.00

Example 3:

Enter temperature in Celsius: -10
Temperature in Fahrenheit: 14.00

Congratulations! You have successfully written a C program to convert Celsius to Fahrenheit.

Conclusion

In this article, we discussed how to write a C program to convert temperature from Celsius to Fahrenheit. We explained the conversion formula, provided a step-by-step explanation of the code, and included a complete code example.

Additionally, we demonstrated the formula’s usage with examples to further clarify the conversion process. Now you can use this program to convert temperatures in your own projects. Happy coding!

Follow us on Facebook, YouTube, Instagram, and Twitter for more exciting content and the latest updates.

The post C Program to Convert Celsius to Fahrenheit appeared first on CodingTute.

]]>
5393
C Program to Calculate the Power of a Number https://codingtute.com/c-program-to-calculate-the-power-of-a-number/ Sat, 25 Mar 2023 20:03:12 +0000 https://codingtute.com/?p=4940 In this tutorial, you will learn how to calculate the Power of a Number using a C program. C Program to Calculate the Power of a Number Output Explanation The function first checks if the exponent is 0. If it is, the function returns 1, since any number raised to the power of 0 is ... Read more

The post C Program to Calculate the Power of a Number appeared first on CodingTute.

]]>
In this tutorial, you will learn how to calculate the Power of a Number using a C program.

C Program to Calculate the Power of a Number

#include <stdio.h>

int power(int base, int exponent) {
    if (exponent == 0) {
        return 1;
    }
    return base * power(base, exponent - 1);
}

int main() {
    int base, exponent;
    printf("Enter the base: ");
    scanf("%d", &base);
    printf("Enter the exponent: ");
    scanf("%d", &exponent);
    int result = power(base, exponent);
    printf("%d^%d = %d\n", base, exponent, result);
    return 0;
}
Run Code in Online C Compiler

Output

Enter the base: 3
Enter the exponent: 4
3^4 = 81

Explanation

The function first checks if the exponent is 0. If it is, the function returns 1, since any number raised to the power of 0 is 1. If the exponent is not 0, the function calls itself with the base and exponent – 1 as arguments, and returns the result of the base multiplied by the result of the recursive call. This continues until the exponent becomes 0, at which point the function returns 1 and the recursive calls begin to unwind, multiplying the base by each result as they return. This process calculates the power of the base raised to the exponent.

You can find more C Programming Examples here.

Follow us on Facebook, YouTube, Instagram, and Twitter for more exciting content and the latest updates.

The post C Program to Calculate the Power of a Number appeared first on CodingTute.

]]>
4940
C Program to Check Whether a Number is Palindrome or Not https://codingtute.com/c-program-to-check-whether-a-number-is-palindrome-or-not/ Sun, 08 Jan 2023 07:21:16 +0000 https://codingtute.com/?p=4928 In this tutorial, you will learn how to check a number is a palindrome or not using a C program. C Program to Check Whether a Number is Palindrome or Not Output Explanation This program first reads in an integer from the user and stores it in the variable num. It then stores a copy ... Read more

The post C Program to Check Whether a Number is Palindrome or Not appeared first on CodingTute.

]]>
In this tutorial, you will learn how to check a number is a palindrome or not using a C program.

C Program to Check Whether a Number is Palindrome or Not

#include <stdio.h>

int main()
{
    // variables to store the number and its reverse
    int num, reversed_num=0;

    // variable to store the remainder when the number is divided by 10
    int remainder;

    // variable to store a copy of the original number
    int original_num;

    printf("Enter an integer: ");
    scanf("%d", &num);

    original_num = num; // store the original number

    // reverse the number
    while(num != 0)
    {
        remainder = num % 10; // get the remainder when the number is divided by 10

        // append the remainder to the reversed number
        reversed_num = reversed_num * 10 + remainder; 
        num /= 10; // divide the number by 10 to remove the last digit
    }

    // check if the original number is equal to its reverse
    if(original_num == reversed_num)
        printf("%d is a palindrome.", original_num);
    else
        printf("%d is not a palindrome.", original_num);

    return 0;
}

Output

Enter an integer: 565
565 is a palindrome.

Explanation

This program first reads in an integer from the user and stores it in the variable num. It then stores a copy of the original number in the variable original_num.

Next, the program enters a while loop that continues until num is equal to 0. Inside the loop, it calculates the remainder when num is divided by 10 and stores it in the variable remainder. It then appends the remainder to the reversed number, which is stored in the variable reversed_num. Finally, it divides num by 10 to remove the last digit.

After the while loop completes, the program checks if the original number is equal to its reverse. If they are equal, it prints that the number is a palindrome. If they are not equal, it prints that the number is not a palindrome.

You can find more C Programming Examples here.

Follow us on Facebook, YouTube, Instagram, and Twitter for more exciting content and the latest updates.

The post C Program to Check Whether a Number is Palindrome or Not appeared first on CodingTute.

]]>
4928
C Program to Reverse a Number https://codingtute.com/c-program-to-reverse-a-number/ Sun, 08 Jan 2023 06:36:09 +0000 https://codingtute.com/?p=4926 In this tutorial, you will learn how to reverse a number using c programming. C Program to Reverse a Integer Output Explanation You can find more C Programming Examples here.

The post C Program to Reverse a Number appeared first on CodingTute.

]]>
In this tutorial, you will learn how to reverse a number using c programming.

C Program to Reverse a Integer

#include <stdio.h>

int main() {
  int num, rev = 0;

  // Read the number from the user
  printf("Enter a number: ");
  scanf("%d", &num);

  // Reverse the number
  while (num > 0) {
    rev = rev * 10 + num % 10;
    num /= 10;
  }

  // Print the reversed number
  printf("Reversed number: %d\n", rev);

  return 0;
}

Output

Enter a number: 1234
Reversed number: 4321

Explanation

  • The program first reads the number from the user using scanf().
  • It then initializes a variable rev to 0 and enters a loop. The loop continues as long as num is greater than 0.
  • Inside the loop, rev is updated to rev * 10 + num % 10. This effectively appends the last digit of num to rev. For example, if num is 123 and rev is 0, then after the first iteration, rev will be 3, after the second iteration it will be 30, and after the third iteration it will be 300.
  • num is then updated to num / 10, which removes the last digit. For example, if num is 123, it will become 12 after the first iteration, 1 after the second iteration, and 0 after the third iteration, at which point the loop will terminate.
  • Finally, the reversed number is printed using printf().

You can find more C Programming Examples here.

Follow us on Facebook, YouTube, Instagram, and Twitter for more exciting content and the latest updates.

The post C Program to Reverse a Number appeared first on CodingTute.

]]>
4926
C Program to find Sum of Digits in a Number https://codingtute.com/c-program-to-find-sum-of-digits-in-a-number/ Sun, 22 May 2022 13:22:22 +0000 https://codingtute.com/?p=2941 In this example, you will learn to find the sum of digits in a given number using C Programming. For example, 123 should give the output 6 (1+2+3). C Program to find Sum of Digits in a Number Output Explanation We take input from user and store it in a variable n. We will initialize ... Read more

The post C Program to find Sum of Digits in a Number appeared first on CodingTute.

]]>
In this example, you will learn to find the sum of digits in a given number using C Programming.

For example, 123 should give the output 6 (1+2+3).

C Program to find Sum of Digits in a Number

#include <stdio.h>
int main() {
  long n;
  int sum = 0;
  printf("Enter an integer: ");
  scanf("%ld", &n);
 
  while(n!=0){
      sum+=(n%10);  // sum = sum + (n%10)
      n/=10;   //will remove the last digit
  }

  printf("Sum of digits: %d", sum);
}

Output

Enter an integer: 1234
Sum of digits: 10

Explanation

We take input from user and store it in a variable n. We will initialize a variable sum=0. Now we will run the while loop till n becomes 0 and inside the loop, we will remove the last digit of n per each iteration. n%10 returns the last digit of n and n/10 removes the last digit of n.

So sum+=(n%10) will add the last digit of n at each iteration of the loop.

Follow us on Facebook, YouTube, Instagram, and Twitter for more exciting content and the latest updates.

The post C Program to find Sum of Digits in a Number appeared first on CodingTute.

]]>
2941
C program to Check for a Perfect Square https://codingtute.com/c-program-to-check-for-a-perfect-square/ Sun, 08 May 2022 06:33:36 +0000 https://codingtute.com/?p=2953 In this example, you will learn to check a given number is a perfect square or not using the c program. A perfect square is an integer that is the square of another integer. For example, 25 is a perfect square as 5 square is 25. C program to Check for a Perfect Square Output ... Read more

The post C program to Check for a Perfect Square appeared first on CodingTute.

]]>
In this example, you will learn to check a given number is a perfect square or not using the c program.

A perfect square is an integer that is the square of another integer. For example, 25 is a perfect square as 5 square is 25.

C program to Check for a Perfect Square

#include<stdio.h>
int main()
{
    int i, number, flag=0;

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

    if(number == 1 || number == 0){
        printf("%d is a Perfect Square.", number);
        flag=1;
    }

    for(i = 2; i <= number/2; i++)
    {
        if(number == i*i)
        {
            printf("%d is a Perfect Square.", number);
            flag=1;
            break;
        }
    }
    if(flag == 0)
        printf("%d is not a Perfect Square\n", number);

    return 0;
}
Run Code on Online C Compiler

Output

Enter a number: 25
25 is a Perfect Square.

Explanation

At first, we will add the known condition i.e 0 and 1 are perfect squares. Now, will run a loop till number/2 as no number more than 4 will have its root whose value is more than half of its value.

For every iteration of i, number == i*i will check if the square of i is matching with number. If it matches then the number has a perfect square and we will make the flag=1.

If no i value satisfies the perfect square condition, then flag value remains 0. At the end of the program if the flag=0, then the given number is not a perfect square.

Follow us on Facebook, YouTube, Instagram, and Twitter for more exciting content and the latest updates.

The post C program to Check for a Perfect Square appeared first on CodingTute.

]]>
2953
C Program to Count Number of Digits in an Integer https://codingtute.com/c-program-to-count-number-of-digits-in-an-integer/ Sat, 06 Nov 2021 18:39:15 +0000 https://codingtute.com/?p=2903 In this example, you will learn to count the number of digits present in the given integer. C Program to Count Number of Digits in an Integer Output Explanation Firstly, we will read the integer from the user and store it in a variable n and initialize a variable count with 0. Now, we will ... Read more

The post C Program to Count Number of Digits in an Integer appeared first on CodingTute.

]]>
In this example, you will learn to count the number of digits present in the given integer.

C Program to Count Number of Digits in an Integer

#include <stdio.h>
int main() {
  long n;
  int count = 0;
  printf("Enter an integer: ");
  scanf("%ld", &n);
 
  while(n!=0){
      n/=10;   //will remove the last digit
      count++;
  }

  printf("Number of digits: %d", count);
}

Output

Enter an integer: 1233
Number of digits: 4

Explanation

Firstly, we will read the integer from the user and store it in a variable n and initialize a variable count with 0.

Now, we will remove the last digit of the given number stored in n and increment the count.

The last digit of a number can be removed by dividing the number with 10 (n=n/10).

We need to increment the count until n becomes 0. So we will iterate the while loop till n!=0.

Follow us on Facebook, YouTube, Instagram, and Twitter for more exciting content and the latest updates.

The post C Program to Count Number of Digits in an Integer appeared first on CodingTute.

]]>
2903
C Program to Display Alphabets https://codingtute.com/c-program-to-display-alphabets/ Thu, 04 Nov 2021 18:51:21 +0000 https://codingtute.com/?p=2885 In this example, you will learn to display alphabets sequentially using loops and ASCII values. You can find more about ASCII here. C Program to Display Uppercase Alphabets Output C Program to Display Lowercase Alphabets Output Explanation We know each character holds a unique ASCII value. For uppercase alphabets (A-Z) the ASCII value ranges between ... Read more

The post C Program to Display Alphabets appeared first on CodingTute.

]]>
In this example, you will learn to display alphabets sequentially using loops and ASCII values.

You can find more about ASCII here.

C Program to Display Uppercase Alphabets

#include <stdio.h>

int main()
{
    //ASCII values of A - Z (65 - 90) 
    for(int i = 65; i < 91; i++)
        printf("%c ",i);
}

Output

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

C Program to Display Lowercase Alphabets

#include <stdio.h>

int main()
{
    //ASCII values of a - z (97 - 122) 
    for(int i = 97; i < 123; i++)
        printf("%c ",i);
}

Output

a b c d e f g h i j k l m n o p q r s t u v w x y z 

Explanation

We know each character holds a unique ASCII value. For uppercase alphabets (A-Z) the ASCII value ranges between 65-90 respectively and for lowercase alphabets (a-z) the ASCII value ranges between 97-122. In our c program, we loop within the range of alphabets and print the loop variable i with format specifier %c which in terms prints the respective character.

Follow us on Facebook, YouTube, Instagram, and Twitter for more exciting content and the latest updates.

The post C Program to Display Alphabets appeared first on CodingTute.

]]>
2885