C Programs
C Programs
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+
answer is 5.000000
answer is 2.000000
answer is 4.500000
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?
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)
{
0 0 0
INVALID INPUTS
0 2 4
LINEAR EQUATION
ROOT=-2.000000
1 2 1
Root1=-
1.000000
Root2=-1.000000
1 4 1
ROOT1=-
0.267949
ROOT2=-3.732051
1 2 4
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
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
Name: REMYA
MeterCharge: 196.000000
Name: REMYA
MeterCharge: 287.000000
Name: REMYA
MeterCharge: 370.000000
Name: REMYA
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?
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
15
90
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
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.