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

C Programs for Common Algorithms

The document outlines algorithms and C programs for various mathematical operations including miles-to-kilometers conversion, reversing a number, calculating factorial, checking for palindromes, identifying Armstrong numbers, and generating a Fibonacci series. Each section includes a clear algorithm followed by a corresponding C program implementation. The programs demonstrate fundamental programming concepts such as loops, conditionals, and arithmetic operations.

Uploaded by

ssnetworks1024
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 views6 pages

C Programs for Common Algorithms

The document outlines algorithms and C programs for various mathematical operations including miles-to-kilometers conversion, reversing a number, calculating factorial, checking for palindromes, identifying Armstrong numbers, and generating a Fibonacci series. Each section includes a clear algorithm followed by a corresponding C program implementation. The programs demonstrate fundamental programming concepts such as loops, conditionals, and arithmetic operations.

Uploaded by

ssnetworks1024
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

a) Miles-to-Kilometers Conversion Program

Algorithm:

1. Start

2. Read miles

3. kilometers = miles × 1.60934

4. Print kilometers

5. Stop

C Program:

#include

int main() {

float miles, km;

scanf("%f", &miles;);

km = miles * 1.60934;

printf("%.2f", km);

return 0;

--------------------------------------------

b) Reverse of a Number

Algorithm:

1. Start

2. Read number

3. rev = 0

4. While number > 0:

digit = number % 10
rev = rev * 10 + digit

number = number / 10

5. Print rev

6. Stop

C Program:

#include

int main() {

int n, rev=0, d;

scanf("%d", &n;);

while(n>0){

d=n%10;

rev=rev*10+d;

n/=10;

printf("%d", rev);

--------------------------------------------

c) Factorial of a Number

Algorithm:

1. Start

2. Read n

3. fact = 1

4. For i = 1 to n:

fact = fact × i
5. Print fact

6. Stop

C Program:

#include

int main() {

int n; long fact=1;

scanf("%d", &n;);

for(int i=1;i<=n;i++) fact*=i;

printf("%ld", fact);

--------------------------------------------

d) Palindrome Check

Algorithm:

1. Start

2. Read n

3. temp = n

4. Reverse n

5. If temp == rev -> palindrome else not palindrome

C Program:

#include

int main(){

int n,temp,rev=0,d;

scanf("%d",&n;);

temp=n;
while(n>0){

d=n%10;

rev=rev*10+d;

n/=10;

if(rev==temp) printf("Palindrome");

else printf("Not Palindrome");

--------------------------------------------

e) Armstrong Number

Algorithm:

1. Start

2. Read n

3. sum = sum of cubes of digits

4. If sum == n -> Armstrong else not

C Program:

#include

int main(){

int n,temp,d,sum=0;

scanf("%d",&n;);

temp=n;

while(n>0){

d=n%10;

sum+=d*d*d;

n/=10;
}

if(sum==temp) printf("Armstrong");

else printf("Not Armstrong");

--------------------------------------------

f) Fibonacci Series (10 terms)

Algorithm:

1. Start

2. a=0,b=1

3. Print a,b

4. Loop 3 to 10:

c=a+b

Print c

a=b

b=c

C Program:

#include

int main(){

int a=0,b=1,c;

printf("%d %d ",a,b);

for(int i=3;i<=10;i++){

c=a+b;

printf("%d ",c);

a=b;
b=c;

You might also like