Solutions for assignments on using variables and various operators
as01.c
The common requirement for most of the programs is to read a number from
the user and printing a number back to the user. The following program
shows how to read an integer number into integer variable. And also
printing integer as decimal, hex and octal number. Students are expected
to know how to convert a number between decimal, hex and octal.
#include <stdio.h>
int main()
{
int intNum;
printf("Enter an integer number\n");
scanf("%d", &intNum);
printf("Entered number is %d\n", intNum);
printf("Entered number in hex %x\n", intNum);
printf("Entered number in octal %o\n", intNum);
}

as02.c
Main purpose of this assignment is to show that while reading a short
integer, one should use %hd as format specification. However while printing
short integer both %d and %hd works. While reading a short variadble %d
may appear working, but while reading with %d, it thinks variable is integer
and writes the given number into 32 bits, instead of 16 bits. As a result
two bytes after the short variable will get over writtern.
#include <stdio.h>
int main()
{
short shNum;
printf("Enter an short integer number\n");
scanf("%hd", &shNum);
printf("Entered number is %d\n", shNum);
printf("Entered number in hex %x\n", shNum);
printf("Entered number in octal %o\n", shNum);
}

as03.c
In the following program, a character is read into the char variable.
The size of char variable is one byte (i.e. 8 bits). When we read char, the
ASCII code of that char will get stroed in the 8 bit variable. Now we can
print this variable, both as character and as 8 bit integer. By printing as
integer we will know the ASCII code of the character.
#include <stdio.h>
int main()
{
char ch;
printf("Enter a character, by typing a character and carriage return:\n");
scanf("%c", &ch);
printf("Entered character is %c\n", ch);
printf("Entered character in hex %x\n", ch);
printf("Entered character in decimal %d\n", ch);
}

as04.c
The following program shows how to use char variable as an 8 bit small
integer. In fact every char variable is a small integer with 8 bits. But
we normall use char variable to store chararacter that is ASCII code with
7 bits. However note that char variable is a small integer. When we wish to
store numbers in the range of 128 to +127, char (small) integer will do.
#include <stdio.h>
int main()
{
char smallNum;
printf("Enter small, that is 8 bit integer:\n");
scanf("%hhd", &smallNum);
printf("Entered small integer is: %d\n", smallNum);
printf("Entered small integer in hex: %x\n", smallNum);
printf("entered small integer in octal: %o\n", smallNum);
}

as05.c
The following program shows how to use 64 bit integer. Note that your
mobile number may not fit in a 32 bit integer. So you should use 64 bit
integer. The format specification for scanf and printf of 64 bit integer
is %lld.
#include <stdio.h>
int main()
{
long long llNum;
printf("Enter your mobile number:\n");
scanf("%lld", &llNum);
printf("Your mobile number is %lld\n", llNum);
}

as06.c
Understand the use of modulo operator %
#include <stdio.h>
int main()
{
int num1,num2,sum,diff,prod,quot,remain;
printf("\n\nEnter two integer numbers: ");
scanf("%d%d",&num1,&num2);
printf("\n\nEntered numbers are:%d and %d",num1,num2);
sum = num1 + num2;
printf("\n\nSum of the two numbers is:%d",sum);
diff = num1  num2;
printf("\n\nDifference of the two numbers is:%d",diff);
prod = num1 * num2;
printf("\n\nProduct of the two numbers is:%d",prod);
quot = num1 / num2;
printf("\n\nDivision quotient of the two numbers is:%d",quot);
remain= num1 % num2;
printf("\n\nRemainder of the two numbers is:%d\n",remain);
}

as07.c
Understand that, the result of conditional operation also can be stored
in a variable.
#include <stdio.h>
int main()
{
int a,b,result;
printf("Enter the two integer numbers a and b:");
scanf("%d%d",&a,&b);
printf("Entered numbers are: %d and %d\n",a,b);
result = a<b;
printf("Result of a<b is: %d\n", result);
result = a>b;
printf("Result of a>b is: %d\n", result);
result = a<=b;
printf("Result of a<=b is: %d\n", result);
result = a>=b;
printf("Result of a>=b is: %d\n", result);
result = a==b;
printf("Result of a==b is: %d\n", result);
result = a!=b;
printf("Result of a!=b is: %d\n", result);
}

