Aim: Write A Program To Calculate Power of A Number: Practical No. 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Practical No.

Aim : Write a program to calculate power of a number

Algorithum :

Step 1: Declare int and long variables.

Step 2: Enter base value through console.

Step 3: Enter exponent value through console.

Step 4: While loop.

Exponent !=0

i. Value *=base

ii. –exponent

Step 5: Print the result.


Flowchart :
Start

Declare n and count

as int

No
if(n&n-1==0

and n!=0)

Yes

Count=log(n)/log(2) ;

Print(count)

End
Program :

#include<stdio.h>

int main(){

int base, exponent;

long value = 1;

printf("Enter a base value:\n ");

scanf("%d", &base);

printf("Enter an exponent value: ");

scanf("%d", &exponent);

while (exponent != 0){

value *= base;

--exponent;

printf("result = %ld", value);

return 0;

Output :
Practical No.2

Aim : Write a program to check whether the number is armstrong or not.

Algorithum :

Step 1: Start

Step 2: Declare Variable sum, temp, num

Step 3: Read num from User

Step 4: Initialize Variable sum=0 and temp=num

Step 5: Repeat Until num>=0

5.1 sum=sum + cube of last digit i.e [(num%10)*(num%10)*(num%10)]

5.2 num=num/10

Step 6: IF sum==temp

Print "Armstrong Number"

ELSE

Print "Not Armstrong Number"

Step 7: Stop
Flowchart : Start

Assign sum=0

Read a number , num

Assigm temp = num

Whether temp=0

Reached ?

Compute digit = temp% 10

Temp = temp/10

Sum = sum + (digit***3 )

if num ==sum

Print num is not armstrong

Print num is armstrong

Stop
Program :

#include <math.h>

#include <stdio.h>

int main() {

int num, originalNum, remainder, n = 0;

float result = 0.0;

printf("Enter an integer: ");

scanf("%d", &num);

originalNum = num;

// store the number of digits of num in n

for (originalNum = num; originalNum != 0; ++n) {

originalNum /= 10; }

for (originalNum = num; originalNum != 0; originalNum /= 10) {

remainder = originalNum % 10;

// store the sum of the power of individual digits in result

result += pow(remainder, n); }

// if num is equal to result, the number is an Armstrong number

if ((int)result == num)

printf("%d is an Armstrong number.", num);

else

printf("%d is not an Armstrong number.", num);

return 0;

}
Output :
Practical No. 3

Aim : Write a Program to convert binary into decimal and vice versa.

Algorithum :

Step 1: Start

Step 2: Read the binary number from the user, say ‘n’

Step 3: Initialize the decimal number, d=0

Step 4: Initialize i=0

Step 5: Repeat while n != 0:

Extract the last digit by: remainder = n % 10

n = n/10

d = d + (remainder * 2<sup>i</sup>)

Increment i by 1

Step 6: Display the decimal number, d

Step 7: Stop
Flowchart : Start

Read the number from the user : n

Initialize d=0 , i=0

d = d + (remainder *2 ^i)

n = n/10

Remainder = n % 10

No
Is n=0 ?

Yes

Display d

Stop
Program :

#include <math.h>

#include <stdio.h>

int convert(long long n);

int main() {

long long n;

printf("Enter a binary number: ");

scanf("%lld", &n);

printf("%lld in binary = %d in decimal", n, convert(n));

return 0;

int convert(long long n) {

int dec = 0, i = 0, rem;

while (n != 0) {

rem = n % 10;

n /= 10;

dec += rem * pow(2, i);

++i;

return dec;

}
Output :
Practical No. 4

Aim : Write a program to make suitable calculator using suitable case .

Algorithum:

Step 1 : Begin

Step 2 : Print enter your choice

Step 3 : enter your choice

Step 4 : Enter two operands for operation .

Step 5 : user will enter +,-,*,/ .

Step 6 : switch (operator)

Step 7 : do the operation

Step 8 : print the result

Step 9 : EXIT.
Flow chart : Yes
Start Case1 Add()

No

Read a
Yes Substract ()
Case2
Read b

No

Display Calculator options Yes


Multiply ()
Case3

No
Read Choices
Yes Divide ()
Case4

Switch (choice) No
Yes
Case5 Mod()

No

Yes
End Default

Program :

#include <stdio.h>

int main() {

char op;

double first, second;


printf("Enter an operator (+, -, *, /): ");

scanf("%c", &op);

printf("Enter two operands: ");

scanf("%lf %lf", &first, &second);

switch (op) {

case '+':

printf("%.1lf + %.1lf = %.1lf", first, second, first + second);

break;

case '-':

printf("%.1lf - %.1lf = %.1lf", first, second, first - second);

break;

case '*':

printf("%.1lf * %.1lf = %.1lf", first, second, first * second);

break;

case '/':

printf("%.1lf / %.1lf = %.1lf", first, second, first / second);

break;

// operator doesn't match any case constant

default:

printf("Error! operator is not correct");

return 0;

}
Output :

You might also like