PSIPL2
PSIPL2
Experiment No. 2
Program 1
PROBLEM STATEMENT : Write a program to check whether a given number is an armstrong number or not. For example 371 is 3^3+7^3+1^3=371
ALGORITHM: First, we input a number, then count the number of digits. Next, we take each individual digit, raise it to the power of the total
number of digits, and add the results. If the sum is equal to the input number, then it is an Armstrong number.
FLOWCHART:
PROGRAM: #include<stdio.h>
#include<math.h>
int main()
printf("Enter a number:");
scanf("%d", &num);
int temp1=num;
int temp2=num;
while(temp2)
temp2/=10;
c++;
while (num)
if (sum == temp1)
else
return 0;
RESULT:
Program 2
PROBLEM STATEMENT : Write a program to convert a decimal number to binary or convert a binary number to decimal.
ALGORITHM: First, we will input a decimal number, then take the modulus by 2 and multiply it by 10 raised to the power of the digit's place (if it
is in the units place, multiply by 10 to the power of 0, and so on). Then, divide the number by 10 and repeat the process, increasing
FLOWCHART:
PROGRAM: #include<stdio.h>
#include<math.h>
int main()
printf("Enter a number:");
scanf("%d", &num);
while(num)
num = num/2;
length++;
printf("%d", sum);
return 0;
RESULT:
Program 3
PROBLEM STATEMENT: Write a C program to calculate the factorial of N using a for loop.
ALGORITHM: First, we will input a number, then start a counter from 1, increment it, and multiply it until the inputted number.
FLOWCHART:
PROGRAM: #include <stdio.h>
int main(){
int num,fact=1;
printf("Enter a number:");
scanf("%d",&num);
if(num==0 || num==1)
else
for(int i=1;i<=num;i++)
fact*=i;
printf("Factorial of %d is %d",num,fact);
return 0;
}
RESULT:
Program 4
PROBLEM STATEMENT: Write a program to find out whether a number is kaprekar or not. Consider an n-digit number k. Square it and add the right n digits to the
left n or n-1 digits. If the resultant sum is k, then k is called a Kaprekar number. For example, 9 is a Kaprekar number since 9^2=81 and
The first few are 1, 9, 45, 55, 99, 297, and 703.
ALGORITHM: First, we will take the number and square it, then add the digits of the square. Next, we will separate the number of digits in the
square according to the digits of the inputted number. Then, we will add the two parts of the square, and if the sum is equal to the
PROGRAM: #include<stdio.h>
#include<math.h>
int main()
{
int num, temp, div, counter=0, calc;
printf("Enter a number:");
scanf("%d", &num);
temp = num;
int sq = num*num;
while(temp>0)
temp = temp/10;
counter++;
printf("%d\n", calc);
if (calc == num)
else
return 0;
RESULT:
Program 5
PROBLEM STATEMENT: Take two numbers as input and calculate their LCM and GCD (HCF).
ALGORITHM: First, we will input two numbers and get their GCD using Euclid’s algorithm, and then we will calculate the LCM using the GCD-
LCM formula.
FLOWCHART:
PROGRAM: #include<stdio.h>
int main()
temp1 = num1;
temp2 = num2;
while(mod)
temp1 = num2;
GCD = temp2;
temp2 = mod;
return 0;
RESULT:
Program 6
1
12
123
1234
12345
ALGORITHM: First, we will take input from the user to obtain a number, which will be stored in the variable row. The algorithm then uses an
outer loop to iterate from 1 to row. For each iteration, an inner loop prints numbers from 1 to the current outer loop index i. After
printing the numbers for each row, a newline character is added. This process continues until all rows are printed, generating a
FLOWCHART:
PROGRAM: #include<stdio.h>
int main()
int row;
scanf("%d", &row);
printf("%d",j);
printf("\n");
}
return 0;
RESULT:
CONCLUSION: From this experiment, we learned about complex concepts in C and tried solving some intricate programs. We explored pattern