0% found this document useful (0 votes)
0 views15 pages

C Loops Programs

The document contains a series of C programming code snippets that demonstrate various basic programming concepts. These include printing numbers, calculating sums, finding factorials, reversing numbers, checking for palindromes, and generating Fibonacci series. Each code snippet is self-contained and illustrates a specific task or algorithm.

Uploaded by

ishantmovva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views15 pages

C Loops Programs

The document contains a series of C programming code snippets that demonstrate various basic programming concepts. These include printing numbers, calculating sums, finding factorials, reversing numbers, checking for palindromes, and generating Fibonacci series. Each code snippet is self-contained and illustrates a specific task or algorithm.

Uploaded by

ishantmovva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

1.

Print numbers from 1 to N (for loop)

#include <stdio.h>
int main() {
int i, n;
printf("Enter N: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
printf("%d ", i);
}
return 0;
}
2. Print even numbers from 1 to N (while loop)

#include <stdio.h>
int main() {
int i = 2, n;
printf("Enter N: ");
scanf("%d", &n);
while (i <= n) {
printf("%d ", i);
i += 2;
}
return 0;
}
3. Print odd numbers from 1 to N

#include <stdio.h>
int main() {
int i = 1, n;
printf("Enter N: ");
scanf("%d", &n);
while (i <= n) {
printf("%d ", i);
i += 2;
}
return 0;
}
4. Find sum of first N natural numbers

#include <stdio.h>
int main() {
int n, sum = 0, i;
printf("Enter N: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
sum += i;
}
printf("Sum = %d\n", sum);
return 0;
}
5. Find sum of even numbers up to N

#include <stdio.h>
int main() {
int n, sum = 0, i;
printf("Enter N: ");
scanf("%d", &n);
for (i = 2; i <= n; i += 2) {
sum += i;
}
printf("Sum of even numbers = %d\n", sum);
return 0;
}
6. Find sum of odd numbers up to N

#include <stdio.h>
int main() {
int n, sum = 0, i;
printf("Enter N: ");
scanf("%d", &n);
for (i = 1; i <= n; i += 2) {
sum += i;
}
printf("Sum of odd numbers = %d\n", sum);
return 0;
}
7. Print multiplication table of any number

#include <stdio.h>
int main() {
int n, i;
printf("Enter a number: ");
scanf("%d", &n);
for (i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", n, i, n * i);
}
return 0;
}
8. Print factorial of a number (n!)

#include <stdio.h>
int main() {
int n, i;
unsigned long long factorial = 1;
printf("Enter a number: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
factorial *= i;
}
printf("Factorial of %d = %llu\n", n, factorial);
return 0;
}
9. Find reverse of a number

#include <stdio.h>
int main() {
int n, rev = 0, rem;
printf("Enter a number: ");
scanf("%d", &n);
while (n != 0) {
rem = n % 10;
rev = rev * 10 + rem;
n /= 10;
}
printf("Reversed number = %d\n", rev);
return 0;
}
10. Check if a number is palindrome

#include <stdio.h>
int main() {
int n, original, rev = 0, rem;
printf("Enter a number: ");
scanf("%d", &n);
original = n;
while (n != 0) {
rem = n % 10;
rev = rev * 10 + rem;
n /= 10;
}
if (original == rev)
printf("Palindrome\n");
else
printf("Not Palindrome\n");
return 0;
}
11. Print Fibonacci series up to N terms

#include <stdio.h>
int main() {
int n, t1 = 0, t2 = 1, nextTerm, i;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (i = 1; i <= n; i++) {
printf("%d ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
return 0;
}
12. Print all divisors of a number

#include <stdio.h>
int main() {
int n, i;
printf("Enter a number: ");
scanf("%d", &n);
printf("Divisors of %d are: ", n);
for (i = 1; i <= n; i++) {
if (n % i == 0)
printf("%d ", i);
}
return 0;
}
13. Count digits in a number

#include <stdio.h>
int main() {
int n, count = 0;
printf("Enter a number: ");
scanf("%d", &n);
while (n != 0) {
n /= 10;
count++;
}
printf("Number of digits: %d\n", count);
return 0;
}
14. Find sum of digits of a number

#include <stdio.h>
int main() {
int n, sum = 0, rem;
printf("Enter a number: ");
scanf("%d", &n);
while (n != 0) {
rem = n % 10;
sum += rem;
n /= 10;
}
printf("Sum of digits = %d\n", sum);
return 0;
}
15. Check if a number is Armstrong

#include <stdio.h>
#include <math.h>
int main() {
int n, original, rem, result = 0, digits = 0;
printf("Enter a number: ");
scanf("%d", &n);
original = n;
while (original != 0) {
original /= 10;
digits++;
}
original = n;
while (original != 0) {
rem = original % 10;
result += pow(rem, digits);
original /= 10;
}
if (result == n)
printf("Armstrong Number\n");
else
printf("Not Armstrong\n");
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy