Mastering Loops and Conditional Statements in C Programming
Dive into tutorials, tips, and insights on web development, programming languages, and the latest tech trends. Whether you're a beginner or a seasoned developer, you'll find something to fuel your passion for coding.
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
1. #include <stdio.h>
2.
3. int main() {
4. int num = 10;
5.
6. if (num > 0) {
7. printf("Number is positive.\n");
8. } else if (num < 0) {
9. printf("Number is negative.\n");
10. } else {
11. printf("Number is zero.\n");
12. }
13. return 0;
14. }
Read more about Positive Number
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;
}
Read more about Reverse Number
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;
}
Read more about Armstrong Number
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;
}
Read more about Palindrome Number
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.
Writing efficient and well-structured C programs necessitates a firm grasp of loops and conditional expressions. By mastering these basic techniques, beginners can build a solid foundation for progressively more complicated programming concepts. Try out multiple examples and practice often to strengthen your C programming skills. Enjoy yourself while learning to code!
Read More Article-