0% found this document useful (0 votes)
10 views13 pages

PSIPL2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views13 pages

PSIPL2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Name Vatsal Panchal

UID no. 2024800084

Experiment No. 2

AIM: To apply various control structures to solve given problem statements

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()

int sum=0, num, c=0;

printf("Enter a number:");

scanf("%d", &num);

int temp1=num;

int temp2=num;

while(temp2)

temp2/=10;

c++;

while (num)

sum += pow(num%10, c);

num = num / 10;

printf("%d %d\n",sum ,c);

if (sum == temp1)

printf("It is an Armstrong number.");

else

printf("It is not an Armstrong number.");

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

the power by 1 each time.

FLOWCHART:

PROGRAM: #include<stdio.h>

#include<math.h>
int main()

int num, length=0, sum=0;

printf("Enter a number:");

scanf("%d", &num);

while(num)

sum += num%2 * pow(10, length);

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)

printf("The factorial of %d is 1.",num);

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

8+1=9 and 297 is a Kaprekar number since 297^2=88209 and 88+209=297

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

inputted number, it is a Kaprekar number.


FLOWCHART:

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++;

calc = (sq/(int)pow(10,counter)) + (sq%(int)pow(10,counter));

printf("%d\n", calc);

if (calc == num)

printf("%d is a Kaprekar's Number.", num);

else

printf("%d is not a Kaprekar number.", num);

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()

int mod, num1, num2, temp1, temp2, GCD;

printf("Enter two numbers to get GCD and LCM:");

scanf("%d %d", &num1, &num2);

temp1 = num1;

temp2 = num2;

while(mod)

mod = temp1 % temp2;

temp1 = num2;

GCD = temp2;

temp2 = mod;

int LCM = num1*num2/GCD;

printf("GCD of %d %d is %d and LCM is %d.", num1, num2, GCD, LCM);

return 0;

RESULT:

Program 6

PROBLEM STATEMENT: Write a C program to print the below pattern

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

pattern of increasing numbers, after which the program ends.

FLOWCHART:

PROGRAM: #include<stdio.h>

int main()

int row;

printf("Enter a number: ");

scanf("%d", &row);

for(int i=1; i<=row; i++)

for(int j=1; j<=i; j++)

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

printing, nested for loops, variable swapping, and more.

You might also like