Post on 03-Jan-2016
description
Exercise 1Solving problems such as temperature conversion, student grading, income tax calculation, etc which expose the students to use basic C operators.a) Temperature Conversion from Fahrenheit to Celsius.
Program
#include
#includevoid main()
{
float f,c;
clrscr();
printf("\n Enter Temperature in F: ");
scanf("%f",&f);
c=5.0/9*(f-32);
printf(" Temperature in C: %f",c);
getch();
}
Output Enter Temperature in F: 98
Temperature in C: 36.666668
b) Student grading based on his percentage.
Program
#include
#includevoid main()
{
int p;
clrscr();
printf("\n Enter Percentage: ");
scanf("%d",&p);
if(p>=70)
printf(" Distinction");
else if(p>=60&&p=50&&p=40&&pb)
{
if(a>c)
printf(" a is large");
else
printf(" c is large");
}
else
if(b>c)
printf(" b is large");
else
printf(" c is large");
getch();
}Output Enter a,b,c: 5 9 3
b is large e) Income Tax calculation
0
2000000%
20000030000010%
30000050000020%
Above 500000
30%
Program
#include
#includevoid main()
{
long int a,tax=0;
clrscr();
printf("\n Enter amount: ");
scanf("%ld",&a);
if(a200000&&a300000&&a500000)
tax=50000+(a-500000)*30/100;
printf(" Tax: %ld",tax);
getch();
}
Output
Enter amount: 600000
Tax: 80000
Exercise 2
2s complement of a number is obtained by scanning it from right to left and complementing all the bits after the first appearance of a 1. Thus 2s complement of 11100 is 00100. Write a C program to find the 2s complement of a binary number.
Program#include
#include#include
#include
void main()
{
char a[20];
int i,j,k,len;
clrscr();
printf("\n Enter Binary string: ");
gets(a);
len=strlen(a);
for(k=0;a[k]!='\0';k++)
{
if(a[k]!='0'&&a[k]!='1')
{
printf(" Incorrect Binary number...");
getch();
exit(0);
}
}
for(i=len-1;a[i]!='1';i--);
for(j=i-1;j>=0;j--)
{
if(a[j]=='1')
a[j]='0';
else
a[j]='1';
}
printf(" 2's complement: %s",a);
getch();
}
Output
Enter Binary string: 110101010
2's complement: 001010110
Exercise 3a) Write a C Program to find the sum of individual digits of a positive integer
Program
#include
#include
void main()
{
int n,sum=0;
clrscr();
printf("\n Enter n: ");
scanf("%d",&n);
if(n