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

C Special Programs

C program

Uploaded by

tgwz98rrzf
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

C Special Programs

C program

Uploaded by

tgwz98rrzf
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

Fibonacci Series up to n terms


The Fibonacci sequence is a sequence where the next term is the sum of
the previous two terms. The first two terms of the Fibonacci sequence are 0
followed by 1.

The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21

#include <stdio.h>
int main() {

int i, n;

// initialize first and second terms


int t1 = 0, t2 = 1;

// initialize the next term (3rd term)


int nextTerm = t1 + t2;

// get no. of terms from user


printf("Enter the number of terms: ");
scanf("%d", &n);

// print the first two terms t1 and t2


printf("Fibonacci Series: %d, %d, ", t1, t2);

// print 3rd to nth terms


for (i = 3; i <= n; ++i) {
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}

return 0;
}

Enter the number of terms: 10


Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,
2. C Program to Check Armstrong Number

A positive integer is called an Armstrong number (of order n ) if

abcd... = an + bn + cn + dn +

In the case of an Armstrong number of 3 digits, the sum of cubes of each


digit is equal to the number itself. For example, 153 is an Armstrong num

153 = 1*1*1 + 5*5*5 + 3*3*3

Check Armstrong Number of three digits


#include <stdio.h>
int main() {
int num, originalNum, remainder, result = 0;
printf("Enter a three-digit integer: ");
scanf("%d", &num);
originalNum = num;

while (originalNum != 0) {
// remainder contains the last digit
remainder = originalNum % 10;

result += remainder * remainder * remainder;

// removing last digit from the orignal number


originalNum /= 10;
}
if (result == num)
printf("%d is an Armstrong number.", num);
else
printf("%d is not an Armstrong number.", num);

return 0;
}

Output

Enter a three-digit integer: 371


371 is an Armstrong number.
3. C Program to Check Whether a Number is
Palindrome or Not

An integer is a palindrome if the reverse of that number is equal to the


original number.

Program to Check Palindrome


#include <stdio.h>
int main() {
int n, reversed = 0, remainder, original;
printf("Enter an integer: ");
scanf("%d", &n);
original = n;

// reversed integer is stored in reversed variable


while (n != 0) {
remainder = n % 10;
reversed = reversed * 10 + remainder;
n /= 10;
}

// palindrome if orignal and reversed are equal


if (original == reversed)
printf("%d is a palindrome.", original);
else
printf("%d is not a palindrome.", original);

return 0;
}

Output

Enter an integer: 1001


1001 is a palindrome.
4. C Program to Find Factorial of a Number

The factorial of a positive number n is given by:

factorial of n (n!) = 1 * 2 * 3 * 4....n

The factorial of a negative number doesn't exist. And, the factorial of 0 is 1.

Factorial of a Number
#include <stdio.h>
int main() {
int n, i;
unsigned long long fact = 1;
printf("Enter an integer: ");
scanf("%d", &n);

// shows error if the user enters a negative integer


if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");
else {
for (i = 1; i <= n; ++i) {
fact *= i;
}
printf("Factorial of %d = %llu", n, fact);
}

return 0;
}

Output

Enter an integer: 10
Factorial of 10 = 3628800

This program takes a positive integer from the user and computes the
factorial using for loop.
5. C Program to Check Whether a Number is Prime
or Not
A prime number is a positive integer that is divisible only by 1 and itself. For
example: 2, 3, 5, 7, 11, 13, 17.

Program to Check Prime Number


#include <stdio.h>
int main() {

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

// 0 and 1 are not prime numbers


// change flag to 1 for non-prime number
if (n == 0 || n == 1)
flag = 1;

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

// if n is divisible by i, then n is not prime


// change flag to 1 for non-prime number
if (n % i == 0) {
flag = 1;
break;
}
}

// flag is 0 for prime numbers


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

Output

Enter a positive integer: 29


29 is a prime number.
#include <stdio.h>

main() {
int n, i, c = 0;
printf("Enter any number n:");
scanf("%d", &n);

//logic
for (i = 1; i <= n; i++) {
if (n % i == 0) {
c++;
}
}

if (c == 2) {
printf("n is a Prime number");
}
else {
printf("n is not a Prime number");
}
return 0;
}
Program Output:

Enter any number n: 7

n is Prime

Explanation:

consider a number n=5


for(i=0;i<=n;i++) /* for loop is executed until the n value equals i */
i.e. for(i=0;i<=5;i++) /* here the for loop is executed until i is equal to n */

1st iteration: i = 1; i <= 5; i++


here i is incremented i.e. i value for next iteration is 2
now if(n%i==0) then c is incremented
i.e.if(5%1==0)then c is incremented, here 5%1=0 thus c is incremented.
now c=1;

2nd iteration: i = 2; i <= 5; i++


here i is incremented i.e. i value for next iteration is 3
now if(n%i==0) then c is incremented
i.e.if(5%2==0) then c is incremented, but 5%2!=0 and so c is not incremented, c
remains 1
c=1;
3rd iteration: i = 3; i <= 5; i++
here i is incremented i.e. i value for next iteration is 4
now if(n%i==0) then c is incremented
i.e.if(5%3==0) then c ic incremented, but 5%3!=0 and so c is not incremented, c
remains 1
c=1;

4th iteration: i = 4; i <= 5; i++


here i is incremented i.e. i value for next iteration is 5
now if(n%i==0) then c is incremented
i.e. if(5%4==0) then c is incremented, but 5%4!=0 and so c is not incremented, c
remains 1
c=1;

5th iteration: i = 5; i <= 5; i++


here i is incremented i.e. i value for next iteration is 6
now if(n%i==0) then c is incremented
i.e. if(5%5==0) then c is incremented, 5%5=0 and so c is incremented.
i.e. c=2

6th iteration: i = 6; i <= 5; i++


here i value is 6 and 6<=5 is false thus the condition fails and control leaves the for
loop.
now if(c==2) then n is a prime number
we have c=2 from the 5th iteration and thus n=5 is a Prime number.

You might also like