Monday, January 24, 2011

Managing Input and Output Operations

Reading of real numbers (in both decimal point and exponential notation) is illustrated in program.
#include<stdio.h>
void main()
{
      float x,y;
       double p,q;
       printf("Values of x and y:");
       scanf("%f %e", &x, &y);
       printf("\n");
       printf("x = %f\ny = %f\n\n", x, y);
       printf("Values of p and q:");
       scanf("%lf %lf", &p, &q);
       printf("\np = %lf\nq = %e",p,q);
       printf("\n\np = %.12lf\nq = %.12e", p,q);
       getch();
   }

Managing Input and Output Operations

Various input formatting options for reading integers are experimented in the program.
#include<stdio.h>
void main()
{
int a,b,c,x,y,z;

int p,q,r;
printf("Enter three integer numbers\n");
scanf("%d %*d %d",&a,&b,&c);
printf("%d %d %d \n\n",a,b,c);
printf("Enter two 4-digit numbers\n");
scanf("%2d %4d",&x,&y);
printf("%d %d\n\n", x,y);
printf("Enter two integers\n");
scanf("%d %d", &a,&x);
printf("%d %d \n\n",a,x);
printf("Enter a nine digit number\n");
scanf("%3d %4d %3d",&p,&q,&r);
printf("%d %d %d \n\n",p,q,r);
printf("Enter two three digit numbers\n");
scanf("%d %d",&x,&y);
printf("%d %d",x,y);
getch();
}

Managing Input and Output Operations

A program that reads a character from keyboard and then prints it in reverse case. That is, if the
input is upper case, the output will be lower case and vice versa.
#include<stdio.h>
void main()
{
char alphabet;

printf("Enter an alphabet");
putchar('\n'); /* move to next line */
alphabet = getchar();
if (islower(alphabet))
putchar(toupper(alphabet));
else
putchar(tolower(alphabet));
getch();
}

Managing Input and Output Operations

The program that requests the user to enter a character and displays a message on the screen
telling the user whether the character is an alphabet or digit, or any other special character.
#include<stdio.h>
void main()
{
char character;

printf("Press any key\n");
character = getchar();
if (isalpha(character) > 0)
printf("The character is a letter.");
else
if (isdigit (character) > 0)
printf("The character is a digit.");
else
printf("The character is not alphanumeric.");
getch();
}

Friday, January 21, 2011

Operators and Expressions

The cost of one type of mobile service is Rs. 250 plus Rs. 1.25 for each call made over and above 100
calls. Write a program to read customer codes and calls made and print
the bill for each customer.
#include<stdio.h>
#define ms 250
void main()
{
int custcode,x,n=0;
float call;
printf("Enter Customer code and calls made by him \n");
while(n<=100)
{
scanf("%d %d",&custocode,&x);
call=ms+1.25*(float)x;
n++;
printf("%d %f",custcode,call);
}
getch();
}

Operators and Expressions

