0% found this document useful (0 votes)
4 views21 pages

C Programs

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

C Programs

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

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

PURPOSE: To simulate a calculator using arithmetic expressions


INPUT: Enter num1 and num2
OUTPUT: Print the result of addition or subtraction or multiplication or division or modulus
Algorithm
Step1: START
Step2: Read the operator
Step3: Read two operands
Step4: If operator is equal to plus
4.1 add operand1 and operand2
4.2 Print Result
Step5: If operator is equal to minus
5.1 subtract operand1 and operand2
5.2 Print Result
Step6: If operator is equal to multiplication
6.1 multiply operand1 and operand2
6.2 Print Result
Step7: If operator is equal to division
7.1 divide operand1 and operand2
7.2 Print Result
Step8: If operator is equal to modulus
8.1 divide operand1 and operand2
8.2 Print Result(remainder)
Step9: if operator is any other symbol
9.1 Print invalid operator
Step10: STOP

FLOWCHART
Program
#include <stdio.h>

int main()
{
int operand1,operand2;
char operator;
float Result;
printf("enter the operator");
scanf("%c",&operator);
printf("enter two operands");
scanf("%d%d",&operand1,&operand2);
switch(operator)
{
case'+': Result=operand1+operand2;
printf("answer is %f",Result);
break;
case'-': Result=operand1-operand2;
printf("answer is %f",Result);
break;
case'*': Result=operand1*operand2;
printf("answer is %f",Result);
break;
case'/': Result=(float)operand1/operand2;
printf("answer is %f",Result);
break;
case'%': Result=operand1%operand2;
printf("answer is %f",Result);
break;
default: printf("Invalid operator");
}

}
Output
enter the operator+

enter two operands2 3

answer is 5.000000

enter the operator-

enter two operands5 3

answer is 2.000000

enter the operator*

enter two operands2 3


answer is 6.000000

enter the operator/

enter two operands9 2

answer is 4.500000

enter the operator%

enter two operands9 2

answer is 1.000000

VIVA QUESTIONS:
1) What is Switch statement?
2) How many cases can you have in switch statement?
3) How switch statement works?
4) What does break in switch statement indicate?
5) What is a case in a switch statement?

Develop a program to compute the roots of a quadratic equation by accepting the


coefficients. Print appropriate messages
Purpose : To find roots of a given quadratic equation.
Input: Coefficients of quadratic equation a, b, c
Output: Root1 and Root2

ALGORITHM
Purpose : To find roots of a given quadratic equation.
Input: Coefficients of quadratic equation a, b, c
Output: Root1 and Root2
Step 1 : START
Step2 : read a, b, c
Step3 : Calculate the determinant
determinant = b*b-4*a*c
Step4 : Check for validity
If a is equal to 0 and b is equal to 0
print “Invalid Inputs”
Step5 : Check for different roots
If a is equal to 0
print “Linear equation”
Root1=-c/b
Print “Root1”
Step6 : Check for real and equal roots
If determinant is equal to 0
print “Roots are real and equal”
Root1= -b/(2*a)
Root2 = -b/(2*a)
Print “Root1 & Root2”
Step7 : Check for real and distinct roots
If determinant is greater than 0
Then print “Roots are real and distinct”
Root1= (-b+ (sqrt (fabs (determinant))))/(2*a)
Root2= (-b-(sqrt (fabs (determinant))))/(2*a)
Print “root1 and root2”
End if
Step8 : Check for imaginary roots
print “Roots are imaginary”
Real=-b/ (2*a)
Imaginary=sqrt (fabs (determinant))/ (2*a)
print “Root1 and R oot2”
Step9 :STOP
#include<stdio.h>
#include<math.h>
int main()
{
float a,b,c;
float root1,root2;
float determinant,real,imaginary;
printf("Enter the Co-efficient of Quadratic Equation\n");
scanf("%f%f%f",&a,&b,&c);
determinant=b*b-4*a*c;
if(a==0 && b==0)
{

printf("INVALID INPUTS\n");
}

else if(a==0)
{

printf("LINEAR EQUATION\n");
root1=-c/b;
printf("ROOT=%f\n",root1);
}
else if(determinant==0)
{

printf("ROOTS ARE REAL AND EQUAL\n");


root1=-b/(2*a);
root2=-b/(2*a);
printf("Root1=%f\n Root2=%f",root1,root2);
}
else if(determinant>0)
{

printf("ROOTS ARE REAL AND DISTINCT\n");


root1=(-b+(sqrt(fabs(determinant))))/(2*a);
root2=(-b-(sqrt(fabs(determinant))))/(2*a);
printf("ROOT1=%f\n ROOT2=%f",root1,root2);
}
else
{

printf("ROOTS ARE IMAGINARY\n");


real=-b/(2*a);
imaginary=sqrt(fabs(determinant))/(2*a);
printf("ROOT1=%f+i%f\n",real,imaginary);
printf("ROOT2=%f-i%f\n",real,imaginary);
}
}
Enter the Coefficient of Quadratic Equation

