Home > Backend Development > C++ > body text

Mastering Loops and Conditional Statements in C Programming

PHPz
Release: 2024-07-17 07:28:09
Original
383 people have browsed it

Mastering Loops and Conditional Statements in C Programming

C Programming – For those who are new to programming, one of the essential languages is C. Since they are the foundation of most programs, understanding loops and conditional statements is essential. This blog post will discuss some standard loop and condition techniques in C programming that all newcomers should be familiar with.

Introduction to Conditional Statements and Loops in C Programming
Certain code blocks can be executed based on conditions thanks to conditional statements. If a condition is true, the if statement assesses it and then runs a block of code. You can check multiple criteria with the else if statement, and it also gives a default action in the event that none of the circumstances are met.

1. Positive number program

#include <stdio.h>

int main() {
 int num = 10;

if (num > 0) {
  printf("Number is positive.\n");
 } else if (num < 0) {
  printf("Number is negative.\n");
 } else {
 printf("Number is zero.\n");
 }
 return 0;
}

Copy after login

(Read more about positive number in c)

2. Reversing a Number

#include <stdio.h>

int RevNum(int num) {
    int R = 0;

    // Reversing the number
    while (num != 0) {
        int remainder = num % 10;
        R = R * 10 + remainder;
        num /= 10;
    }    
    return R;
}
int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    printf("Reversed number: %d\n", RevNum(num));
    return 0;
}

Copy after login

(Read more about Reversing number in c)

3. Armstrong Number

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

// Function to calculate the number of digits in a number
int countDigits(int num) {
    int count = 0;
    while (num != 0) {
        num /= 10;
        ++count;
    }
    return count;
}

// Function to check if a number is an Armstrong number
int isArmstrong(int num) {
    int No, remainder, result = 0, n = 0, power;

    No = num;

    // Count number of digits
    n = countDigits(num);

    // Calculate result
    while (No != 0) {
        remainder = No % 10;

        // Power of remainder with respect to the number of digits
        power = round(pow(remainder, n));
        result += power;
        No /= 10;
    }

    // Check if num is an Armstrong number
    if (result == num)
        return 1; // Armstrong number
    else
        return 0; // Not an Armstrong number
}


int main() {
    int num;

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

    if (isArmstrong(num))
        printf("%d is an Armstrong number  =  ", num);
    else
        printf("%d is not an Armstrong number  =  ", num);

    return 0;
}

Copy after login

(Read more about Armstrong number in c)

4. Palindrome number

#include <stdio.h>
// Function to check if a number is palindrome or not
int P(int num) {
    int i = 0, no = num;
        // Reversing the number
    while (num != 0) {
        int remainder = num % 10;
        i = i * 10 + remainder;
        num /= 10;
    }    
    // Checking if the reversed number is equal to the original number
    if (no == i)
        return 1; // Palindrome no
    else
        return 0; // Not a palindrome
   end if
}
int main() 
{
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    if (P(num))
        printf("%d palindrome no.\n", num);
    else
        printf("%d is not a palindrome no .\n", num);
 end if
    return 0;
}

Copy after login

(Read more about Palindrome number in c)

Conclusion
These programs are crucial for novices to comprehend as they illustrate basic C programming ideas. Effective understanding of these ideas will be aided by practice and experimenting with these examples.

The above is the detailed content of Mastering Loops and Conditional Statements in C Programming. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template