Monday, April 11, 2016

Program to convert and print distance meter and centimeter.

Q.The distance between two cities (in kms) is input through the keyboard. Write a program to convert and print this distance in meters, and centimeters.
Program:
#include <stddio.h>
#include <conio.h>
void main()
{
float km,m,c;
printf(“enter the distance in km;”);
scanf(“%f”,&km);
m=km*1000;
c=m*1000;
printf(“\nThe distance in meter is=%f”,m);
printf(“\nThe distance in centimeter is=%f”,c);
getch();
}
Output:
enter the distance in km:2
The distance in meter is=2000

The distance in centimeter is=2000000

Program that will take a integer as input and display its square as output

Q.Write a program that will take a integer as input and display its square as output.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,x;
printf(“enter the integer for a=”);
scanf(“%d”,&a);
x=a*a;
printf(“\nThe square of a is=%d,x);
getch();
}
Output:
enter the integer for a=2

The square of a is=4

Program that take dollar currency as input and display output in NC and IC

Q.Write a program that take dollar currency as input and display output in NC and IC.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
float DC, NC,IC;
printf(“enter the dollar currency=”);
scanf(“%f”,&DC);
NC=DC*100;
IC=NC*16;
printf(“\ncurrency in Nepal is=%f”,NC);
printf(“\ncurrency in India is=%f”,IC);
getch();
}
Output:
enter the currency in dollar=2
currency in Nepal is=200

currency in India is=32.0

Program to convert this temperature into Celsius.

Q.Temperature of a city in Fahrenheit degree is input through the keyboard. Write a program to convert this temperature into Celsius.
Program:
      #include<stdio.h>
#include<conio.h>
void main()
{
float F,C;
printf(“enter temperature in fahrenheit=”);
scanf(“%f”,&F);
c=5.0/9*(f-32);
printf(\nThe temperature in celcius is=%f”,C);
getch();
}
Output:
enter temperature in Fahrenheit=40

The temperature in celcius is=-40.000

Program to calculate the area and perimeter of the rectangle, and the area and circumference of the circle.

Q.The length and breadth of a rectangle and radius of a circle are input through the keyboard. Write a program to calculate the area and perimeter of the rectangle, and the area and circumference of the circle.
Program:
#include <stdio.h>
#include<conio.h>
#define PI 3.1428
void main()
{
float r,a,p,l,b,pr,A;
printf(“enter length and breadth of rectangle=”);
scanf(“%f”&l,&b);
printf(“enter radius of circle=”);
scanf(“%f”&r);
     a=PI*r*r;
p=2*PI*r;
     A=l*b;
     pr=2*(l+b);
     printf(“\nThe area of circle is=%f”, a);
     printf(“\nThe perimeter of circle is=%f,p);
     printf(“\nThe area of rectangle is=%f”,A);
     printf(“\nThe perimeter of rectangle is=%f”,pr);
     getch();
     }
Output:
enter length and breadth of rectangle= 3,2
enter radius of circle =7
The area of circle is=15309972
The perimeter or circle=43.9992
The area of rectangle is=6

The perimeter of rectangle is=10

C Building Blocks



C Building 
In any language there are certain building blocks:
• Constants
• Variables
• Operators
• Methods to get input from user(scanf( ), getch( ) etc.)
• Methods to display output (Format Specifier, Escape Sequences etc.) etc.
Format Specifiers
Format Specifiers tell the printf statement where to put the text and how to display the text. The various format specifiers are:
%d => integer
%c => character
%f => float etc.
Variables and Constants
If the value of an item is to be changed in the program then it is a variable. If it will not change then that item is a constant. The various variable types (also called data type) in C are:
int, float, char, long ,double etc they are also of the type signed or unsigned.
Escape Sequences
Escape Sequence causes the program to escape from the normal interpretation of a string, so that the next character is recognized as having a special meaning. The back slash “\” character is called the Escape Character”. The escape sequence includes the following:
\n => new line
\t => tab
\b => back space
\r => carriage return
\” => double quotations
\\ => back slash etc.
Taking Input From the User
The input from the user can be taken by the following techniques: scanf( ), getch( ), getche( ), getchar( ) etc.