0 0 0

INVALID INPUTS

Enter the Coefficient of Quadratic Equation

0 2 4

LINEAR EQUATION

ROOT=-2.000000

Enter the Coefficient of Quadratic Equation

1 2 1

ROOTS ARE REAL AND EQUAL

Root1=-
1.000000

Root2=-1.000000

Enter the Co-


efficient of Quadratic Equation

1 4 1

ROOTS ARE REAL AND DISTINCT

ROOT1=-
0.267949
ROOT2=-3.732051

Enter the Coefficient of Quadratic Equation

1 2 4

ROOTS ARE IMAGINARY

ROOT1=-
1.000000+i1.732051

ROOT2=-1.000000-i1.732051

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

ALGORITHM
Purpose : To check the given integer is palindrome or not
Input: Num
Output : Reverse and palindrome or not

STEP 1:START
STEP 2: [Input a number]
read num
STEP 3: Temp=num
STEP 4: Loop while temp not equal to 0
4.1Remainder=temp%10
4.2Temp=temp/10
4.3Reverse=reverse*10+remainder
STEP4:print “reverse number”
STEP 5: [Check number is a palindrome or not]
If Num is equal to reverse
print “Num is palindrome “
Else
print “Num is not palindrome”
STEP 6: STOP
Program

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

}
}
OUTPUT
enter the number1991

reverse of 1991 is 1991

1991 is a palindrome number

enter the number123

reverse of 123 is 321

123 not a palindrome number

Viva Questions:
1. What are Looping control statements?
2. Explain while loop.
3. What is the difference between while and for loops?
4. What are the Entry controlled and Exit controlled loops in C?
5. Write the flowchart for „while‟ loop.

An electricity board charges the following rates for the use of electricity: for the first
200 units 80paise per unit: for the next 100 units 90 paise per unit: beyond 300 units Rs
1 per unit. All users are charged a minimum of Rs. 100 as meter charge. If the total
amount is more than Rs 400, then an additional surcharge of 15% of total amount is
charged. Write a program to read the name of the user, number of units consumed and
print out the charges.
ALGORITHM
PURPOSE: Read The Name Of The User, Number Of Units Consumed And Print
INPUT: name [10], unit
OUTPUT: Print the charges for total number of units consumed
STEP 1:START
STEP 2: [Input a name and units]
read name and unit
STEP 2: Metercharge=100
STEP3: If unit less than or equal to 200
3.1 metercharge= metercharge+(unit*.80)
STEP 4: If unit greater than 200 and unit greater than or equal to 300
4.1metercharge= metercharge+(200*0.80)+((unit-200)*0.90))
STEP 5: If unit is greater than 300
1metercharge= metercharge+(200*0.80)+(100*0.90)+((unit-300)*1))
STEP 6:If metercharge greater than or equal to 400
metercharge=metercharge+(metercharge*0.15);
STEP 7: STOP
#include <stdio.h>
int main()
{
double unit,metercharge=100;
char name[20];
printf("enter the name of the customer");
scanf("%s",name);
printf("enter the unit consumed");
scanf("%lf",&unit);
if(unit<=200)
{
metercharge=metercharge+unit*.8;
}
else if(unit>200&&unit<=300)
{
metercharge=metercharge+((200*.8)+((unit-200)*.9));
}
else
{
metercharge=metercharge+((200*.8)+(100*.9)+((unit-300)*1));
}

if(metercharge>=400)
{
metercharge=metercharge+(metercharge*.15);
}
printf("Name: %s\n Number of unit consumed: %f \n MeterCharge:
%f",name,unit,metercharge);
}

OUTPUT

enter the name of the customerREMYA

enter the unit consumed120

Name: REMYA

Number of unit consumed: 120.000000

MeterCharge: 196.000000

enter the name of the customerREMYA

enter the unit consumed230

Name: REMYA

Number of unit consumed: 230.000000

MeterCharge: 287.000000

enter the name of the customerREMYA

enter the unit consumed320

Name: REMYA

Number of unit consumed: 320.000000

MeterCharge: 370.000000

enter the name of the customerREMYA

enter the unit consumed500

Name: REMYA

Number of unit consumed: 500.000000

MeterCharge: 632.500000
VIVA question
1)What is else-if ladder?
2) What are the possible outputs of if statement?
3) What is conditional branching statement?
4) Write the syntax of if- statement?
5) Write the syntax of else if statement?

