0% found this document useful (0 votes)
3 views7 pages

Basic C Programs

The document contains a collection of basic C programs demonstrating various algorithms and concepts such as generating Fibonacci series (both iteratively and recursively), checking for prime numbers, identifying palindrome numbers, calculating factorials (using loops and recursion), determining Armstrong numbers, summing digits, reversing numbers, checking odd or even numbers, finding LCM and HCF, and printing patterns like stars and pyramids. Each program includes code snippets and prompts for user input. The document serves as a practical guide for beginners to understand fundamental programming techniques in C.

Uploaded by

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

Basic C Programs

The document contains a collection of basic C programs demonstrating various algorithms and concepts such as generating Fibonacci series (both iteratively and recursively), checking for prime numbers, identifying palindrome numbers, calculating factorials (using loops and recursion), determining Armstrong numbers, summing digits, reversing numbers, checking odd or even numbers, finding LCM and HCF, and printing patterns like stars and pyramids. Each program includes code snippets and prompts for user input. The document serves as a practical guide for beginners to understand fundamental programming techniques in C.

Uploaded by

aravindhkumar311
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

basic c programs:

1)FIBONACCI SERIES:
-----------------
FIBONACCI SERIES WITHOUT RECURSION:

#include<stdio.h>
int main()
{
int n1=0,n2=1,n3,i,number;
printf("Enter the number of elements:");
scanf("%d",&number);
printf("\n%d %d",n1,n2);//printing 0 and 1
for(i=2;i<number;++i)//loop starts from 2 because 0 and 1 are already printed
{
n3=n1+n2;
printf(" %d",n3);
n1=n2;
n2=n3;
}
return 0;
}

FIBONACCI SERIES WITH RECURSION:

#include<stdio.h>
void printFibonacci(int n){
static int n1=0,n2=1,n3;
if(n>0){
n3 = n1 + n2;
n1 = n2;
n2 = n3;
printf("%d ",n3);
printFibonacci(n-1);
}
}
int main(){
int n;
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("Fibonacci Series: ");
printf("%d %d ",0,1);
printFibonacci(n-2);//n-2 because 2 numbers are already printed
return 0;
}

2)PRIME NUMBERS:
--------------
#include <stdio.h>

int main() {

int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);

if (n == 0 || n == 1)
flag = 1;

for (i = 2; i <= n / 2; ++i) {

if (n % i == 0) {
flag = 1;
break;
}
}
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);

return 0;
}

PRIME NUMBERS IN CERTAIN RANGE:

#include <stdio.h>
int main()
{
int a, b, i, j, flag;

printf("Enter lower bound of the interval: ");


scanf("%d", &a);
printf("Enter upper bound of the interval: ");
scanf("%d", &b);
printf("Prime numbers between %d and %d are: ",
a, b);
for (i = a; i <= b; i++)
{
if (i == 1 || i == 0)
continue;

flag = 1;

for (j = 2; j <= i / 2; ++j)


{
if (i % j == 0)
{
flag = 0;
break;
}
}
if (flag == 1)
printf("%d ", i);
}
return 0;
}

3)PALINDROME NUMBER:
-------------------
#include<stdio.h>
int main()
{
int n,r,sum=0,temp;
printf("enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
printf("palindrome number ");
else
printf("not palindrome");
return 0;
}

4)FACTORIAL NUMBER:
-----------------
FACTORIAL NUMBER USING LOOP:
#include<stdio.h>
int main()
{
int i,fact=1,number;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=number;i++){
fact=fact*i;
}
printf("Factorial of %d is: %d",number,fact);
return 0;
}

FACTORIAL NUMBER USING RECURSION:

#include<stdio.h>

long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
void main()
{
int number;
long fact;
printf("Enter a number: ");
scanf("%d", &number);

fact = factorial(number);
printf("Factorial of %d is %ld\n", number, fact);
return 0;
}

5)ARMSTRONG NUMBERS:
------------------
#include<stdio.h>
int main()
{
int n,r,sum=0,temp;
printf("enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
printf("armstrong number ");
else
printf("not armstrong number");
return 0;
}

6)SUM OF DIGITS :
--------------
#include<stdio.h>
int main()
{
int n,sum=0,m;
printf("Enter a number:");
scanf("%d",&n);
while(n>0)
{
m=n%10;
sum=sum+m;
n=n/10;
}
printf("Sum is=%d",sum);
return 0;
}
7)REVERSE NUMBERS:
-------------------
#include<stdio.h>
int main()
{
int n, reverse=0, rem;
printf("Enter a number: ");
scanf("%d", &n);
while(n!=0)
{
rem=n%10;
reverse=reverse*10+rem;
n/=10;
}
printf("Reversed Number: %d",reverse);
return 0;
}

ODD OR EVEN NUMBERS:

#include <stdio.h>

int main() {
int number;

printf("Enter a number: ");


scanf("%d", &number);

if (number % 2 == 0) {
printf("%d is an even number.\n", number);
} else {
printf("%d is an odd number.\n", number);
}

return 0;
}

ODD OR EVEN NUMBERS WITH RANGE:

#include<stdio.h>
int main()
{
int lower, upper, i, count=0;
printf("Enter the lower limit: ");
scanf("%d", &lower);
printf("\nEnter the upper limit: ");
scanf("%d", &upper);
for(i = lower; i <= upper; i++)
{
if(i % 2 != 0)
{
count++;
}
}
printf("Number of odd numbers: %d", count);
printf("\nNumber of even numbers: %d", upper - lower + 1 - count);
}

LCM ,HCF OF AN NUMBER:

LCM OF NUMBER:

#include <stdio.h>

int main() {

int n1, n2, max, lcm;

printf("Enter two positive integers: ");


scanf("%d %d", &n1, &n2);
max = (n1 > n2) ? n1 : n2;

lcm = max;

while ((lcm % n1 != 0) || (lcm % n2 != 0)) {


lcm += max;
}

printf("The LCM of %d and %d is %d.", n1, n2, lcm);

return 0;
}

HCF OF AN NUMBER:

#include <stdio.h>
int main() {
int num1, num2, x, y, temp, gcd, lcm;
printf("Enter two integers
");
scanf("%d%d", &x, &y);
num1 = x;
num2 = y;
while (num2 != 0) {
temp = num2;
num2 = num1 % num2;
num1 = temp;
}
gcd = num1;
lcm = (x*y)/gcd;
printf("GCD of %d and %d = %d", x, y, gcd);
printf("LCM of %d and %d = %d", x, y, lcm);
return 0;
}

PATTERN NUMBERS:

#include <stdio.h>
int main()
{
int i, j, r;
printf("Input the number of rows: ");
scanf("%d", &r);
for (i = 1; i <= r; i++)
{
for (j = 1; j <= i; j++)
{
printf("* ");
}
printf("\n");
}
return 0;
}

FULL PYRAMID:

#include <stdio.h>
int main()
{
int i, space, r, k = 0;
printf("Enter the number of rows: ");
scanf("%d", &r);
for (i = 1; i <= r; ++i, k = 0)
{
for (space = 1; space <= r - i; ++space)
{
printf(" ");
}
while (k != 2 * i - 1)
{
printf("* ");
++k;
}
printf("\n");
}
return 0;
}

You might also like