Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
13 views6 pages

Basic C Programs

The document contains 20 basic C programming examples, including simple programs such as printing 'Hello, World!', adding two numbers, and checking for prime numbers. Each example includes the C code and a brief description of its functionality. The examples cover fundamental programming concepts like loops, conditionals, and functions.

Uploaded by

ayushmondalspc
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)
13 views6 pages

Basic C Programs

The document contains 20 basic C programming examples, including simple programs such as printing 'Hello, World!', adding two numbers, and checking for prime numbers. Each example includes the C code and a brief description of its functionality. The examples cover fundamental programming concepts like loops, conditionals, and functions.

Uploaded by

ayushmondalspc
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/ 6

20 Basic C Programming Examples

1. Hello World
#include <stdio.h>

int main() {
printf("Hello, World!");
return 0;
}

2. Add Two Numbers


#include <stdio.h>

int main() {
int a = 5, b = 10;
printf("Sum: %d", a + b);
return 0;
}

3. Find Odd or Even


#include <stdio.h>

int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0)
printf("Even");
else
printf("Odd");
return 0;
}

4. Swap Two Numbers (Using Temp Variable)


#include <stdio.h>

int main() {
int a = 3, b = 7, temp;
temp = a;
a = b;
b = temp;
printf("a = %d, b = %d", a, b);
return 0;
}

5. Factorial of a Number
#include <stdio.h>

int main() {
int n, i;
unsigned long long fact = 1;
printf("Enter number: ");
scanf("%d", &n);
for(i = 1; i <= n; i++)
fact *= i;
printf("Factorial: %llu", fact);
return 0;
}

6. Check Leap Year


#include <stdio.h>

int main() {
int year;
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
printf("Leap year");
else
printf("Not a leap year");
return 0;
}

7. Print Fibonacci Series


#include <stdio.h>

int main() {
int n, a = 0, b = 1, next;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("%d ", a);
next = a + b;
a = b;
b = next;
}
return 0;
}

8. Check Prime Number


#include <stdio.h>

int main() {
int n, i, isPrime = 1;
scanf("%d", &n);
for (i = 2; i <= n/2; i++) {
if (n % i == 0) {
isPrime = 0;
break;
}
}
if (isPrime)
printf("Prime");
else
printf("Not Prime");
return 0;
}

9. Find Largest of Three Numbers


#include <stdio.h>

int main() {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
if (a >= b && a >= c)
printf("Largest: %d", a);
else if (b >= a && b >= c)
printf("Largest: %d", b);
else
printf("Largest: %d", c);
return 0;
}

10. Simple Calculator (if-else)


#include <stdio.h>

int main() {
char op;
int a, b;
scanf(" %c %d %d", &op, &a, &b);
if (op == '+') printf("%d", a + b);
else if (op == '-') printf("%d", a - b);
else if (op == '*') printf("%d", a * b);
else if (op == '/') printf("%d", a / b);
else printf("Invalid operator");
return 0;
}

11. Reverse a Number


#include <stdio.h>

int main() {
int n, rev = 0;
scanf("%d", &n);
while (n != 0) {
rev = rev * 10 + n % 10;
n /= 10;
}
printf("Reversed: %d", rev);
return 0;
}

12. Sum of Digits


#include <stdio.h>

int main() {
int n, sum = 0;
scanf("%d", &n);
while (n != 0) {
sum += n % 10;
n /= 10;
}
printf("Sum: %d", sum);
return 0;
}

13. Palindrome Number


#include <stdio.h>

int main() {
int n, rev = 0, temp;
scanf("%d", &n);
temp = n;
while (temp != 0) {
rev = rev * 10 + temp % 10;
temp /= 10;
}
if (rev == n)
printf("Palindrome");
else
printf("Not Palindrome");
return 0;
}

14. Print ASCII Value of Character


#include <stdio.h>

int main() {
char c;
scanf("%c", &c);
printf("ASCII: %d", c);
return 0;
}

15. Check Vowel or Consonant


#include <stdio.h>

int main() {
char c;
scanf("%c", &c);
if (c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||
c=='A'||c=='E'||c=='I'||c=='O'||c=='U')
printf("Vowel");
else
printf("Consonant");
return 0;
}
16. Print Multiplication Table
#include <stdio.h>

int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= 10; i++)
printf("%d x %d = %d\n", n, i, n * i);
return 0;
}

17. Print Number Pattern (Triangle)


#include <stdio.h>

int main() {
int i, j, n;
scanf("%d", &n);
for (i = 1; i <= n; i++) {
for (j = 1; j <= i; j++)
printf("%d ", j);
printf("\n");
}
return 0;
}

18. Count Number of Digits


#include <stdio.h>

int main() {
int n, count = 0;
scanf("%d", &n);
while (n != 0) {
n /= 10;
count++;
}
printf("Digits: %d", count);
return 0;
}

19. Find Power of a Number


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

int main() {
int base, exp;
scanf("%d %d", &base, &exp);
printf("Result: %.0f", pow(base, exp));
return 0;
}

20. Check Armstrong Number (3-digit)


#include <stdio.h>

int main() {
int n, temp, sum = 0;
scanf("%d", &n);
temp = n;
while (temp != 0) {
int digit = temp % 10;
sum += digit * digit * digit;
temp /= 10;
}
if (sum == n)
printf("Armstrong");
else
printf("Not Armstrong");
return 0;
}

You might also like