Wednesday 21 December 2011


Volume of a box




To accept the length,breadth and height of a rectangular box and to display its volume
#include<stdio.h>
Void main();
{
float length,breadth,height,volume;
clrscr();
printf(“enter the dimension of a box\n”);
printf(“length ?”);
scanf(“%f”,&length);
printf(“breadth ?”);
scanf(“%f”,&breadth);
printf(“height ?”);
scanf(“%f”,&height);




TO SUM THE SINE SERIES




#include<stdio.h>
#include<math.h>
Void main()
{
Int i=3;
Float x,tempx;
Double fact=1.0,sum=0.0,term,tempter;
Printf(“enter the angle in degree:”);
Scanf(“%d”,&x);
Tempx=x;
X=x*3.14/180.0;
Term=tempter=x;
Printf(“the terms are:\n”);
For(;fabs(term)>0.0001;i+=2)
{
Printf(“%g\t”,term);
Sum+=term;
Fact=fact*i*(i-1);
Tempterm=(-1*tempter*x*x);
Term=tempterm/fact;
}
Printf(“\n\nsin(%g)=%.2f %.2f”,tempx,sum);
Getch();
}



TO ROUND AN INTEGER TO ITS NEAREST TENTH AND HUNDREDTH




To round of an integer to its nearest tenth and hundredth
#include<stdio.h>
Void main()
{
Int number,tenth,hundredth;
Clrscr();
Printf(“enter the number”);
Scanf(“%d”,&number);
Tenth=((number+5)/10)*10;
Hundredth=((number+50)/100)*100;
Printf(“%d can be rounded to the nearest tenth as %d\n”,number,tenth);
Printf(“%d can be rounded to the nearest hundredth as %d”,number,hundredth);
Getch();
}


TO INPUT THE FIRST LETTER OF THE COLOUR S IN VIBGYOR AND TO DISPLAY THE NAME OF THE COLOUR




#include<stdio.h>
Void main()
{
Char c;
Printf(“ente your letter from VIBGIYOR”);
Switch(toupper(c=getchar())
{
Case ‘v’ : printf(“violet”);
Break;
Case ‘i’: printf(“indigo”);
Break;
Case ‘b’: printf(“blue”);
Break;
Case ’g’: printf(“green”);
Break;
Case ’y’: printf(“yello’);
Break;
Case ’o’: printf(“orange”);
Break;
Case ‘r’: printf(“red”);
Break;
Default : printf(“bad data”);
}
Getch();
}


TO FIND LARGEST AND SMALLEST AMONG ‘N’ NUMBERS




#include<stdio.h>
Void main()
{
Int i=1,n;
Float num,large,small;
Printf(“how many numbers?”);
Scanf(“%d”,&n);
If(n<=0)
Printf(“invalid input”);
Else
{
Printf(“enter number-%d:”,i);
Scanf(“%f”,&num);
Large=small=num;
For(;i<n;++i)
{
Printf(“enter number-%d”,i+1);
Scanf(“%f”,&num);
If(large<num)
Large=num;
If(small>num)
Small=num;
}
Printf(“largest=%g\n”,large);
Printf(“smallest=%g”,small):
}
Getch();
}


TO DISPLAY THE SUM OF INDIVIDAL DIGITS OF A FOUR DIGIT NUMBER




To accept a four digit number and to display the sum of individual digits
#include<stdio.h>
Viod main()
{
Int number,temp,d1,d2,d3,d4,sum;
Clrscr();
Printf(“enter a number”;
Scanf(“%d”,&number);
Temp=number;
D4=number/1000;
Number=number%1000;
D3=number/100;
Number=number%100;
D2=number/10;
D1=number%10;
Sum=d4+d3+d2+d1;
Printf(“sum of individual digits of %d is %d”,temp sum);
Getch();
}

TO DISPLAY THE SUM AND AVERAGE OF A SET OF NUMBER THE PROGRAM SHOULD TERMINATE ONLY WHEN ZERO IS ENTERD




#incude<stdio.h>
Void main()
{
Int count=0;
Float num,sum=0.0,average;
Do
{
Printf(“enter your number -%d(zero to stop):”,++count”);
Scanf(“%f”,&num);
Sum=sum+num;
}
While(num!=0);
Average=sum/(count-1);
Printf(“sum is %g\naverage is %g”,sum ,average);
Getch();
}


TO DISPLAY THE MULTIPLES OF 5 WITHIN A GIVEN RANGE BOTH THE UPPER & LOWER BOUNDA ARE EXCLUDED




#include<stdio.h>
Void main()
{
Int lower,upper,temp;
Printf(“enter lower bound”);
Scanf(“%d”,&lower);
Printf(“enter upper bound”);
Scanf(“%d”,&upper);
If(lower>upper)
{
Temp=upper;
Upper=lower;
Lower=temp;
}
Printf(“the multiples of 5 between %d and %d are:”,lower,upper);
If(lower%5=0)
Lower=lower+5;
Else
Lower=lower+(5-(lower%5));
For(;lower<upper;lower+=5)
Printf(“%d\t”,lower);
Getch();
}


TO DISPLAY THE MEAN AND STANDERD DEVIATION OF THREE NUMBERS




To compute mean and standard deviation of three number a,b,c
#include<stdio.h>
#include<math.h>
Void main()
{
Float a,b,c,d,m,sd;
Clrscr();
Printf(“enter three numbers”);
Printf(“first number ?”);
Scanf(“%f”,&a);
Printf(“second number ?”);
Scanf(“%f”,&b);
Printf(“third number ?”);
Scanf(“%f”,&c);
M=(a+b+c)/3.0;
Sd=sqrt((pow(a-m),2)+pow(b-m),2)+pow(c-m),2))/3.0;
Printf(“average of %g,%g and %g is %g”,a,b,c,sd);
Getch();
}


TO DISPLAY THE FIRST ‘N’ RPIME NUMBERS




#include<stdio.h>
#include<math.h>
Void main()
{
Int num,i,n,flag,count=0;
Clrscr();
Printf(“how many prime number ?”);
Scanf(“%d”,&n);
Printf(“first %d prime numbers are :\n”,n);
For(num=2;count<=n;++num)
{
Flag=1;
For(i=2;i<=sqrt(num);++i)
{
if(num%i==0)
{flag=0;
Break;
}
If(flag)
{
Printf(“%d\t”,num);
++count;
}
}
getch();
}


TO DISPLAY THE FIBONACCI NUMBERS WITHIN A RANGE BOTH UPPER AND LOWER BOUNDA ARE EXCLUDED




#include<stdio.h>
Void main()
{
Long lower,upper,a=1,b=0,c,temp;
Clrscr();
Printf(“enter a range\n”);
Printf(“wht is the lower bound ?”);
Scanf(“%d”,&lower);
Printf(“what is the upper bound ?”);
Scanf(“%d”,upper);
If(lower>upper)
{
Temp=lower;
Lower=upper;
Upper=temp;
}
Printf(“Fibonacci numbers between %d and %d are :\n”,lower,upper);
Do
{
C=a+b;
a=b;
b=c;
if((c>lower)&&(c<upper))
printf(“%d\t”,c);
}
While(c<upper);
Getch();
}


TO DISPLAY FIRST ‘N’ EVEN NUMBERS IN REVERSE ORDER




#include<stdio.h>
Void main()
{
Int num;
Printf(“how many even numbers:”);
Scanf(“%d”,&num);
Printf(“first %d numbers in reverse order:\n”,num);
Num=2*num;
For(;num>1;num-=2)
Printf(“%d\t”,num);
Getch();





TO DISPLAY FIRST ‘N’ ODD NUMBERS ALSO DISPLAY THIR SUM AND AVERAGE




#include<stdio.h>
Void main()
{
Int n,num=1,count=0;
Float sum=0.0,average;
Printf(“how many odd numbers?”);
Scanf(“%d”,&n);
Printf(“first %d odd numbers are :\n”,n);
For(;count<n;++count)
{
Printf(“%d\t”,num);
Sum+=num;
Num+=2;
}
Average=sum/n;
Printf(“\nsum of the first %d odd numbers is %g”,n,sum);
Printf(“average of the first %d odd numbers is %g”,n,average);
Getch();
}


TO DISPLAY FACTORIAL OF ODD NUMBERS FROM 1 TO 10 AND THEIR SUM




#include<stdio.h>
Void main()
{
Int i=1;
Float fact=1.0,sum=0.0;
Printf(“odd numbers\t\tfactorial”);
For(;i<=10;++i,fact*=i)
If((i%2)==1)
{
Printf(“ %d \t\t %g\n”,i,fact);
Sum+=fact;
}
Printf(“sum of above factorials is %g”,sum);
Getch();
}



TO DISPLAY EVEN AND ODD NUMBERS WITHIN A RANGE SEPERATELY ALSO DISPLAY THEIR SUM AND AVERAGE




#include<stdio.h>
Void main()
{
Int lower,upper,even,odd,count,temp;
Float evensum=0.0,oddsum=0.0,evenavg,oddavg;
Clrscr();
Printf(“enter the lower bound:”);
Scanf(“%d”,&lower);
Printf(“enter the upper bound:”);
Scanf(“%d”,&upper);
If(lower>upper)
{
Temp=lower;
Lower=upper;
Upper=temp;
}
If(lower%2==0)
Even=lower+2;
Else
Even=lower+1;
Printf(“even numbers between %d and %d are :\n”,lower,upper);
For(count=0;even<upper;even+=2,++count)
{
Printf(“%d\t”,even);
Evensum+=even;
}
Evenavg=avensum/count;
Printf(“sum of the even numbers from %d to %d is %g”,lower,upper,evensum);
Printf(“average of the even numbers from %d to %d is %g\n\n”,lower,upper,evenavg);
if(lower%2=1)
odd=lower+2;
else
odd=lower+1;
printf(“odd numbers from %d to %d are\n”,lower,upper);
for(count=0;odd<upper;odd+=2,++count)
{
Printf(“%d\t”,odd);
Oddsum+=odd;
}
Oddavg=oddsum/count;
Printf(“\nsum of the odd numbers from %d to %d is %g”,lower,upper,oddsum);
Printf(“\naverage of the odd numbers from %d to %d is %g”,lower,upper,oddavg);
Getch();
}


TO DISPLAY AMSTRONG NUMBERS UP TO A GIVEN NUMBER




#include<stdio.h>
#include<math.h>
Void main()
{
Int sum,num,n,digit,temp;
Clrscr();
Printf(“up to which number ?”);
Scanf(“%d”&n);
Printf(“amstong numbers up to %d are:\n”);
For(num=1;num<=n;+}+num)
{
For(temp=num,sum=0;temp>0;temp/=10)
{
Digit=temp%10;
Sum+=pow(digit,3);
}
If(num==sum)
Printf(“%d\t”,num);
}
Getch();
}


TO CONVERT TEMPERATURE IN DEGREE CELSIUS TO FAHRENHEIT




To accept a temperature in degree Celsius(c) and to display it in degree fahrenheit(f) using the formula f=9.0/5.0*c+32
#include<stdio.h>
Void main()
{
float Celsius,fahrenheit;
clrscr();
printf(“enter temperature in degree Celsius:”);
scanf(“%f”,&celsius);
Fahrenheit=(9.0/5)*Celsius+32;
printf(“%g degree Celsius is %g fahrenheit”,elsius,fahrenheit);
Getch();
}



TO CONVERT NUMBER OF DAYS INTO YEARS, WEEKS AND DAYS




TO convert a given number of days into to a measure of time given in years weeks and days
#include<stdio.h>
Void main();
{
Int temp,days,week, year,day;
Printf(“enter the number of days:”);
Scanf(“%d”,&days);
Temp=days;
Year=(int)days/365;
Days=days%10;
Week=(int)days/7;
Days=days%7;
Printf(“%d days equals %d year,%d week and %d days”,temp,year,week,day);
Getch();
}


TO CONVERT A LOWER CASE TEXT TO UPPERCASE




#include<stdio.h>
Void main()
{
Int count=0;
Char text[80];
Clrscr();
Text[count]=getchar();
While(text[count]!=’\n’)
{
Count++;
Text[count]=getchar();
}
Count=0;
While(text[count]!=’\n’)
{
Putchar(toupper(text[count]));
Count++;
}
Getch();



TO CHECK WHETHER A GIVEN NUMBER IS PERFECT FACTORIAL OR NOT




#include<stdio.h>
Void main()
{
Int n,i=1,flag=0;
Long fact=1;
Printf(“enter your number:”);
Scanf(“%d”,&n);
For(;fact<=n;++,fact*=i)
If(fact==n)
{
Flag=1;
Break;
}
If(flag)
Printf(“%d is perfect factorial”,n);
Else
Printf(“%d is not a perfect factorial”,n);
Getch();
}


To calculate perimeter & area of a rectangle




To accept the length,breadth of rectangle and to display its perimeter and area

#include<stdio.h>
Void main()
{
Float length,breadth,perimeter,area;
Clrscr();
Printf(“enter the dimension of a rectangle\n”);
Printf(“length ?”);
Scanf(“%f”,&length);
Printf(“breadth ?”);
Scanf(“%f”,&breadth);
Perimeter=2*(length+breadth);
area=length*breadth;
printf(“perimeter of the rectangle is %.3g\n”,perimeter);
printf(“area of the rectangle is % .3g”,area);
getch();



To calculate circumference & area of a circle




To accept the radius of a circle and to display its circumference and area
#include<stdio.h>
#define pi 3.14
Void main()
{
Float radius,circumference,perimeter,area;
Clrscr();
Printf(“enter radius of a circle”);
Scanf(“%f”,&radius);
circumference=2*pi*radius;
area=pi*radius*radius;
printf(“circumference of the circle is %.3g\n”,circumference);
printf(“area of the circle is %.3g”,area);
getch();
}

SQURE OF A NUMBER


  • To input three integer quantities to the variables a,b,c and then perform the following
1.halves the value of a
2.doubles b
3.multiply c by itself
4.display the result of the preceding operations


#include<stdio.h>
Void main()
{
Int a,b,c;
Clrscr();
Printf(“enter three numbers\n”);
Printf(“first number?”);
Scanf(“%d”,&a);
Rpintf(“second number?”);
Scanf(“%d”,&b);
Printf(“third number ?”);
Scanf(“%d”,&c);
Printf(“half of %d is %g\n”,a,a/2.0);
Printf(double fo %d is %d\n”,b,b+b);
Printf(“square of %d is %d\n”,c c*c);
getch();
}

Keywords


Keywords are the standard identifiers that have standard,predefined meaning in c.these key word can be used only for their intended purpose and thay cannot be used as programmer defined identifiers. The standard key words are :
Auto do goto signed unsigned
Break double if sizeof void
Case else int static volatile
Char enum long stuct while
Const extern register switch continue
Float return typedef default for
Short union
Data types
The c language defines five fundamental data types:
Data type
Meaning
Size(byte)
Minimal range
Char
Character
1
-128 to 127
Int
Integer
2
-32,768 to 32767
Float
Double precision real number
4
3.4e- to 3.4e+38 with 6 digits of precision
Double
single precision real number
8
1.7e-308 to 1.7e+308 with 10 digit precision
void
Valueless
0



PROGRAM TO CALCULATE THE DISTANCE TRAVELLED


  • To input speed per hour and elapsed time and to calculate and display the distance travelled.
#include<stdio.h>
Void main()
{
Float distance,time,speed;
Clrscr();
Printf(“speed in Km/Hr?”);
Scanf(“%f”,&speed);
Printf(“time of journey?”);
Scanf(“%f”,&time);
distance=speed*time;
Printf(“%.2f Kms travelld within %.2fHrs at a speed of %.2fKm/Hr.”,distance,time,speed);
getch();
}



ALGORITM AND FLOW CHART


ALGORITHM
The term algorithm may be formally defined as a sequence of precise and unambiguous instruction for solving a problem in a finite number of operations.An algorithm must possess the following characteristics.
  • Each and every instruction should be precise and unambiguous.
  • Each instruction should be effective.
  • One or more instruction should not repeated infinitely.
  • An algorithm may have zero or more inputs,which are externally supplied.
  • An algorithm must produce the desired output(s).

FLOWCHART
Flowchart is the pictorial representation of a data processing procedure. It make the use of geometrical symbols to denote different types of instructions. The actual instructions are written within these symbols using clear and concise statements..These symbols are connected by solid line s having arrow marks to indicate the flow of operation,i.e.,the exact sequence in which the instructions are to be executed.
Terminal start stop
Terminal symbol is used to indicate the beginning (start),ending(stop) and pauses(halt) in the program logic flow.
Input/Output Read Print
The input/output symbol is used to denote any function of an input/output device in the program.
Processing Processing
A processing symbol is used in a flowchart to represent arithmetic and data movement operations.
Flow lines
Floe lines with arrowheads are used to indicate the flow of operations.
Decisions
Is condition No
Yes
A decision symbol is used in floe chart to indicate a point at which a decision has to be made and a branch to one or more alternative points is possible.


Connector A A

Connecter symbols represent an entry from or an exit to another part of the flowchart.A connector symbol is represented by a curcle and a letter or digit is placed within the circle to indicate the link.

Predefined process Function name
The Predefined process symbol is used to show the execution of a function tha is elsewhere in the flowchart or on a separate flowchart.
Magnetic Disk
Access to a file stored
On a magnetic disk

The access to data file stored on a magnetic disk is represented by the following disk symbol.

Wednesday 20 July 2011

design

C is an imparative(procedural) system implemetation language. It was designed to be compiled using a relatively straightforward compiler, to provide low-level access to memory, to provide language constructs that map efficiently to machine instructions, and to require minimal run time support C was therefore useful for many applications that had formerly been coded in assemblylanguage
Despite its low-level capabilities, the language was designed to encourage cross platformprogramming. A standards-compliant and portably written C program can be compiled for a very wide variety of computer platforms and operating systems with few changes to its source code. The language has become available on a very wide range of platforms, from embedded micro controller to supercomputer

structure of the c rogram

Every c program consists of one or more modules called functions.One of the functions must be called main.The programing will always begin by executing the main function.Any other function definition must be defined separately,either ahead or after the main.each function must contain :
                                                                  
  1.  A function heading,which consists of function name followed by an optional list of arguments,enclosed in parantheses.
  2. a list of argument declarations,if arguments are included in the heading.
  3. a compound statement,which comprises the remainder of the function.

Indroduction to c

c is a general purpose,structured progrmming language.Dennis Ritchie at the Bell laborataries in USA originally developed c in the 1970s.c is the result of the development process that started with an older language called BCPL developed by Martin Richards,and it influenced a language called B,which was invented by Ken Thomson.b led to the development of c in 1970s