as08.c
To become a good embedded programmer, one should understand bit operators
very well. One should view every integer variable as a string of 32 bits. Or
as a string of 32 zeros and ones. Similarly short variable as 16 zeros and
ones. Once you start looking every int/short/char as bits, then the quesiton
of, what is the value of bit 3 or bit 5 makes sense. Similarly how to make
bit 5 of an integer to 1 or 0 also makes sense. Like this finding the value
of bit, or setting a bit to 0 or 1 are very common operations in embedded
programming.
#include <stdio.h>
int main()
{
int a,b,result;
printf("Enter the two numbers a and b in Hex, No need to enter 0x\n");
scanf("%x%x",&a,&b);
printf("Entered numbers in hex are %x and %x",a,b);
result = a & b;
printf("Result of a & b is: %x\n",result);
result = a  b;
printf("Result of a  b is: %x\n",result);
result = a ^ b;
printf("Result of a ^ b is: %x\n",result);
result = ~a;
printf("Result of ~a is : %x\n",result);
result = a >> 2;
printf("Result of a >> 2 is: %x\n",result);
result = b << 2;;
printf("Result of b << 2 is: %x\n",result);
}

as09.c
Understand the difference between bit operators and logical operators.
#include <stdio.h>
int main()
{
int a,b,result;
printf("\n\nEnter two numbers a and b in hex:");
scanf("%x%x",&a,&b);
printf("\n\nEntered numbers in hex are: %x and %x",a,b);
result = a && b;
printf("\n\nResult of a&&b is: %x",a&&b);
result = a  b;
printf("\n\nResult of ab is: %x",ab);
result = !a;
printf("\n\nResult of !a is: %x",!a);
result = !b;
printf("\n\nResult of !b is: %x",!b);
}

as10.c
Understand how to combine conditional operators with logical operators.
Conditional operations results in a boolean values. Logical operators works
well with boolean values.
#include <stdio.h>
int main()
{
int a,b,c,result;
printf("\n\nEnter three numbers a,b and c:");
scanf("%d%d%d",&a,&b,&c);
printf("\n\nEntered numbers are: %d ,%d and %d",a,b,c);
result = (ab) && (a>c);
printf("\n\nResult of (a>b)&&(a>c) is: %x",result);
result = (a==b) && (a==c);
printf("\n\nResult of (a==b)&&(a==c) is: %x",result);
}

as11.c
So far we have dealt with integer variables of various sizes. Starting
from 8 bit integer to 64 bit integers. Now is the time to use 32 bit
floating point variables.
#include <stdio.h>
int main()
{
float a,b,sum,diff,prod,quot;
printf("\nEnter two float numbers a and b: ");
scanf("%f%f",&a,&b);
printf("Entered float numbers are:%f and %f\n",a,b);
sum= a+b;
printf("Sum of the two numbers is:%f\n",sum);
diff = ab;
printf("Difference of the two numbers is:%f\n",diff);
prod = a*b;
printf("Product of the two numbers is:%f\n",prod);
quot = a/b;
printf("Division quotient of the two numbers is:%f\n",quot);
}

as12.c
Understand the using of double variables, which are 64 bit floats.
#include <stdio.h>
int main()
{
double a,b,sum,diff,prod,quot;
printf("\nEnter two float numbers a and b: ");
scanf("%lf%lf",&a,&b);
printf("Entered float numbers are:%lf and %lf\n",a,b);
sum= a+b;
printf("Sum of the two numbers is:%lf\n",sum);
diff = ab;
printf("Difference of the two numbers is:%lf\n",diff);
prod = a*b;
printf("Product of the two numbers is:%lf\n",prod);
quot = a/b;
printf("Division quotient of the two numbers is:%lf\n",quot);
}

as13.c
Now programs are becoming, bit more practical. In this program we are
converting degrees to radians. This is because, lot of math functions, we are
going to use later, will take angle in radians only. Notice that math.h file
is also included along with stdio.h file. In math.h, the pi value constant
M_PI is defined.
#include <stdio.h>
#include <math.h>
int main()
{
double degrees,radians;
printf("Enter angle in degrees\n");
scanf("%lf",°rees);
radians = (M_PI/180.0) * degrees;
printf("Angle in Degrees: %lf and same angle in Radians: %lf\n",
degrees,radians);
}

