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

0% found this document useful (0 votes)
47 views11 pages

CP Imp Programs

The document contains multiple programs demonstrating checks for palindromes and Armstrong numbers, as well as generating Fibonacci sequences and printing patterns. Each program is presented in three variations: without functions, using functions, and using recursion. The source code for each variation is provided, illustrating different programming techniques in C.

Uploaded by

rc.pro.c333
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)
47 views11 pages

CP Imp Programs

The document contains multiple programs demonstrating checks for palindromes and Armstrong numbers, as well as generating Fibonacci sequences and printing patterns. Each program is presented in three variations: without functions, using functions, and using recursion. The source code for each variation is provided, illustrating different programming techniques in C.

Uploaded by

rc.pro.c333
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/ 11

Program 1: Palindrome Check

Without Functions
Logic:
 Take a number as input.

 Reverse the number by extracting digits using modulo and division


operations.
 Compare the original number with the reversed number.
Source Code:
#include <stdio.h>
int main() {
int num, reversed = 0, remainder, original;
printf("Enter a number: ");
scanf("%d", &num);
original = num;
while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}
if (original == reversed)
printf("%d is a palindrome.\n", original);
else
printf("%d is not a palindrome.\n", original);
return 0;
}
Program 1: Palindrome Check
Using Functions
Logic:
 Create a function to reverse a number.
 Use this function to check if the number equals its reverse.

Source Code:
#include <stdio.h>
int reverseNumber(int num) {
int reversed = 0, remainder;
while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}
return reversed;
}
int main() {
int num, reversed;
printf("Enter a number: ");
scanf("%d", &num);
reversed = reverseNumber(num);
if (num == reversed)
printf("%d is a palindrome.\n", num);
else
printf("%d is not a palindrome.\n", num);
return 0;
}
Program 1: Palindrome Check
Using Recursion
Logic:
 Use recursion to reverse the number.
 Compare the reversed number with the original.

Source Code:
#include <stdio.h>
int reverseNumber(int num, int reversed) {
if (num == 0)
return reversed;
return reverseNumber(num / 10, reversed * 10 + num % 10);
}
int main() {
int num, reversed;
printf("Enter a number: ");
scanf("%d", &num);
reversed = reverseNumber(num, 0);
if (num == reversed)
printf("%d is a palindrome.\n", num);
else
printf("%d is not a palindrome.\n", num);
return 0;
}
Program 2: Armstrong Number Check
Without Functions
Logic:
 Calculate the sum of cubes of the digits of the number.
 Check if the sum equals the original number.

Source Code:
#include <stdio.h>
#include <math.h>
int main() {
int num, sum = 0, remainder, original;
printf("Enter a number: ");
scanf("%d", &num);
original = num;
while (num != 0) {
remainder = num % 10;
sum += pow(remainder, 3);
num /= 10;
}
if (original == sum)
printf("%d is an Armstrong number.\n", original);
else
printf("%d is not an Armstrong number.\n", original);
return 0;
}
Program 2: Armstrong Number Check
Using Functions
Logic:
 Create a function to compute the sum of cubes of digits.
 Use this function to check if the number is an Armstrong number.

Source Code:
#include <stdio.h>
#include <math.h>
int isArmstrong(int num) {
int sum = 0, remainder, original = num;
while (num != 0) {
remainder = num % 10;
sum += pow(remainder, 3);
num /= 10;
}
return sum == original;
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (isArmstrong(num))
printf("%d is an Armstrong number.\n", num);
else
printf("%d is not an Armstrong number.\n", num);
return 0;
}
Program 2: Armstrong Number Check
Using Recursion
Logic:
 Use recursion to calculate the sum of cubes of digits.
 Compare the result with the original number.
Source Code:
#include <stdio.h>
#include <math.h>
int armstrongSum(int num, int sum) {
if (num == 0)
return sum;
int remainder = num % 10;
return armstrongSum(num / 10, sum + pow(remainder, 3));
}
int main() {
int num, sum;
printf("Enter a number: ");
scanf("%d", &num);
sum = armstrongSum(num, 0);
if (sum == num)
printf("%d is an Armstrong number.\n", num);
else
printf("%d is not an Armstrong number.\n", num);

return 0;
}
Program 3: Fibonacci Sequence
Without Functions
Logic:
 Use a loop to calculate the Fibonacci series up to n terms.
 Each term is the sum of the two preceding terms.
Source Code:
#include <stdio.h>
int main() {
int n, t1 = 0, t2 = 1, nextTerm;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Sequence: ");
for (int i = 1; i <= n; i++) {
printf("%d ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
return 0;
}
Program 3: Fibonacci Sequence
Using Functions
Logic:
 Create a function to generate and print the Fibonacci sequence.
Source Code:
#include <stdio.h>
void printFibonacci(int n) {
int t1 = 0, t2 = 1, nextTerm;
printf("Fibonacci Sequence: ");
for (int i = 1; i <= n; i++) {
printf("%d ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
}
int main() {
int n;
printf("Enter the number of terms: ");
scanf("%d", &n);
printFibonacci(n);
return 0;
}
Program 3: Fibonacci Sequence
Using Recursion
Logic:
 Use a recursive function to calculate each Fibonacci term.
Source Code:
#include <stdio.h>
int fibonacci(int n) {
if (n <= 1)
return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int n;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Sequence: ");
for (int i = 0; i < n; i++) {
printf("%d ", fibonacci(i));
}
return 0;
}
Program 4: Pattern Printing
Star Pattern (Pyramid)
Logic:
 Use nested loops to print rows and spaces for a pyramid.
Source Code:
#include <stdio.h>
int main() {
int n;
printf("Enter the number of rows: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = i; j < n; j++)
printf(" ");
for (int k = 1; k <= (2 * i - 1); k++)
printf("*");
printf("\n");
}
return 0;
}
Number Pattern (Triangle)
Logic:
 Use nested loops to print increasing numbers in a triangular form.
Source Code:
#include <stdio.h>
int main() {
int n;
printf("Enter the number of rows: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++)
printf("%d ", j);
printf("\n");
}
return 0;
}

You might also like