Вы находитесь на странице: 1из 7

2.

Develop a program to solve simple computational problems using arithmetic expressions


and use of each operator leading to simulation of a commercial calculator. (No built-in math
function)
ALGORITHM:

Step 1: Start the program

Step 2: Read two values and an operator- num1, num2 and operator

Step 3: Evaluate option code with case statements

Step 3.1: case ‘+’ result=num1+um2, goto step 4

Step 3.2: case ‘-‘ result=num1-num2, goto step 4

Step 3.3: case ‘*’ result=num1*num2, goto step 4

Step 3.4: case ‘/’ check if the denominator is 0

if true print divide by 0 error

else

result = num1/num2

goto step 7

Step 3.5 : case ‘%’ result =num1%num2

Step4: print the result

Step 5: Stop the program

FLOWCHART:
#include<stdio.h>
#include<conio.h>
void main()
{
float a,b,res;
char op;
printf("Enter an Expression (eg:1+6): ");
scanf("%f %c %f",&a,&op,&b);
switch(op)
{
case '+' : res=a+b;
break;
case '-' : res=a-b;
break;
case '*' : res=a*b;
break;
case '/' : if(b==0)
{
printf("Arthimetic Exception:Cannot Divide a number by 0.");
return; D
}
else
{
res=a/b;
}
break;
case '%' : res= (int)a % (int)b;
}
printf("%g %c %g = %g \n",a,op,b,res);
}

OUTPUT:
3. Develop a program to compute the roots of a quadratic equation by accepting the coefficients.
Print appropriate messages.

ALGORITHM:
#include<stdio.h>
#include<math.h>
void main( )
{
float a,b,c,disc,root1,root2,real,imag;

printf("Enter a,b,c values \n");


scanf("%f%f%f",&a,&b,&c);
disc =b*b - 4*a*c;
if( (a==0) && (b==0) )
{
printf("Invalid co-efficients \n");
}
else if(a==0)
{
printf("Linear equation \n");
root1= - c / b;
printf("Root=%f",root1);
}
else if(disc==0)
{
printf("The roots are Real and Equal \n");
root1= - b / (2*a);
root2= - b / (2*a);
printf("Root1=%f \n Root2=%f \n",root1,root2);
}
else if(disc>0)
{
printf("The roots are Real and distinct \n");
root1=( - b + sqrt(disc) ) / (2*a);
root2=( - b - sqrt(disc)) / (2*a);
printf("Root1=%f \n Root2=%f \n",root1,root2);
}
else
{
printf("The roots are Real and Imaginary \n");
real=- b / (2*a);
imag=sqrt( fabs ( disc ) ) / (2*a);
printf("Root1=%f + i %f \n",real,imag);
printf("Root2=%f - i %f \n",real,imag);
}
}

OUTPUT:
4. Develop a program to find the reverse of a positive integer and check for palindrome or not.
Display appropriate messages.
ALGORITHM:

Step1: [Initialize] Start


[Input the original number]
Read num

Step 2: [Set number num to a variable ’n’,


If ‘num’ value becomes 0, control comes out of the loop. So num’s original value is lost.
So, num value is stored in other variable ‘n’]
n num

Step 3: [Iterate until num is not equal to 0,


Reverse of the number is calculated]
Remainder num mod 10
num num/10
rev rev*10+remainder

Step 4: [Print the reversed number]


print rev

Step 5: [Check if original number and reversed number are same. If it is then, number is a
palindrome. Otherwise, not palindrome]
if(rev=n) then
print “palindrome”
else
print “not a palindrome”
end if

Step 6: [Finished]
End

FLOWCHART:

#include<stdio.h>
void main()
{
long int temp,rev=0,i,num,remainder;
printf("Enter the number \n");
scanf("%ld",&num);
temp=num;
while(num!=0)
{
remainder=num%10;
num=num/10;
rev=rev*10+remainder;
}
printf("The reverse of the number is %ld\n",rev);
if(rev==temp)
printf("%ld is a palindrome \n",temp);
else
printf("%ld is not a palindrome \n",temp);
}

Вам также может понравиться