as14.c
#include <stdio.h>
#include <math.h>
int main()
{
double angle;
printf("Enter angle in degrees\n");
scanf("%lf",&angle);
angle = (M_PI/180.0) * angle;
printf("Angle in Radians: %lf\n",angle);
}

as15.c
So far we have used only two library functions. These are printf() and
scanf() functions. From now onwards, we are using some mathematical (math)
library functions. The proto types of these math library functions are kept
in math.h file. So we should include the math.h file also. In this program
we are using sin() function, which takes angle in radians and returns the
sine value of given angle. Note that the programs using math libary
functions must be linked with math library, while compiling.
// Compile this program by giving the math library as below
// gcc as15.c lm
// or
// gcc as15.c o as15 lm
#include <stdio.h>
#include <math.h>
int main()
{
double deg,rad,sinVal;
printf("Enter degrees\n");
scanf("%lf", °);
rad = (M_PI/180.0) * deg;
printf("Deg: %lf, Rad: %lf\n", deg,rad);
sinVal = sin(rad);
printf("sine of %lf degrees is %lf\n",deg,sinVal);
}

as16.c
Note the usage of three math library functions sin(), cos() and tan()
#include <stdio.h>
#include <math.h>
int main()
{
double deg,rad,sinVal,cosVal,tanVal;
printf("Enter degrees\n");
scanf("%lf",°);
rad = (M_PI/180.0) * deg;
printf("Deg: %lf, Rad: %lf\n", deg,rad);
sinVal = sin(rad);
printf("sine of %lf degrees is %lf\n",deg,sinVal);
cosVal = cos(rad);
printf("cosine of %lf degrees is %lf\n",deg,cosVal);
tanVal = tan(rad);
printf("tan of %lf degrees is %lf\n",deg,tanVal);
}

as17.c
Note the usage of sin, cos and tan inverse functions asin(), acos() and
atan(). These inverse functions takes the value as input and returns the
angle in radians
#include <stdio.h>
#include <math.h>
int main()
{
double r,d,s,c,t,is,ic,it;
printf("Enter degrees\n");
scanf("%lf",&d);
r = (M_PI/180.0) * d;
printf("Deg: %lf, Rad: %lf\n", d,r);
s = sin(r);
printf("sine of %lf degrees is %lf\n",d,s);
c = cos(r);
printf("cos of %lf degrees is %lf\n",d,c);
t = tan(r);
printf("tan of %lf degrees is %lf\n",d,t);
is = asin(s);
printf("sine inverse of %lf is %lf\n",s,is);
ic = acos(c);
printf("cosine inverse of %lf is %lf\n",c,ic);
it = atan(t);
printf("tan inverse of %lf is %lf\n",t,it);
}

as18.c
Note the usage of square root function
#include <stdio.h>
#include <math.h>
int main()
{
double num,sqroot;
printf("Enter a number:\n");
scanf("%lf",&num);
sqroot = sqrt(num);
printf("number: %lf,and its square root: %lf\n",num,sqroot);
}

as19.c
Note the usage of pow() function. It can be used to calculate the power of
any base number with any given power. Both base and power should be variables
of type double. This function can also be used to compute square root,
cubic root, or root of any order.
#include <stdio.h>
#include <math.h>
int main()
{
double base,power,result;
printf("Enter the base number and power to raise:\n");
scanf("%lf%lf",&base,&power);
result = pow(base,power);
printf("base: %lf, power: %lf, and base raised to power %lf\n",base,power,
result);
}

as20.c
In this program we are going to check that for any angle square root
of sum of sin square plus cos square is 1.
#include <stdio.h>
#include <math.h>
int main()
{
double rad,deg,sinval,cosval,final;
printf("Enter degrees\n");
scanf("%lf",°);
rad = (M_PI/180.0) * deg;
printf("Deg: %lf, Rad: %lf\n", rad,deg);
sinval = sin(rad);
printf("sine of %lf degrees is %lf\n",deg,sinval);
cosval = cos(rad);
printf("cosine of %lf degrees is %lf\n",deg,cosval);
final = sqrt((sinval*sinval)+(cosval*cosval));
printf("square root of sin square plus cos square is %lf\n",final);
}

