Number Programs in C
Number Programs in C
Introduction
Number programs help in building strong logic and problem-solving skills in C programming.
👉 These programs involve mathematical operations and conditions.
Why Learn Number Programs?
- ✔ Improve logical thinking
- ✔ Useful in interviews
- ✔ Practice loops & conditions
- ✔ Strengthen programming basics
1. Check Even or Odd
#include <stdio.h>
int main() {
int num = 10;
if (num % 2 == 0)
printf("Even");
else
printf("Odd");
return 0;
}
Output
Even
2. Find Factorial
#include <stdio.h>
int main() {
int i, n = 5, fact = 1;
for (i = 1; i <= n; i++) {
fact = fact * i;
}
printf("Factorial = %d", fact);
return 0;
}
Output
Factorial = 120
3. Check Prime Number
#include <stdio.h>
int main() {
int n = 7, i, flag = 1;
for (i = 2; i <= n / 2; i++) {
if (n % i == 0) {
flag = 0;
break;
}
}
if (flag == 1)
printf("Prime");
else
printf("Not Prime");
return 0;
}
Output
Prime
4. Reverse a Number
#include <stdio.h>
int main() {
int num = 123, rev = 0;
while (num != 0) {
rev = rev * 10 + num % 10;
num = num / 10;
}
printf("Reverse = %d", rev);
return 0;
}
Output
Reverse = 321
5. Check Palindrome
#include <stdio.h>
int main() {
int num = 121, temp, rev = 0;
temp = num;
while (num != 0) {
rev = rev * 10 + num % 10;
num = num / 10;
}
if (temp == rev)
printf("Palindrome");
else
printf("Not Palindrome");
return 0;
}
Output
Palindrome
6. Sum of Digits
#include <stdio.h>
int main() {
int num = 123, sum = 0;
while (num != 0) {
sum += num % 10;
num /= 10;
}
printf("Sum = %d", sum);
return 0;
}
Output
Sum = 6
Number Programs Table
| Program | Purpose |
|---|---|
| Even/Odd | Check number type |
| Factorial | Multiply sequence |
| Prime | Check prime number |
| Reverse | Reverse digits |
| Palindrome | Check same forward/backward |
Important Notes
- Use loops properly
- Understand number logic
- Practice different problems
Common Mistakes
- ❌ Wrong loop conditions
- ❌ Incorrect variable usage
- ❌ Missing logic steps
Pro Tips
- ✔ Practice daily
- ✔ Break problem into steps
- ✔ Try variations
- ✔ Focus on logic
Conclusion
Number programs are essential for building strong programming logic. They help in understanding conditions and loops deeply.
Practice regularly to improve your coding skills.
👉 This article is part of Dharani Tech Edu Hub — where learning programming is made simple and practical.
Comments
Post a Comment