Operators
There are various types of operators that may be placed in three categories:
Basic: + - * / %
Assignment: = += -= *= /= %=
(++, -- may also be considered as assignment operators)
Relational: < > <= >= == !=
Logical: && , || , !
EXERCISE
1. Write a program which shows the function of each escape sequence character.
eg printf(“alert ring bell rings like \a\a\a\a\a\a\a\a\a\a\a\a\a\a”);
printf(“the tab is inserted like \t this”);etc
Ans:
Output:
(i)  With pop sound, alert ring bell like
(ii) the tab is inserted like this

2. Write down C statements to perform the following operations:
i. z = 4.2((x+y)5/z – 0.52x/(y+z))/(x+y)2
Ans :
(i)4.2*((x+y)*5/z-0.52*x/(y+z))/(x*x+2*x*y+y*y)
ii. x = a2 +2ab+b
Ans :
x=a*a+2*a*b+b*b
3. What will be the output of the mix mode use of integers and float.
a=5/9;
b=5.0/9;
printf(“%f,%f”,a,b);
Ans:
       Output:
       a=0.000000
       b =0.555556

4. What will be the output if a=5,
(i) printf(“%d”,++a);
(ii) printf(“%d”,a++);
Ans:
Output:
      (i)  a=6
      (ii) b =5

5. Write an output of the following program.
i. #include<stdio.h>

void main()
      {
      int a,b,c,d;
      float x,y,z,l;
     
char ch;
      printf("\nThe value of a=\n");
      scanf("%d",&a);

      printf("\nThe value of b=\n");
      scanf("%d",&b);

      printf("\nThe value of c=\n");
      scanf("%d",&c);
     
      printf("\nThe value of d=\n");
      scanf("%d",&d);

      x=a+b;
      y=a-b;
      z=a*b;
      l=a*b*c*d;

      printf("\na + b = %f",x);
      printf("\na - b = %f",y);
      printf("\na * b = %f",z);
      printf("\n\na*a*c*d = %f",l);
      }

Ans:
Output:
The value of a=1
The value of b=2
The value of c=3
The value of d=2
a + b =3
a - b= -1
a * b= 2
a*b*c*d=12

ii. #include<stdio.h>
void main()
{
int i,j,k;
i=5;
j=10;
k=15;
printf(“\n%d %d %d”,i,j,k);
i++;
j++;
k++;
printf(“\n%d %d %d”,i,j,k);
printf(“\n%d %d %d”,i++,j++,k++);
printf(“\n%d %d %d”,i,j,k);
}
Ans:
Output:
7
12
17

Introduction To Turbo C IDE

Introduction of Turbo C IDE and Programming Environment
THEORY
The Development Environment - Integrated Development Environment (IDE):
The Turbo C compiler has its own built-in text editor. The files you create with text editor are called source files, and for C++ they typically are named with the extension .CPP, .CP, or .C.

The C Developing Environment, also called as Programmer’s Platform, is a screen display with windows and pull-down menus. The program listing, error messages and other information are displayed in separate windows. The menus may be used to invoke all the operations necessary to develop the program, including editing, compiling, linking, and debugging and program execution.


To invoke the IDE from the windows you need to double click the TC icon in the directory c:\tc\bin.
The alternate approach is that we can make a shortcut of tc.exe on the desktop. This makes you enter the IDE interface, which initially displays only a menu bar at the top of the screen and a status line below will appear. The menu bar displays the menu names and the status line tells what various function keys will do.
Default Directory
The default directory of Turbo C compiler is c:\tc\bin.
Using Menus
If the menu bar is inactive, it may be invoked by pressing the [F10] function key. To select different menu, move the highlight left or right with cursor (arrow) keys. You can also revoke the selection by pressing the key combination for the specific menu.