as21.c
Here we are calculating the volume of sphere when radius is given.
#include <stdio.h>
#include <math.h>
int main()
{
double vol,radius;
printf("Enter the radius of the sphere:\n");
scanf("%lf",&radius);
vol = (4.0/3.0) * M_PI * radius * radius * radius;
printf("Radius : %lf,and Volume of the sphere : %lf\n",radius,vol);
}

as22.c
Here we are calculating the surface area of sphere when radius is given.
#include <stdio.h>
#include <math.h>
int main()
{
double sArea,radius;
printf("Enter the radius of the sphere:\n");
scanf("%lf",&radius);
sArea = (4.0) * M_PI * radius * radius ;
printf("Radius : %lf,and Surface area of the sphere : %lf\n",radius,sArea);
}

as23.c
Here we are calculating the weight of sphere when radius and density
of metal are given.
#include <stdio.h>
#include <math.h>
int main()
{
double vol,density,wt,rad;
printf("Following are the densities of various metals\n");
printf(" Aluminium : 2.712\n");
printf(" Copper : 8.94\n");
printf(" Gold : 19.32\n");
printf(" Iron : 7.85\n");
printf(" Silver : 10.49\n");
printf("Enter the radius of the sphere: ");
scanf("%lf",&rad);
printf("Enter the density of metal: ");
scanf("%lf",&density);
vol = (4.0/3.0) * M_PI * rad * rad * rad;
wt = vol * density ;
printf("Radius of sphere: %lf\n",rad);
printf("Volume of sphere: %lf\n",vol);
printf("Weight of sphere: %lf\n",wt);
}

as24.c
Here we are calculating the compound interest.
#include <stdio.h>
#include <math.h>
int main()
{
double A,p,r,n,t;
printf("\nThe principle or initial amount :");
scanf("%lf",&p);
printf("\nAnnual rate of interest :");
scanf("%lf",&r);
printf("\nNumber of years the amount is deposited :");
scanf("%lf",&n);
printf("\nNumber of times the interest is compounded :");
scanf("%lf",&t);
A = p * ( pow ( 1 + ( r / n ) , ( n * t ) ) ) ;
printf("\n\nAmount of money accumulated after %lf years : %lf\n",n,A);
}

as25.c
Reading a character and printing it as small integer
#include <stdio.h>
int main()
{
char a;
printf("Enter a character:\n");
scanf("%c", &a);
printf("Entered small integer is: %d\n", a);
printf("Entered small integer in hex: %x\n", a);
}

as26.c
Reading a small integer and printing it as character. Understanding
the difference between small integer and character is important. Both
are 8 bit variables and named as type char. The difference lies in our
interpretation. It can be viewed as 8 bit integer number or as a character
holding its ASCII code
#include <stdio.h>
int main()
{
char a;
printf("Enter a small integer:");
scanf("%hhd", &a);
printf("Entered small integer is: %d\n", a);
printf("\nEntered character is : %c\n", a);
}

as27.c
Note that ASCII codes of small and capital letters and also digits are in
the increasing order. ASCII code of letter B is one greater than ASCII code
of letter A. Similarly one less than ASCII code of letter C. Same is valid
for ASCII codes of small letters and digits.
#include <stdio.h>
int main()
{
char ch, prvCh, nextCh;
printf("\nEnter a character:");
scanf("%c", &ch);
prvCh = ch  1 ;
nextCh = ch + 1 ;
printf("Entered character is : %c\n", ch);
printf("Previous character is : %c\n", prvCh);
printf("Next character is : %c\n", nextCh);
}

as28.c
The difference between the ASCII codes of a capital letter and the
corresponding small letter is 32 (or 0x20). ASCII code of small letter is
32 more than ASCII code of corresponding capital letter. So it will be easy
to convert between small letters to capital letters and vice versa.
Another way of looking at the difference between capital and small letters
is in terms of bits. The 5th bit (as 2 power 5 is 32) makes the difference.
If 5th bit of small letter is made to zero, it will become capital letter.
Similary if we set the 5th bit of capital letter, it will become small letter.
Again note that posital value of 5th bit is 32. So setting 5th bit from
0 to 1, increases its value by 32. Similarly clearing 5th bit from 1 to 0, will
decrease the value by 32.
Another most important point to note from this and next assignment is to
setting and clearing of a particular bit. To set a bit we have to OR with
a number in which that particular bit is 1 and all other bits are zeroes.
Similarly to clear a bit, we have to AND with a number in which that
particular bit is zero and all other bits should be ones. Also note how
easy to generate such a number with shift operator and compliment operator
#include <stdio.h>
int main()
{
char ch;
printf("Enter a capital letter : ");
scanf("%c", &ch);
printf("char before setting the 5th bit: %c\n", ch);
ch = ch  0x20; //0x20 means: 0010 0000
//Here we are OR ing with a number in which 5th bit is set
// and all other bits are zeros
//But such number can be produced very easily as (1<<5)
//So above statement can be written as below also
//ch = ch  (1<<5);
printf("char after setting the 5th bit: %c\n", ch);
}