Write a program to read three values using scanf statement and print the following result;
a. Sum of the values
b. Average of the three values
c. Largest of the three
d. Smallest of three
#include<stdio.h>
void main()
{
int a,b,c,x;
float ave;
clrscr();
printf("Enter three integer values\n");
scanf("%d %d %d",&a,&b,&c);
x = a+b+c;
ave = (float)x/3;
printf("Sum of these number = %d",x);
prinf("Average of these number = %f",ave);
if(a>b)
{
if(a>c)
printf("Largest number is %d",a);
else
printf("Largest number is %d",c);
}
else
{
if(c>b)
printf("Largest number is %d\n",c);
else
printf("Largest number is %d\n",b);
}
if(a
{
if(a
printf("Smallest number is %d",a);
else
printf("Smallest number is %d",c);
}
else
{
if(c
printf("Smallest number is %d",c);
else
printf("Smallest nubmer is %d",b);
}
getch();
}

Operators and Expressions

Given three values, write a program to read three values from keyboard and print out the largest of
them without using if statement.
#include<stdio.h>
void main()
{
int x,y,x,R;
printf("Enter three integer number\n");
scanf("%d %d %d",&x,&y,&z);
R=(x>y)?((x>z)?x:z):((z>y)?z:y);
printf("Largest number is %d",R);
getch();
}

Operators and Expressions

Write program to print the size of various data types in C.
#include<stdio.h>
void main()
{
int Int;
char Char;
float Float;
double Double;
Int =  sizeof(int);
Char = sizeof(char);
Float = sizeof(float);
Double =  sizeof(double);
printf("Int = %d bytes Char = %d bytes Float = %f bytes Double = %lf bytes",Int,Char,Float,Double);
getch();
}

Operators and Expressions

In inventory management, the Economic Order Quantity for a single item is given by
EOQ = sqrt((2*demand rate * setup costs)/holding cost per item per unit time))
and the optional Time Between Orders
TBO = sqrt((2*setup costs)/(demand rate*holding cost per item per unit time))
Write a program to compute EOQ and TBO, given demand rate (item per unit time), setup costs (per order),
and the holding cost (per item per unit time).
#include<stdio.h>
#include<math.h>
void main()
{
float EOQ, d_r, s_c, h_c, TBO;
printf("Enter Demand rate, Setup costs and Holding costs\n");
scanf("%f %f %f",&d_r,&s_c,&h_c);
EOQ = sqrt((2*d_r*s_c)/h_c);
TBO = sqrt((2*s_c)/(d_r*h_c));
printf("TOQ = %8.2f\nTBO = %8.2f",EOQ,TBO);
getch();
}

Operators and Expressions

The total distance travelled by a vehicle in t seconds is given by
distance = ut + (at2)/2
Where u is the initial velocity (meters per second), a is the acceleration (metres per second 2).
Write a program to evaluate the distance travelled at regular intervals of time, given the values of
u and a. The program should provide the flexibility to the user to select his own time intervals and
repeat the calculations for different values of u and a.
#include<stdio.h>
void main()
{
float u,t,a,distance;
printf("Enter the velocity, accelaration and time intervals\n\n");
scanf("%f %f %f",&u,&a,&t);
distance = u*t + (a*t*t)/2;
printf("Velocity = %5.2fm/s\nAccelaration = %5.2fm/sec2 \nTime interval = %5.2fsec\n",u,a,t);
printf("Distance = %8.2f",distance);
getch();
}

Operators and Expressions

The straight line method of computing the yearly depriciation of the of and item is given by:
Depriciation=(Purchase price - Salvage value)/years of service.
Write a program to determine the salvage value of an item when the purchase
price years of service and annual depriciation are given.

#include<stdio.h>
void main()
{
float depriciation,P-P,S-V,Y-S;
printf("Enter purchase price,years of service and annual depriciation\n\n");
scanf("%f %f %f",&P-P,&Y-S,&depriciation);
S-V=P-P-depriciation*Y-S;
printf("Salvage value =%8.2f",S-V);
getch();
}

Operators and Expressions

Write a program that will obtain the length and width of a rectangle from
the user and compute its area and perimeter.

#include<stdio.h>
void main()
{
float length,width,area,perimeter;
printf("Enter the length and width of a rectangle\n\n");
scanf("%f %f",&length,&width);
area=length*width;
perimeter=2*(length+width);
printf("Area of rectangle=%5.2f\nPerimeter of rectangle=%5.2f",area,perimeter);
getch();
}

Operators and Expressions

Given the value of the variables x,y and z. Write a program to rotate their
their values such that x has the value of y,y has the value of z and z has
the value of x.

#include<stdio.h>
void main()
{
int x,y,z,temp;
printf("Enter the value of z\n");
scanf("%d",&x);
printf("Enter the value of y\n");
scanf("%d",&y);
printf("Enter the value of z\n");
scanf("%d",&z);
temp=x,x=y,z=temp;
printf("X=%d\nY=%d\nZ=%d",x,y,z);
getch();
}

Operators and Expressions

A computer manufacturing company has the following monthly compensation policy to their sales-persons:
Minimum base salary                             : 1500.00
Bonus for every computer sold              : 200.00
Commission on the total monthly sales : 2 percent
Since the prices of computers are changing, the sales of each computer is fixed at the beginning of
every month. Given the base salary,bonus, and commission rate, the inputs necessary to calculate the
gross salary are, the price of each computer and the number sold during the month.
The gross salary is given by the equation:
Gross salary = base salary + (quantity*bonus rate) + (quantity*price)*commission rate.
#include<stdio.h>
#define BASE_SALAR 1500.00

#define BONUS_RATE 200.00
#define COMMISSION 0.02
void main()
{
int quantity;
float gross_salary,price;
float bonus,commission;
printf("Input number sold and price\n");
scanf("%d %f",&quantity,&price);
bonus = BONUS_RATE*quantity;
commission=COMMISSION*quantity*price;
gross_salary=BASE_SALAR+bonus+commission;
printf("\n");
printf("Bonus =%6.2f\n",bonus);
printf("Commission =%6.2f\n",commission);
printf("Gross salary =%6.2f\n",gross_salary);
getch();
}

Operators and Expressions

This program shows using a cast to evaluate the equation.
#include<stdio.h>
void main()
{
float sum;
int n;
sum = 0;
for( n = 1; n <= 10; ++n)
{
sum = sum + 1/(float)n ;
printf("%2d %6.4f\n", n, sum) ;
}
getch();
}

Operators and Expressions

Output of the program shows round-off errors that can occur in computation of floating point numbers.
#include<stdio.h>
void main()
{
float sum, n, term ;

int count = 1 ;
sum = 0 ;
printf("Enter value of n\n") ;
scanf("%f", &n) ;
term = 1.0/n ;
while( count <= n )
{
sum = sum + term ;
count++ ;
}
printf("Sum = %f\n", sum) ;
getch();
}

Operators and Expressions

The program illustrates the use of variables in expressions and their evaluation.
#include<stdio.h>
void main()
{
float a, b, c, x, y, z;

a = 9;
b = 12;
c = 3;
x = a - b / 3 + c * 2 - 1;
y = a - b / (3 + c) * (2 - 1);
z = a -(b / (3 + c) * 2) - 1;
printf("x = %f\n", x);
printf("y = %f\n", y);
printf("z = %f\n", z);
getch();
}

Operators and Expressions

The program emloys different kinds of operators. The results of their evaluation are also shown for comparison.

# include <stdio.h>
void main()
{
int a, b, c, d;
a = 15;
b = 10;
c = ++a - b;
printf("a = %d b = %d c = %d\n",a, b, c);
d = b++ +a;
printf("a = %d b = %d d = %d\n",a, b, d);
printf("a/b = %d\n", a/b);
printf("a%%b = %d\n", a%b);
printf("a *= b = %d\n", a*=b);
printf("%d\n", (c>d) ? 1 : 0);
printf("%d\n", (c
getch();
}


Operators and Expressions

Program which prints a sequence of squares of numbers. Note the use of the shorthand operator *=.
#include<stidio.h>
#define N 100

#define A 2
void main()
{
int a;
a = A;
while( a < N )
{
printf("%d\n", a);
a *= a;
}
getch();
}

Operators and Expressions

The program which shows the use of integer arithmetic to convert a given number of days into months and days.
#include<stdio.h>
void main()
{
int months, days;
printf("Enter days\n");
scanf("%d",&days);
months=days/30;
days=days%30;
printf("Months =%d Days =%d, months,days);
getch();
}

Thursday, January 20, 2011

Constants, Variables and Data Types

The program to converts the given temprature in fahrenheit to celsius using the following conversion formula.
C = (F-32)/1.8
#include<stdio.h>
#define F_LOW 0
#define F_MAX 250
#define STEP 25
void main()
{
typedef float REAL;
REAL fahrenheit, celsius;
fahrenheit = F_LOW;
printf("Fahrenheit Celsius\n\n");
while(fahrenheit<=F_MAX)
{
celsius = (fahrenheit - 32.0)/1.8;
printf("%5.1f  %7.2f\n",fahrenheit,celsius);
fahrenheit = fahrenheit+STEP;
}
getch();
}

Constants, Variables and Data Types

Program to calculate the value of money at the end of each year of investement, assuming an interest rate and prints the year, and the corresponding amount in two columns.Using scanf function.
#include<stdio.h>
void main()
{
int year, period ;

float amount, inrate, value ;
printf("Input amount, interest rate, and period\n\n") ;
scanf ("%f %f %d", &amount, &inrate, &period) ;
printf("\n") ;
year = 1 ;
while( year <= period )
{
value = amount + inrate * amount ;
printf("%2d Rs %8.2f\n", year, value) ;
amount = value ;
year = year + 1 ;
}
getch();
}

Constants, Variables and Data types

The program which illustrates the use of scanf function.
#include<stdio.h>
void main()
{
int number;
printf("Enter an integer number\n");
scanf ("%d", &number);
if ( number < 100 )
printf("Your number is smaller than 100\n\n");
else
printf("Your number contains more than two digits\n");
getch();
}

Constants,Variables and Data Types

Program to show typical declarations, assignments and values stored in various types of variables.
#include<stdio.h>
void main()
{

float x, p ;
double y, q ;
unsigned k ;
int m = 54321 ;
long int n = 1234567890 ;
x = 1.234567890000 ;
y = 9.87654321 ;
k = 54321 ;
p = q = 1.0 ;
printf("m = %d\n", m) ;
printf("n = %ld\n", n) ;
printf("x = %.12lf\n", x) ;
printf("x = %f\n", x) ;
printf("y = %.12lf\n",y) ;
printf("y = %lf\n", y) ;
printf("k = %u p = %f q = %.12lf\n", k, p, q) ;
getch();
}

Overview of C

Given the value of three variables a,b and c, write a program to compute and display the value of x, where
x = a/(b-c);
Execute your program for the following values:
a = 250, b = 85, c = 25
#include<stdio.h>
void main()
{
int x,a,b,c;

a=250,b=85,c=25;
x=a/(b-c);
printf("Answer is %d",x);
getch();
}

Overview of C

Given two integers 20 and 10, write a program that uses a function add() to add these two numbers
and sub() to find the defference of these two numbers and then display the sum and defferences in
the following form:
20 + 10 =30
20 - 10 = 10
#include<stdio.h>
int add(int a,int b);                       /****function declaration****/


int sub(int a,int b);                        /****function declaration****/
void main()
{
int x,y,z;
x=20;
y=10;
z=add(x,y);                                  /****function call****/
printf("%d + %d = %d",x,y,z);
printf("\n");
z=sub(x,y);                                  /****function call****/
printf("%d + %d = %d",x,y,z);
getch();
}


int add(int a,int b)
{
int c;
c=a+b;
return(c);
}


int sub(int a ,int b)
{
int c;
c=a-b;
return(c);
}

Overview of C

Write a program using one print statement to print the pattern of asterisks as shown below:
*
*   *
*   *   *
*   *   *   *
#include<stdio.h>
void main ()
{
printf("*\n*   *\n*   *   *\n*   *   *   *\n");
getch();
}

Overview of C

Write a program that will print you mailing address in the following form:
First line        : Name
Second line    : Door No, Street
Third line       : City, Pin code
#include<stdio.h>
void main ()
{
printf("Name                   : Rajesh Kumar\n");
printf("Door No, Street   : S-94, 5\n");
printf("City, Pin code     : 110044\n");
getch();
}

Overview of C

Program that uses C math library function.
# include <stdio.h>

#include <math.h>
#define PI 3.1416
#define MAX 180
void main ( )
{
int angle;
float x,y;
angle = 0;
printf(" Angle Cos(angle)\n\n");
while(angle <= MAX)
{
x = (PI/MAX)*angle;
y = cos(x);
printf("%15d %13.4f\n", angle, y);
angle = angle + 10;
}
getch();
}

Overview of C

This presents a very simple program that uses a mul() function.
#include< stdio.h >  
int mul (int a, int b);
void main ()
{
int a, b, c;
a = 5;
b = 10;
c = mul (a,b);
printf ("multiplication of %d and %d is %d",a,b,c);
getch();
}
int mul (int x, int y)
{
int p;
p = x*y;
return(p);
}

Overview of C

This program calculates the value of money at end of each yaer of investment,
assuming an interest rate of 11 percent and prints the year, and the corresponding amount,
int two columns.

# include< stdio.h > 
#define PERIOD 10
#define PRINCIPAL 5000.00
void main()
{
int year;
float amount, value, inrate;
amount = PRINCIPAL;
inrate = 0.11;
year = 0;

while(year <= PERIOD)
{
 printf("%2d %8.2f\n",year, amount);
value = amount + inrate * amount;
year = year + 1;
amount = value;
}
getch();
}

Overview of C

Write a program which performs addtion on two numbers and displays the result.
#include< stdio.h > 
void main()
{
int number;
float amount;
number = 100;
amount = 30.75 + 75.35;
printf("%d\n",number);
printf("%5.2f",amount);
getch();
}