Opening New Window
To type a program, you need to open an Edit Window. For this, open file menu and click “new”. A window will appear on the screen where the program may be typed.


Writing a Program
When the Edit window is active, the program may be typed. Use the certain key combinations to perform specific edit functions.
Saving a Program
To save the program, select save command from the file menu. This function can also be performed by pressing the [F2] button. A dialog box will appear asking for the path and name of the file. Provide an appropriate and unique file name. You can save the program after compiling too but saving it before compilation is more appropriate.
Making an Executable File
The source file is required to be turned into an executable file. This is called “Making” of the .exe file. The steps required to create an executable file are:
1. Create a source file, with a .c extension.
2. Compile the source code into a file with the .obj extension.
3. Link your .obj file with any needed libraries to produce an executable program.
 All the above steps can be done by using Run option from the menu bar or using key
combination Ctrl+F9 (By this linking & compiling is done in one step).
Compiling the Source Code
Although the source code in your file is somewhat cryptic, and anyone who doesn't know C will struggle to understand what it is for, it is still in what we call human-readable form. But, for the computer to understand this source code, it must be converted into machine-readable form. This is done by using a compiler. Hence, compiling is the process in which source code is translated into machine understandable language.
It can be done by selecting Compile option from menu bar or using key combination Alt+F9.
Creating an Executable File with the Linker
After your source code is compiled, an object file is produced. This file is often named with the extension .OBJ. This is still not an executable program, however. To turn this into an executable program, you must run your linker. C programs are typically created by linking together one or more OBJ files with one or more libraries. A library is a collection of linkable files that were supplied with your compiler.
The Development Cycle
If every program worked the first time you tried it that would be the complete development cycle: Write the program, compile the source code, link the program, and run it.
Unfortunately, almost every program, no matter how trivial, can and will have errors, or bugs, in the program. Some bugs will cause the compile to fail, some will cause the link to fail, and some will only show up when you run the program.
Whatever type of bug you find, you must fix it, and that involves editing your source code, recompiling and relinking, and then rerunning the program.

Exiting IDE
An Edit window may be closed in a number of different ways. You can click on the small square in the upper left corner, you can select close from the window menu, or you can press the [Alt][F3] combination. To exit from the IDE, select [Alt][X] Combination.
EXERCISE
Exit from the File Menu or press
1. Type the following program in C Editor and execute it. Mention the Error.
void main(void)
{
printf(“ This is my first program in C ”);
}
Error: Function ‘ printf ’ should have a prototype.

2.Add the following line at the beginning of the above program. Recompile the
program. What is the output?
#include<stdio.h>
Ans:
#include <stdio.h>
void main()
{
     printf (“  This is my first program”);
 }
Output:-

This is my first program in C.


3.   Make the following changes to the program. What Errors are observed?
i. Write Void instead of void .
Ans:
    Error: Declaration syntax error.
ii. write void main (void);
Ans:
     Error :Declaration terminated incorrectly.
iii. Remove the semi colon ‘;’.
Ans:
     Error: Statement missing.
iv. Erase any one of brace ‘{’ or ‘}’.
Ans:
     Error (1): Declaration syntax error
     Error (2): Unexpected }.

Compiling and linking in the IDE
In the Turbo C IDE, compiling and linking can be performed together in one step. There are two ways to do this: you can select Make EXE from the compile menu, or you can press the [F9] key.

Executing a Program
If the program is compiled and linked without errors, the program is executed by selecting Run from the Run Menu or by pressing the [Ctrl+F9] key combination.

Correcting Errors
If the compiler recognizes some error, it will let you know through the Compiler window. You’ll see that the number of errors is not listed as 0, and the word “Error” appears instead of the word “Success” at the bottom of the window. The errors are to be removed by returning to the edit window. Usually these errors are a result of a typing mistake. The compiler will not only tell you what you did wrong; they’ll point you to the exact place in your code where you made the mistake.