0% found this document useful (0 votes)
4 views

BASIC and advance C - PROGRAMS

The document contains a collection of basic C programs that demonstrate fundamental programming concepts. It includes examples such as printing 'Hello, World!', checking for positive or negative numbers, reversing a number using recursion, finding the greatest of three numbers, and more. Each program is presented with its code and a brief description of its functionality.

Uploaded by

thanoojpamanji
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)
4 views

BASIC and advance C - PROGRAMS

The document contains a collection of basic C programs that demonstrate fundamental programming concepts. It includes examples such as printing 'Hello, World!', checking for positive or negative numbers, reversing a number using recursion, finding the greatest of three numbers, and more. Each program is presented with its code and a brief description of its functionality.

Uploaded by

thanoojpamanji
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

BASIC C - PROGRAMS ​

// 1. Hello World Program in C


#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}

c
CopyEdit
// 2. C program to check whether the given number is positive or
negative
#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf(n >= 0 ? "Positive\n" : "Negative\n");
return 0;
}

c
CopyEdit
// 3. Reverse an input number using recursion
#include <stdio.h>
void reverse(int n) {
if (n == 0) return;
printf("%d", n % 10);
reverse(n / 10);
}
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
reverse(n);
return 0;
}

c
CopyEdit
// 4. Program to find greatest of three numbers
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d%d%d", &a, &b, &c);
printf("Greatest: %d\n", (a > b && a > c) ? a : (b > c ? b :
c));
return 0;
}

c
CopyEdit
// 5. C Program to print Fibonacci series in a given range
#include <stdio.h>
void fibonacci(int n) {
int a = 0, b = 1, c;
for (int i = 0; i < n; i++) {
printf("%d ", a);
c = a + b;
a = b;
b = c;
}
}
int main() {
int n;
printf("Enter range: ");
scanf("%d", &n);
fibonacci(n);
return 0;
}

c
CopyEdit
// 6. C Program to find factorial of a given number
#include <stdio.h>
int fact(int n) {
return (n <= 1) ? 1 : n * fact(n - 1);
}
int main() {
int n;
printf("Enter number: ");
scanf("%d", &n);
printf("Factorial: %d\n", fact(n));
return 0;
}

c
CopyEdit
// 7. Find Prime numbers in a given range
#include <stdio.h>
int isPrime(int n) {
if (n < 2) return 0;
for (int i = 2; i * i <= n; i++)
if (n % i == 0) return 0;
return 1;
}
int main() {
int a, b;
printf("Enter range: ");
scanf("%d%d", &a, &b);
for (int i = a; i <= b; i++)
if (isPrime(i)) printf("%d ", i);
return 0;
}

c
CopyEdit
// 8. C Program to check if given number is Armstrong or not
#include <stdio.h>
#include <math.h>
int isArmstrong(int n) {
int sum = 0, temp = n, d = log10(n) + 1;
while (temp) {
sum += pow(temp % 10, d);
temp /= 10;
}
return sum == n;
}
int main() {
int n;
printf("Enter number: ");
scanf("%d", &n);
printf(isArmstrong(n) ? "Armstrong\n" : "Not Armstrong\n");
return 0;
}
c
CopyEdit
// 9. C Program to check if given number is palindrome or not
#include <stdio.h>
int isPalindrome(int n) {
int rev = 0, temp = n;
while (temp) {
rev = rev * 10 + temp % 10;
temp /= 10;
}
return rev == n;
}
int main() {
int n;
printf("Enter number: ");
scanf("%d", &n);
printf(isPalindrome(n) ? "Palindrome\n" : "Not Palindrome\n");
return 0;
}

c
CopyEdit
// 10. C program to display palindrome numbers in a given range
#include <stdio.h>
int isPalindrome(int n) {
int rev = 0, temp = n;
while (temp) {
rev = rev * 10 + temp % 10;
temp /= 10;
}
return rev == n;
}
int main() {
int a, b;
printf("Enter range: ");
scanf("%d%d", &a, &b);
for (int i = a; i <= b; i++)
if (isPalindrome(i)) printf("%d ", i);
return 0;
}
c
CopyEdit
// 11. C Program to check if number is odd or even
#include <stdio.h>
int main() {
int n;
printf("Enter number: ");
scanf("%d", &n);
printf(n % 2 ? "Odd\n" : "Even\n");
return 0;
}

c
CopyEdit
// 12. C Program to find out the ASCII value of a character
#include <stdio.h>
int main() {
char c;
printf("Enter character: ");
scanf("%c", &c);
printf("ASCII: %d\n", c);
return 0;
}

c
CopyEdit
// 13. C Program to find the size of int, float, double and char
#include <stdio.h>
int main() {
printf("int: %lu\nfloat: %lu\ndouble: %lu\nchar: %lu\n",
sizeof(int), sizeof(float), sizeof(double),
sizeof(char));
return 0;
}

c
CopyEdit
// 14. C Program to check whether an alphabet is vowel or consonant
#include <stdio.h>
int main() {
char c;
printf("Enter alphabet: ");
scanf(" %c", &c);
printf(("aeiouAEIOU"[c - 'A' & 31] ? "Consonant\n" :
"Vowel\n"));
return 0;
}

c
CopyEdit
// 15. C Program to check leap year
#include <stdio.h>
int main() {
int y;
printf("Enter year: ");
scanf("%d", &y);
printf((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0) ? "Leap
Year\n" : "Not Leap Year\n");
return 0;
}

c
CopyEdit
// 16. C Program to find sum of first n natural numbers
#include <stdio.h>
int main() {
int n;
printf("Enter n: ");
scanf("%d", &n);
printf("Sum: %d\n", n * (n + 1) / 2);
return 0;
}

You might also like