Introduce 1D Array manipulation and implement Binary search


6.1 ALGORITHM

PURPOSE: Implement Binary search


INPUT: n, a[50], key

OUTPUT: key is present at the position or key not found


START
STEP 1: Read n
STEP 2: For

i=0 to n
Read a[i]
End ith for loop
STEP 3: Read key
STEP 4:low = 0;
high = n-1;
STEP 5 :until low<=high
5.1:mid= (low+ high)/2;
5.2: If a[mid] is equal to key
5.2.1: Print key is present at the position mid+1
5.2.3:found=1
5.4: If mid is greater than key
5.4.1:high = mid-1;
5.5: else
5.5.1low = mid+1;
STEP6: : If not found
Print key not found
STEP 7:[Finished]
STOP
PROGRAM
#include <stdio.h>
int main()
{
int a[50], key, i, n , low, high,mid, found =0;
printf("\n Enter the number of elements in the array:");
scanf ("%d", &n);
printf (" \n Enter the elements of the array: ");
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
printf("\n Enter the key to be searched: \n" );
scanf ("%d", &key);
low = 0;
high = n-1;
while (low <=high)
{
mid= (low+ high)/2;
if (a[mid] == key)
{
printf("\n %d is present at the position %d", key, mid+1);
found=1;
break;
}
if (a[mid]>key)
high = mid-1;
else
low = mid+1;
}
if(!found)
{
printf(“key not found”);
}
}

OUTPUT

Enter the number of elements in the array:8

Enter the elements of the array: 10 13 15 20 26 38 49 98


Enter the key to be searched:

15

15 is present at the position 3


Enter the number of elements in the array:8

Enter the elements of the array: 12 14 23 27 35 47 67 89

Enter the key to be searched:

90

key not found

Viva Questions:
What is searching?
What are the types of searching?
Explain with an example for binary search.
Explain with an example for linear search.
Which is better searching technique?

Implement using functions to check whether the given number is prime and display
appropriate
messages. (No built-in math function)
7.1 ALGORITHM
Purpose:To check whether a given number is prime or not.
Input:A number
Output:Given Number is prime or not
START
STEP 1: read n
STEP 2: call for function isprime()
2.1:if function return 1
2.2: Print the number is prime
2.3: Print the number is NOT prime.
STEP 3:STOP

STEP1:isprime(m)
STEP2: for i=2 to m/2
2.1: if m% i is equal to 0
2.2 :return 0;
STEP3:RETURN 1:
PROGRAM
#include<stdio.h>
int isprime(int m)
{
int i;
for(i=2;i<=m/2;i++)
{
if(m%i==0)
return 0;
}
return 1;
}

int main()
{
int n;
printf("Enter a +ve integer :");
scanf("%d",&n);
if (isprime(n))
printf("%d is prime number\n",n);
else
printf("%d is not prime number\n",n);
return 0;
}
Output

Enter a +ve integer :9

9 is not prime number


Enter a +ve integer :7

7 is prime number

VIVA QUESTION:
1. What is prime number?
2. What is a function?
3. What are the types of function?
4. Explain the flow of program with example
5. Explain the syntax of for loop.

You might also like