as29.c
#include <stdio.h>
int main()
{
char ch;
printf("Enter a small letter : ");
scanf("%c", &ch);
printf("char before clearing the 5th bit: %c\n", ch);
ch = ch & 0xdf; //0xdf means: 1101 1111
//Here we are ANDing with a number in which 5th bit is zero
// and all other bits are ones
//But such number can be produced very easily as ~(1<<5)
//So above statement can be written as below also
//The above can be done as below
//ch = ch & ~(1<<5);
printf("char after clearing the 5th bit: %c\n", ch);
}

as30.c
The following programs show how to manipulate bits in a number.
This kind of operations are very very important for the embedded programmers.
#include <stdio.h>
int main()
{
short shNum;
short bit0Val;
short bit9Val;
printf("Enter a short type integer in hex:");
scanf("%hx", &shNum);
printf("Entered number is : %x\n", shNum);
bit0Val = (shNum >> 0) & 1;
bit9Val = (shNum >> 9) & 1;
printf("Bit 0 val: %d\n", bit0Val);
printf("Bit 9 val: %d\n", bit9Val);
}

as31.c
#include <stdio.h>
int main()
{
short shNum;
printf("Enter a short type integer in hex:");
scanf("%hx", &shNum);
printf("Entered number is : %x\n", shNum);
//Clearing 5th bit, by bitwise ANDing with a number in which 5th bit is
//zero and all other bits are ones
shNum = shNum & ~(1<<5);
//Setting 12th bit, by bitwise ORing with a number in which 12th bit is
//one and all other bits are zeroes
shNum = shNum  (1<<12);
printf("Number after clearing 5th bit and setting 12th bit %x\n", shNum);
}

as32.c
#include <stdio.h>
int main()
{
short shNum,bitNum;
printf("Enter a short type integer in hex: ");
scanf("%hx", &shNum);
printf("Entered number is : %x\n", shNum);
printf("Enter the bit number to set: ");
scanf("%hd", &bitNum);
//Clearing 5th bit, by bitwise ANDing with a number in which 5th bit is
//zero and all other bits are ones
shNum = shNum & ~(1<<5);
//Set N th bit, by bitwise ORing with a number in which N th bit is
//one and all other bits are zeroes
shNum = shNum  (1< 
as33.c
#include <stdio.h>
int main()
{
short shNum,bitNum;
printf("Enter a short type integer in hex: ");
scanf("%hx", &shNum);
printf("Entered number is : %x\n", shNum);
printf("Enter the bit number to clear: ");
scanf("%hd", &bitNum);
//Clearing N th bit, by bitwise ANDing with a number in which N th bit is
//zero and all other bits are ones
shNum = shNum & ~(1< 
as34.c
#include <stdio.h>
int main()
{
short shNum,bitNum;
short bitVal;
printf("Enter a short type integer in hex: ");
scanf("%hx", &shNum);
printf("Entered number is : %x\n", shNum);
printf("Enter the bit number : ");
scanf("%hd", &bitNum);
//We wish to print the value present in the four bits starting from the
//given bit
bitVal = (shNum >> bitNum) & 0xf;
printf("Value present in 4 bits starting from bit %d is %x\n",bitNum,bitVal);
}

as35.c
This is high school problem of finding height or width when angle is
known.
#include <stdio.h>
#include <math.h>
int main()
{
double a,h,w;
printf("Enter the width of the road: ");
scanf("%lf",&w);
printf("Enter the angle made by the tip of pole in degrees: ");
scanf("%lf",&a);
a = ( M_PI / 180 ) * a ;
h = tan(a) * w ;
printf("Height of the pole is: %lf\n",h);
}

