AIM:
To Develop an algorithm using C program to repeatedly rearrange the digits of a four-digit number to
form the largest and smallest possible numbers, then subtract them to reach a fixed point, displaying
each step with its result.
THEORY:
This problem is solved by applying Kaprekar's routine, a unique mathematical sequence. Begin with
any four-digit number where the digits are not all identical. First, rearrange the digits to form the
largest and smallest possible numbers, then subtract the smaller number from the larger to obtain a
new result. Repeat this process with each new result until it reaches a fixed point, typically 6174,
known as Kaprekar's constant. Display the result and step number at each stage to observe the
sequence's convergence. The calculation reveals an intriguing property in number theory, as almost all
initial numbers ultimately converge to 6174, showcasing an inherent mathematical beauty.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Function to sort digits in ascending order
int sortAscending(int number) {
char digits[5];
sprintf(digits, "%04d", number); // Convert number to a 4-character string with leading zeros
for (int i = 0; i < 3; i++) {
for (int j = i + 1; j < 4; j++) {
if (digits[i] > digits[j]) {
char temp = digits[i];
digits[i] = digits[j];
digits[j] = temp;
return atoi(digits); // Convert sorted string back to integer
}
// Function to sort digits in descending order
int sortDescending(int number) {
char digits[5];
sprintf(digits, "%04d", number); // Convert number to a 4-character string with leading zeros
for (int i = 0; i < 3; i++) {
for (int j = i + 1; j < 4; j++) {
if (digits[i] < digits[j]) {
char temp = digits[i];
digits[i] = digits[j];
digits[j] = temp;
return atoi(digits); // Convert sorted string back to integer
// Function to perform Kaprekar's routine and print each step
void kaprekarRoutine(int number) {
int step = 0;
printf("Starting number: %04d\n", number);
// Loop until the number reaches 6174
while (number != 6174) {
int largest = sortDescending(number);
int smallest = sortAscending(number);
number = largest - smallest;
step++;
printf("Step %d: %04d - %04d = %04d\n", step, largest, smallest, number);
// Break if number becomes 0 (special case)
if (number == 0) {
printf("Kaprekar's constant cannot be reached from this number.\n");
return;
printf("Reached Kaprekar's constant: 6174 in %d steps\n", step);
int main() {
int initialNumber;
printf("Enter a four-digit number (not all digits the same): ");
scanf("%d", &initialNumber);
if (initialNumber < 1000 || initialNumber > 9999 ||
initialNumber == 1111 || initialNumber == 2222 ||
initialNumber == 3333 || initialNumber == 4444 ||
initialNumber == 5555 || initialNumber == 6666 ||
initialNumber == 7777 || initialNumber == 8888 ||
initialNumber == 9999) {
printf("Please enter a valid four-digit number where not all digits are the same.\n");
return 1;
kaprekarRoutine(initialNumber);
return 0;
Output:
Program Statement:
Develop an algorithm to check whether the given matrix is orthogonal matrix or not.
Algorithm:
1. Read an square matrix from the user.
2. The transpose of a matrix is found by interchanging its rows into columns or
columns into rows.
3. Multiply the original matrix by its transposed Matrix.
4. Compare the resulting matrix from step 3 to the identity matrix.
5. If the resulting matrix is an identity matrix, the input matrix is orthogonal or not.
Program:
Output1:
Output2:
Aim:
To check, the given number is Kaprekar number.
Program Explanation:
1. countDigits Function:
o This function counts the number of digits in a given number. It uses a loop to divide
the number by 10 until it becomes 0, incrementing a count variable with each
iteration.
2. isKaprekar Function:
o This function checks if a given number is a Kaprekar number.
o It first checks if the number is 1 (since 1 is a Kaprekar number).
o Then, it calculates the square of the number.
o It determines the number of digits in both the original number and its square.
o It splits the square into two parts: the right part (last d digits, where d is the number of
digits in the original number) and the left part (the remaining digits).
o It checks if the sum of these two parts equals the original number. If so, it returns 1
(true), indicating that the number is a Kaprekar number. Otherwise, it returns 0
(false).
3. main Function:
o The main function reads an integer input from the user and checks if it is a Kaprekar
number by calling the isKaprekar function.
o It then prints whether the number is a Kaprekar number based on the result.
Program:
#include <stdio.h>
#include <math.h>
int countDigits(int num) {
int count = 0;
while (num != 0) {
num /= 10;
count++;
}
return count;
}
int isKaprekar(int num) {
if (num == 1) {
return 1;
}
int sq_num = num * num;
int num_digits = countDigits(num);
int sq_num_digits = countDigits(sq_num);
int divisor = pow(10, num_digits);
int right_part = sq_num % divisor;
int left_part = sq_num / divisor;
if (right_part + left_part == num) {
return 1;
}
return 0;
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (isKaprekar(num)) {
printf("%d is a Kaprekar number.\n", num);
} else {
printf("%d is not a Kaprekar number.\n", num);
}
return 0;
}
Output:
AIM: Write a C program, given an integer n, the task is to generate al the left shift numbers possible.
A left shift number is a number that is generated when all the digits of the number are shifted one
position to the left and the digit at the first position is shifted to the last. Develop the algorithm and
write a program in C for the above process and use it to find the largest number in the set.
THEORY:
1. Convert to String: We convert the integer nnn to a string (str) for easy manipulation of digits.
2. Loop through Left Shifts: For each left shift, we rotate the characters in str and store the result in
shifted.
For example, 1234 becomes 2341, 3412, 4123, etc.
3. Convert to Integer: Convert each left-shifted string (shifted) back to an integer to compare and
find the maximum.
4. Update Maximum: Keep track of the largest number encountered during the shifts.
5. Output: Print the largest left-shifted number found.
PROGRAM:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
int n;
printf("Enter a number: ");
scanf("%d", &n);
// Convert the integer to a string to manipulate individual
digits
char str[20];
sprintf(str, "%d", n);
int numDigits = strlen(str);
int maxNumber = n; // Initialize with the original number
// Perform left shift for each position
for (int i = 0; i < numDigits; i++) {
// Rotate the string left by 1 position
char shifted[20];
for (int j = 0; j < numDigits - 1; j++) {
shifted[j] = str[j + 1];
shifted[numDigits - 1] = str[0];
shifted[numDigits] = '\0';
// Convert shifted string back to an integer
int shiftedNum = atoi(shifted);
// Update the maximum number if needed
if (shiftedNum > maxNumber) {
maxNumber = shiftedNum;
// Update str for the next rotation
strcpy(str, shifted);
printf("The largest left shift number is: %d\n", maxNumber);
return 0;
}
SINE SERIES
NAME : HARI PRASHANTH S
REG. NO. : 2024212027
DIVISION : EST (I – SEMESTER)
PROBLEM STATEMENT:
To compute sin (x) for a given value x using Taylor series.
PROGRAM EXPLANATION:
The function sin(x) is evaluated as defined by the infinite series expansion,
sin(x) = x – (x3 / 3!) + (x5 / 5!) – (x7 / 7!) + …
Studying the expression for sin(x) it is observed that powers and factorials following the
sequence [1, 3, 5, 7, …] are required. This sequence can be generated by starting with 1 and
successively adding 2. But other problem is to compute the general term (xi/i!), which can be
expressed as
; for i>=1
By expanding the infinite series, we get following terms;
i=3
i=5
i=7
Each of the terms, , , ,… can be described by general term:
; for i = 3, 5, 7, …
Therefore to generate consecutive terms of the sine series we can use
current ith term = * (previous term)
To get the terms to alternate in sign, repeated execution of [sign = - sign], generates
alternating positive and negative terms. After calculating term, it is added to sum. Sum keeps a
running total of all terms, which approximates sin( ) .
An important consideration here is the desired accuracy we require for sin(x). Because it is
observable that the contribution from higher terms quickly become very small. For example the 4 th
term (i.e. x7/7!) makes a contribution of less than 0.0002. In circumstances like this an efficient
method is to terminate the program if the difference between current term and previous term is less
than 0.0001. This condition is implemented in while loop.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
// Function to compute sine using the Taylor series
double compute_sine(double x)
{
double term = x; // First term in the series (x)
double sum = term; // Initialize sum with the first term
int i=1;
double prev_term = 0, sine, diff;
do
{
// Calculate the next term based on the previous term
i+=2;
prev_term = term;
term = term * -(x*x) / (i*(i-1)); // (x^2)/(i*(i-1))
sum = sum + term;
//difference between current and previous terms
diff = abs(prev_term - term);
} while(diff > 0.0001);
sine = sum;
return sine;
}
int main()
{
double x;
printf("Enter the value of x (in radians): ");
scanf("%lf", &x);
double result = compute_sine(x);
printf("sin(%.4f) = %.4f\n", x, result);
return 0;
}
OUTPUT:
[Link] ZERO ELEMENT OF A LOWER TRIANGULAR MATRIX
AIM
To write a C program for non zero elements of a lower triangular matrix and store them in a one
dimensional array.
THEORY
This C program reads a square matrix of size n x n and stores only the non-zero elements of its lower
triangular part (including the diagonal) in a one-dimensional array A. The function LowerTriangle
iterates through each element of the matrix a, and if the element lies on or below the main diagonal
(i.e., row index i is greater than or equal to column index j), it is stored in A. The main function
calculates the number of non-zero elements nze in the lower triangle using the formula (n×(n+1))/2(n
\times (n + 1)) / 2(n×(n+1))/2, fills the matrix with user input, and then displays the contents of A after
extracting the lower triangular elements.
C PROGRAM
#include <stdio.h>
#include <stdlib.h>
void LowerTriangle(int n, int a[n][n], int A[])
{
int k = 0;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
if(i >= j)
{
A[k] = a[i][j];
k++;
}
}
}
}
int main()
{
int n, nze;
printf("Enter the size of matrix: \n");
scanf("%d",&n);
int a[n][n];
printf("Enter the value of elements of lower triangular matrix:
\n");
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
scanf("%d",&a[i][j]);
}
}
nze = (n*(n+1))/2;
int A[nze];
LowerTriangle(n, a, A);
printf("The non-zero elements of a lower triangular matrix is:
\n");
for(int i = 0; i < nze; i++)
{
printf("%d ",A[i]);
}
return 0;
}
OUTPUT
RESULT
C program has been implemented.
MAGIC SQUARE PROGRAM
Problem statement :
Develop an algorithm to create an odd numbered magic square.
Algorithm :
Begin with placing number '1' in the middle of the top [Link] each subsequent number,
the row position is decreased by 1 and the column position is increased by 1 (moving up and right).If
this moves out of bounds, it wraps [Link] wraps around to the last row if it goes above the
[Link] wraps around to the first column if it goes beyond the last [Link] a calculated position
is already occupied, the position should be adjusted by moving down (increase row position by 1) and
moving left (decrease column position by 2).
Program :
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,r,c,tc,tr;
printf("Enter the size:");
scanf("%d",&n);
int ms[n][n];
for(r=0;r<n;r++)
{
for(c=0;c<n;c++)
{
ms[r][c]=0;
}
}
int row=0;
int col=n/2;
ms[row][col]=1;
for(int num=2;num<n*n+1;num++)
{
tc=col;
tr=row;
row=row-1;
if(row==-1)
{
row=n-1;
}
col=col+1;
if(col==n)
{
col=0;
}
if(ms[row][col]!=0)
{
row=tr+1;
col=tc;
if(row==n)
{
row=0;
}
}
ms[row][col]=num;
}
for(r=0;r<n;r++)
{
for(c=0;c<n;c++)
{
printf("%d ",ms[r][c]);
}
printf("\n");
}
}
Output :
Result:
C program has been implemented .
REGISTER NUMBER:2024212043
NAME: LAVANYA V
EIGHT QUEEN PROBLEM
AIM
To solve the N-Queens problem for an 8x8 chessboard by placing 8 queens on the board such that
no two queens threaten each other
EXPLANATION
This code solves the N-Queens problem, where the goal is to place N queens on an NxN
chessboard such that no two queens threaten each other. It defines `N` as 8, representing an 8x8 board.
The “solveNQ” function initializes the board and calls “solveNQUtil”, which uses backtracking to place
queens column by column. The “isSafe” function checks if placing a queen at a given position is safe,
ensuring no other queens are on the same row, upper diagonal, or lower diagonal. If a solution is found,
“printSolution” displays the board with queens (marked as "Q") and empty spaces as dots ("."),
otherwise it outputs that no solution exists.
PROGRAM
#define N 8
#include <stdbool.h>
#include <stdio.h>
void printSolution(int board[N][N])
{
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if(board[i][j])
printf("Q ");
else
printf(". ");
}
printf("\n");
}
}
bool isSafe(int board[N][N], int row, int col)
{
int i, j;
for (i = 0; i < col; i++)
if (board[row][i])
return false;
for (i = row, j = col; i >= 0 && j >= 0; i--, j--)
if (board[i][j])
return false;
for (i = row, j = col; j >= 0 && i < N; i++, j--)
if (board[i][j])
return false;
return true;
}
bool solveNQUtil(int board[N][N], int col)
{
if (col >= N)
return true;
for (int i = 0; i < N; i++) {
if (isSafe(board, i, col)) {
board[i][col] = 1;
if (solveNQUtil(board, col + 1))
return true;
board[i][col] = 0;
}
}
return false;
}
bool solveNQ()
{
int board[N][N] = { { 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 } };
if (solveNQUtil(board, 0) == false) {
printf("Solution does not exist");
return false;
}
printSolution(board);
return true;
}
int main()
{
solveNQ();
return 0;
}
OUTPUT
Name : T. Deva
Reg No : 2024212034
PROBLEM STATEMENT
Develop an algorithm to read elements in a matrix and check whether the given matrix is
symmetric or not.
ALGORITHM
A Square matrix is said to be Symmetric matrix if the transpose of the matrix is same
as the given matrix.
The transpose of a matrix is found by interchanging its rows into columns or columns
into rows.
The matrix is said to be symmetric if it is equal to its transposed matrix else it is not a
symmetric matrix.
C PROGRAM
OUTPUT 1
OUTPUT 2
RESULT
C program has been implemented.
14. Harmonic Divisor Number
AIM:
To develop a C program to check the given integer is Harmonic Divisor Number.
ALGORITHM:
• A Harmonic Divisor Number is a positive integer number.
• This method is to divide the given n number
• For Example: 6, The number 6 will check in how many tables it will be multiply, so the
numbers are 1,2,3,6
• So, for above number there are 4 numbers so the divisor number is 4
• The process is 1+2+3+6=12, after that 12/4=3. So, there is single number without any
floating value so it is harmonic number
• Not harmonic number example 8, 1,2,4,8 there are 4 numbers 1+2+4+8=15, 15/4=3.75 so
it is not Harmonic Number
CODE:
#include <stdio.h>
int sod(int n) {
int sum_divisors = 0;
for (int i = 1; i <= n; i++) {
if (n % i == 0) {
sum_divisors += i;
}
}
return sum_divisors;
}
int d(int n) {
int divisor_count = 0;
for (int i = 1; i <= n; i++) {
if (n % i == 0) {
divisor_count++;
}
}
return divisor_count;
}
int is_harmonic_divisor(int n) {
int divisor_sum = sod(n);
int divisor_count = d(n);
if ((n * divisor_count) % divisor_sum == 0) {
return 1;
} else {
return 0;
}
}
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (is_harmonic_divisor(n)) {
printf("%d is a harmonic divisor number\n", n);
} else {
printf("%d is NOT a harmonic divisor number\n", n);
}
return 0;
}
OUTPUT:
RESULT:
The Harmonic Divisor Number in C program has been implemented
NAME: Pramoth Raj R
REG NO: 2024212029
PROBLEM STATEMENT:
Develop an algorithm and a C program to determine if a given number is a super perfect number. A
number is considered a super perfect number if the sum of the divisors of the sum of its divisors is
equal to twice the original number.
ALGORITHM:
• A number is said to be a super perfect number if the sum of the divisors of the sum of the divisors of
the number is equal to twice the original number.
• The divisors of the number are found using a for loop inside a function named sumofdivisors, which
returns the sum of all divisors of the number.
• First, find the sum of the divisors of the input number. Then, find the sum of the divisors of this
result. If the final sum is equal to twice the original number, it is a super perfect number; otherwise, it
is not.
C PROGRAM:
#include <stdio.h>
#include <stdlib.h>
int sod(int n)
{
int sum=0;
for(int i=1;i<=n;i++)
{
if(n%i==0)
{
sum=sum+i;
}
}
return sum;
}
int main()
{
int n;
printf("Enter a number : ");
scanf("%d",&n);
int s1=sod(n);
int s2=sod(s1);
if(s2==(2*n))
{
printf("Perfect number");
}
else
{
printf("Not a perfect number");
}
return 0;
}
OUTPUT:
NAME: PRATHAP SHARVESH S
REG NO: 20242121028
Problem Statement:
Develop an algorithm for determining the abundant number and deficient number in
the range of 1 – 100.
Algorithm:
• A number is said to be abundant number if the sum of the proper divisors of a
number is less than the number else it is said to be a deficient number.
• The divisors of the number are found out using a for loop inside a function
named sumofdivisors which returns the sum of the divisors of the number.
• Then if the sum of the divisors is less than the number, it is a defective number
else it is an abundant number
C Program:
#include<stdio.h>
int sumofdivisors (int n)
{
int i,sum=0;
for (i=1;i<n/2;i++)
{
if ( n % i == 0)
{
sum+=i;
}
}
return sum;
}
int main()
{
int number;
for (number=1; number<=100; number++)
{
if ( sumofdivisors (number) > number)
{
printf ("%d is an abundant number\n", number);
}
else
{
printf("%d is a deficient number\n",number);
}
}
}
Output:
NAME: EZHILVENDHAN A
REG NO: 20242121031
Problem Statement:
Develop an algorithm and write C program to find the sum of digits of a number until
the sum is a single digit (586596 →5+8+6+5+9+6=393+9=12→1+2 = 3). A Harsha/Niven
number is divisible by the sum of its digits. Create a function to compute the sum of digits
and keep it in a separate file. Implement the tasks in two different projects using this file.
Algorithm:
• The sum of the digits is calculated using the sumofnumbers function.
• The sumofnumbers function takes the last digit of the number and adds to the
sum variable thereby giving the sum of the individual digits.
• If the number is divisible by the sum of individual digits of that number, then
it is a Harshad number, else it is a Niven Number.
• The single digit sum is calculated similarly until the sum becomes less than 9.
C Program:
MAIN:
#include <stdio.h>
#include <stdlib.h>
int sumofnumbers(int n);
int main()
{
int number,rem,n,e;
int sum=0,sum1;
printf("Enter the number:");
scanf("%d",&number);
n=number;
e=number;
while(number>0||sum>9)
{
if(number==0)
{
number=sum;
sum=0;
}
rem=number%10;
sum=sum+rem;
number=number/10;
}
sum1=0;
sum1=sumofnumbers(n);
printf("Sum of the individual digits of the given number is
%d\n",sum1);
printf("Single digit sum: %d\n",sum);
if(e%sum1==0)
{
printf("%d is a Harshad number " ,n);
}
else
{
printf("%d is a Niven number",n);
}
return 0;
}
MOTHER
int sumofnumbers(int n)
{
int rem,sum=0;
while(n > 0 )
{
rem=n%10;
sum = sum + rem ;
n = n/10;
}
return sum;
}
Output:
Name: Srenath S B
Reg No: 2024212030
Problem Statement:
Design a C program to check if two given numbers are amicable pairs. Two numbers are
amicable if the sum of proper divisors of each equals the other number.
Algorithm:
• Find the proper divisors of each number (excluding the number itself).
• Sum the proper divisors of each number.
• Compare the sums to see if each number’s sum of divisors equals the other number.
C Program:
#include <stdio.h>
int sum_of_divisors(int num) {
int sum = 0;
for (int i = 1; i <= num / 2; i++) {
if (num % i == 0) {
sum += i;
}
}
return sum;
}
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
if (sum_of_divisors(num1) == num2 && sum_of_divisors(num2)
== num1) {
printf("%d and %d are amicable numbers.\n", num1, num2);
} else {
printf("%d and %d are not amicable numbers.\n", num1,
num2);
}
return 0;
}
Output:
PROBLEM STATEMENTS
Lower triangular matrix is a matrix which has non-zero value as elements below the principle
diagonal including the principle diagonal and rest of the elements as zero. Develop an algorithm and
pseudocode. Write a program in C to print or display the lower triangular matrix of a given matrix.
ALGORITHUM
A lower triangular matrix is a square matrix where all the elements above the main
diagonal are zero.
Lower triangular matrices are important in computer science for solving and
representing linear systems.
C PROGRAM
#include <stdio.h>
void printLowerTriangular(int matrix[][10], int n) {
printf("Lower triangular matrix:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j <= i)
printf("%d ", matrix[i][j]);
else
printf("0 ");
}
printf("\n");
}
}
int main() {
int n;
int matrix[10][10];
printf("Enter the size of the matrix (n x n): ");
scanf("%d", &n);
printf("Enter the elements of the matrix:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("Element [%d][%d]: ", i, j);
scanf("%d", &matrix[i][j]);
}
}
printLowerTriangular(matrix, n);
return 0;
}.
OUTPUT