a) C-Program to determine the use of the character classification functions found in c-
type library. (Page:267)
#include<stdio.h>
#include<ctype.h>
int main()
{
char charIn;
printf("enter the character to be examined\n");
scanf("%c", &charIn);
if(islower(charIn))
printf("You have entered a lower case letter\n");
else if(isupper(charIn))
printf("You have entered a UPPER case letter\n");
else if(isdigit(charIn))
printf("You have entered a digit\n");
else if(ispunct(charIn))
printf("You have entered a Punctuation character\n");
else if(isspace(charIn))
printf("You have entered a Whitespace character\n");
else
printf("You have entered a control character\n");
return 0;
}
Output:
Enter the character to be examined
a
You have entered a lower case letter
enter the character to be examined
,
You have entered a Punctuation character
b) C-Program to read a test score, calculate the grade for the score and print the grade.
(Page: 259)
#include<stdio.h>
int main()
{
int marks,temp;
/*C Program to Find Grade of a Student Using Switch Case*/
printf("\n-----------------------------------");
printf("\nEnter The Marks Between 0 To 100:");
printf("\nEnter The Mark: ");
scanf("%d", &marks);
if(marks>100)
{
/* Marks greater than 100 */
printf("\nDon't Be Smart Enter your Marks Between Limit\n");
}
else
{
temp=marks/10;
switch(temp)
{
case 10 :
case 9 :
/* Marks between 90-100 */
printf("\n Your Grade is: A");
break;
case 8 :
/* Marks between 80-89 */
printf("\n Your Grade is: B" );
break;
case 7 :
/* Marks between 70-79 */
printf("\n Your Grade is: C" );
break;
case 6 :
/* Marks between 60-69 */
printf("\n Your Grade is: D" );
break;
default :
/* Marks less than 40 */
printf("\n You Grade is: F or Fail\n");
}
}
return 0;
}
Output:
-----------------------------------
Enter The Marks Between 0 To 100:
Enter The Mark: 55
You Grade is: F or Fail
-----------------------------------
Enter The Marks Between 0 To 100:
Enter The Mark: 90
Your Grade is: A
c) C-Program to uses a menu to allow the user to add, multiply, subtract and divide two
numbers using switch case. (Page: 277)
#include <stdio.h>
#include<stdlib.h>
/**
* Function declarations for calculator
*/
int getoption(void);
float calc(int option,int num1,int num2);
float add(int num1, int num2);
float sub(int num1, int num2);
float mult(int num1, int num2);
float divi(int num1, int num2);
int main()
{
int option;
int num1, num2;
float result;
option=getoption();
printf("please enter two integer numbers:");
scanf("%d%d",&num1,&num2);
result=calc(option,num1,num2);
printf("In main result is:%4.2f\n",result);
return 0;
}
int getoption(void)
{
int option;
/* Print welcome message */
printf("WELCOME TO SIMPLE CALCULATOR\n");
printf("----------------------------\n");
printf("Enter [number 1] :add\n");
printf("Enter [number 2] :Subtract\n");
printf("Enter [number 3] :multiply\n");
printf("Enter [number 4] :divide\n");
/* Input two number and operator from user */
scanf("%d", &option);
return option;
}
float calc(int option,int num1,int num2)
{
float result;
switch(option)
{
case 1:
result = add(num1, num2);
break;
case 2:
result = sub(num1, num2);
break;
case 3:
result = mult(num1, num2);
break;
case 4:
if(num2==0.0)
{
printf("error");
exit(0);
}
else
{
result = divi(num1, num2);
break;
}
default:
printf("Invalid operator");
}
/* Print the result */
printf("Result = %4.2f", result);
return result;
}
/**
* Function to add two numbers
*/
float add(int num1, int num2)
{
return num1 + num2;
}
/**
* Function to subtract two numbers
*/
float sub(int num1, int num2)
{
return num1 - num2;
}
/**
* Function to multiply two numbers
*/
float mult(int num1, int num2)
{
return num1 * num2;
}
/**
* Function to divide two numbers
*/
float divi(int num1, int num2)
{
return num1 / num2;
}
Output
WELCOME TO SIMPLE CALCULATOR
----------------------------
Enter [number 1] :add
Enter [number 2] :Subtract
Enter [number 3] :multiply
Enter [number 4] :divide
1
please enter two integer numbers:2
3
Result = 5.00
In main result is:5.00
d) C-Program to read the name of the user, number of units consumed and print out the
charges. An electricity board charges the following rates for the use of electricity:
o For the first 200 units 80 paise per unit
o For the next 100 units 90 paise per unit
o 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.
#include<stdio.h>
#include<string.h>
void main()
{
int cust_no, unit_con;
float charge,surcharge=0, amt, total_amt;
char nm[25];
printf("Enter the customer IDNO :\t");
scanf("%d",&cust_no);
printf("Enter the customer Name :\t");
scanf("%s",nm);
printf("Enter the unit consumed by customer :\t");
scanf("%d",&unit_con);
if (unit_con <200 )
charge = 0.80;
else if (unit_con>=200 && unit_con<300)
charge = 0.90;
else
charge = 1.00;
amt = unit_con*charge;
if (amt>400)
surcharge = amt*15/100.0;
total_amt = amt+surcharge;
printf("\t\t\t\nElectricity Bill\n\n");
printf("Customer IDNO :\t%d",cust_no);
printf("\nCustomer Name :\t%s",nm);
printf("\nunit Consumed :\t%d",unit_con);
printf("\nAmount Charges @Rs. %4.2f per unit :\t%0.2f",charge,amt);
printf("\nSurchage Amount :\t%.2f",surcharge);
printf("\nMinimum meter charge Rs :\t%d",100);
printf("\nNet Amount Paid By the Customer :\t%.2f",total_amt+100);
}
Output
Enter the customer IDNO : 123
Enter the customer Name : pallavi
Enter the unit consumed by customer : 150
Electricity Bill
Customer IDNO : 123
Customer Name : pallavi
unit Consumed : 150
Amount Charges @Rs. 0.80 per unit : 120.00
Surchage Amount : 0.00
Minimum meter charge Rs : 100
Net Amount Paid By the Customer : 220.00