[go: up one dir, main page]

0% found this document useful (0 votes)
53 views14 pages

CP Notes CHPTR 3

The document discusses fundamental concepts of variables and data types in C programming language. It covers character set, keywords, delimiters, constants, basic and derived data types specifying size and range of integer, float, character and other data types.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views14 pages

CP Notes CHPTR 3

The document discusses fundamental concepts of variables and data types in C programming language. It covers character set, keywords, delimiters, constants, basic and derived data types specifying size and range of integer, float, character and other data types.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

CHAPTER - 3

Variables and Data Types

3.1 Fundamental of C
Program is a set of instructions
to interact with computer. The
programs are written in
programing language. C is a
procedural programming
language developed at AT &
T’s Bell Laboratories of USA in
1972. It was designed and
written by a man named Dennis
Ritchie.

3.2 Character set of C


Character set is a package of valid characters recognized by compiler/interpreter. Each natural
language has its own alphabet and characters set as the basic building. We combine different
alphabets to make a word, sentence, paragraph and a meaningful passage. Similarly each
computer languages have their own character set. Particularly, C also has its own character set,
shows in following table:

Uppercase alphabets A, B, C……………………..Z


Lowercase alphabets a, b, c………………………z
Digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Special symbols +-*/!?|\<>&,:;#%*()[]._$^
White space characters Blank space, new line, horizontal tab, vertical tab, etc.

3.3 Keywords
Keywords are the predefined reserved (built-in) words whose meaning has already been defined
to the c compiler. A programmer can’t use the keywords for other task than the task for which it
has been defined. For example: a programmer can’t use the keyword for variable names.

int float, char;

Keywords cannot be used as variable name.

There are 32 keywords in c,


which are listed in figure:

Programming in C - Compiled by Yagya Raj Pandeya, NAST, Dhangadhi ©yagyapandeya@gmail.com Page 1

Print to PDF without this message by purchasing novaPDF (http://www.novapdf.com/)


3.4 Delimiters
Delimiters are small system components that separate the various elements (variables, constants,
statements) of a program. They are also known as separators. Some of the delimiters are:
 Comma: It is used to separate two or more variables, constants.
 Semicolon: It is used to indicate the end of a statement.
 Apostrophes (single quote): It is used to indicate character constant.
 Double quotes: It is used to indicate a string.
 White space: space, tab, new line etc.

3.5 Constants
Constant in C refers to fixed values
that do not change during execution
of a program.

a. Integer Constants: It refers to the sequence of digits with no decimal points. The three
kinds of integer constants are:
 Decimals: Decimal numbers are set of digits from 0 to 9 with leading +ve or –ve
sign. For example: 121, -512 etc.
 Octals: Octal numbers are any combination of digits from set 0 to 7 with leading 0.
For example: 0121, 0375 etc.
 Hexadecimals: Hexadecimal numbers are the sequence of digits preceding 0X or
0x. For example: 0xAB23, 0X787 etc.

b. Real or Floating point Constants: They are the numeric constants with decimal points.
The real constants are further categories as:
 Fractional Real constant: They are the set of digits from 0 to 9 with decimal
points. For example: 394.7867, 0.78676 etc.
 Exponential Real constants: In the exponential form, the constants are
represented in two form: Mentissia E exponent; Where, the Mentissia is
either integer or real constant but the exponent is always in integer form. For
example: 21565.32 = 2.156532 E4, where E4 = 104.

c. Character constant: Character constant are the set of alphabets. Every character has some
integer value known as American Standard Code for Information Interchange (ASCII).
The character constants are further categories as:
 Single character constant: It contains a single character enclosed with in a pair of
single cade mark (‘ ’). Thus, ‘X’, ‘5’ are characters but X, 5 are not.
 String constant: String constants are a sequence of characters enclosed in double
quotes marks (“ ”). The character may be letters, numbers, special symbols or
blank spaces. For example: “Hello”, “X+Y”. Note: ‘x’ ≠ “x”

Characters ASCII Values


A–Z 65 – 90
a–z 97 – 122
0–9 48 – 57

Programming in C - Compiled by Yagya Raj Pandeya, NAST, Dhangadhi ©yagyapandeya@gmail.com Page 2

Print to PDF without this message by purchasing novaPDF (http://www.novapdf.com/)


Basic data type
Data type is useful in C that tells the computer about the type and nature of the constant to be
stored in a variable. There are three basic data types in C. They are integer data type, real data
type and character data type. The secondary (derived) data types are a collection of primary
(primitive) data types.

In the C programming language, a data type refers to an extensive system used for declaring
variables or functions of different types. The type of a variable determines how much space it
occupies in storage and how the bit pattern stored is interpreted.

Data Required Memory


Description
Type
char A single character 1 byte (in all cases)
int An integer character 2 bytes
Floating-point number (i.e., a number containing a 4 bytes
float
decimal point and/or and an exponent)
Double-precision floating-point number (i.e., more 8 bytes
double significant figures, and an exponent which may be
larger in magnitude)

Type Qualifiers
Data type qualifiers add additional information to the data types. They are: short, long, signed or
unsigned. A number of qualifiers or modifiers may be assigned to any basic data type to vary the
number of bits utilized and the range of values represented by that data type. Here, short int may
require less space than an int or it may require the same amount of memory. Similarly, a long int
may require the same amount of memory as an int or it may require more memory, never less
than int. For example, int = 2 bytes, short int may be 1 byte or 2 bytes, int = 2 bytes, long int may
be 2 bytes or 4 bytes. The actual number of bytes used in the internal storage for these data types
depends on the machine being used.

Data types on 16-bit Meaning Format or Size Range


machine Type Specifier
unsigned char Unsigned character %c 8 bits 0 to 255
(positive)
signed char Represents single character. %c 8 bits -128 to 127
char
unsigned int Represents positive %u 16 bits 0 to 65,535
unsigned short int integer numbers
Short signed short represents both positive and %d 16 bits -32,768 to
short int negative integer quantity 32,767
signed short int
int
unsigned long int represents positive long % lu 32 bits 0 to
integer 4,294,967,295
long Represents both positive and % ld 32 bits -
signed long negative long integer 2,147,483,648
long int to
signed long int 2,147,483,647
Float Floating Point Number. %f 32 bits 1.2E-38 to
(6 decimal places) 3.4E38
Double A more accurate floating- % lf 64 bits 2.3E-308 to

Programming in C - Compiled by Yagya Raj Pandeya, NAST, Dhangadhi ©yagyapandeya@gmail.com Page 3

Print to PDF without this message by purchasing novaPDF (http://www.novapdf.com/)


(15decimal places) point number than float 1.7E308
long double Increases the size of double. % lf 80 bits 3.4E-4932 to
(19 decimal places) 1.1E4932
void Defines an empty data type which can then be associated with some
data types. It is useful with pointers.
Note: Signed = Include both negative and positive number. Unsigned: Only positive numbers.

User defended type declaration


C support a feature in which user can declare the type of data value at the initial time called
user defined type declaration. Syntax: typedef type identifier; Where, the
identifier may be any name given to the data type but it cannot be any keywords. For example:
typedef int nast; Where, the new data type nast define the value of integer type. In
other word, the name int is renames by nast.

Type casting
When an operator is used to manipulate operands of different data type then each operand should
have same data type to take operation because the final result must be in one data type. For
example, the given expression is: z = int x + float y; then the data type of z must be
only one. To handle such type of problems, the type conversion and type casting are introduced.
C provides two types of type conversions:
 Implicit type conversion: In implicit type conversion, if the operands of an expression
are of different types, the lower data type is automatically converted to the higher data
type before the operation evaluation. The result of the expression will be of higher data
type. The data types of the operand are changed according as the rule – CIFDL

Character Integer Float Double float Long double


(ASCII Values) (0. 6 digits) (0.14 digits) float
Priority Order

 Explicit type conversion: In explicit type conversion, the user has to enforce the
compiler to convert one data type to another data type by using typecasting operator. This
method of typecasting is done by prefixing the variable name with the data type enclosed
within parenthesis.
Syntax: (data type) variable/expression/value;

Example: float sum;


Sum = (int) (1.5 * 3.8);
The typecast (int) tells the C compiler to interpret the result of (1.5 * 3.8) as
the integer 5, instead of 5.7. Then, 5.0 will be stored in sum, because the
variable sum is of type float.
Note:
 Narrowing: Converting the higher data type value to lower data type value
 Widening: Converting the lower data type value to higher data type value.

3.6 Variables
A constant is a quantity (fixed value) that does not change during program
execution. A constant always remains fixed. Constant supplied by the user is
stored in block of memory by the help of variable. A variable is a quantity
whose value may change during program execution. Consider an expression:
2*x + 3*y = 5. In this expression the quantities x and y are variable. A
variable is a named location in memory that is used to hold a constant value.

Programming in C - Compiled by Yagya Raj Pandeya, NAST, Dhangadhi ©yagyapandeya@gmail.com Page 4

Print to PDF without this message by purchasing novaPDF (http://www.novapdf.com/)


In C, the names given to various program elements such as variables, functions, labels, and other
user defined items are called identifiers.

Rules for declaring variable name


1. Every variable name in C must start with a letter or underscore.
2. A valid variable name should not contain commas or blanks.
3. Valid variable name should not be keyword.
4. A variable name must be declared before using it.
5. Although, there is not a hard and fast rule about the variable name but should be
meaningful.

Declaring variable
After defining suitable variable name, we must declare them before used in our program. The
declaration deals with two things:
 It tells the compiler what the variable name is.
 It specifies what type of data the variable can hold.

Synatax: Data_type variable_names;

For example: int account number;


float a, b, c;
char name[10];

3.7 Operator, operands and operation


C supports a rich set of operators. An operator is a symbol tells the computer to perform
mathematical or logical operations on operands. Operators are used in programs to manipulate
data. C operators can be classified into a number of categories.

A. Arithmetic operators: Arithmetic operators are used for performing most common
arithmetic (mathematical) operations such as addition, multiplication, division and modulus.
For example: Assume variable A holds 10 and variable B holds 20 then:
Operator Name Example
+ Addition A + B will give 30
- Subtraction A - B will give -10
* Multiplication A * B will give 200
/ Division B / A will give 2
% Modulo division B % A will give 0
++ Increment A++ will give 11
-- Decrement A-- will give 9

Note: The increment or decrement operator may be pre increment (++k) or post increment
(k++). Similar the pre decrement (--k) or post decrement (k--).

B. Logical operators
Expressions which use logical operators are evaluated to either true or false. For example:
Assume variable A holds 1 and variable B holds 0 then:

Operator Description Example


&& Logical AND (A && B) is false.
|| Logical OR (A || B) is true.
! Logical NOT ! (A && B) is true.

Programming in C - Compiled by Yagya Raj Pandeya, NAST, Dhangadhi ©yagyapandeya@gmail.com Page 5

Print to PDF without this message by purchasing novaPDF (http://www.novapdf.com/)


C. Assignment Operator: An assignment operator assign a value to a variable and is
represented by “=” (equals to) symbol. Syntax: Variable name = expression. The
Compound assignment operators (+=, *=, -=, /=. %=) are also used.
Operator Name Example
= Assignment C = A + B will assign value of A + B into C
+= Add AND assignment C += A is equivalent to C = C + A
-= Subtract AND assignment C -= A is equivalent to C = C - A
*= Multiply AND assignment C *= A is equivalent to C = C * A
/= Divide AND assignment C /= A is equivalent to C = C / A
%= Modulus AND assignment C %= A is equivalent to C = C % A
<<= Left shift AND assignment C <<= 2 is same as C = C << 2
>>= Right shift AND assignment C >>= 2 is same as C = C >> 2
&= Bitwise AND assignment C &= 2 is same as C = C & 2
^= Bitwise Ex-OR and assignment C ^= 2 is same as C = C ^ 2
|= Bitwise OR and assignment C |= 2 is same as C = C | 2

D. Relational operators
These operators are used to form relational expressions, which evaluates to either true or
false. (True – 1, false – 0). Assume variable A holds 10 and variable B holds 20 then:

Operator Name Example


== Equal to (A == B) is not true.
!= Not equal to (A != B) is true.
> Greater than (A > B) is not true.
< Less than (A < B) is true.
>= Greater than or equal to (A >= B) is not true.
<= Less than or equal to (A <= B) is true.
The truth tables for &, |, and ^
p q p&q p|q p^q
E. Bitwise operators 0 0 0 0 0
These operators are used to access machine at bit level.
0 1 0 1 1
Assume variable A holds 60 and variable B holds 13
1 1 1 1 0
then:
1 0 0 1 1
Operator Name Example
& Binary AND (A & B) will give 12 which is 0000 1100
| Binary OR (A | B) will give 61 which is 0011 1101
^ Binary XOR (A ^ B) will give 49 which is 0011 0001
~ Binary Ones Complement (~A ) will give -60 which is 1100 0011
<< Binary Left Shift A << 2 will give 240 which is 1111 0000
>> Binary Right Shift A >> 2 will give 15 which is 0000 1111

F. Special operators
Operator Name Example
sizeof() Sizeof sizeof(a), where a is integer, will return 2.
& address of &a; will give actual address of the variable.
* Pointer *a; will pointer to a variable.
?: Conditional If (Condition) ? TRUE case : FALSE case
Example: big = (a>b)? a: b;

Programming in C - Compiled by Yagya Raj Pandeya, NAST, Dhangadhi ©yagyapandeya@gmail.com Page 6

Print to PDF without this message by purchasing novaPDF (http://www.novapdf.com/)


Operators Precedence and Associativity
Operator precedence is grouping of terms to evaluate an expression in order to their priority
order. Within an expression, higher precedence operators will be evaluated first. The
precedence of operator has direct impact on determining the result of the expression.
For example: x = 7 + 3 * 2;
Result: x = 13 if * have higher precedence x = 7 + (3 * 2);
Result: x = 20 if + have higher precedence x = (7 + 3) * 2;
The associativity defines the direction of calculation of expression when there is two or more
operator of same precedence.
Operator(s) Name Order of evaluation (precedence)
() Parentheses If the parentheses are nested, the expression in the innermost
pair is evaluated first. If there are several pairs of parentheses
“on the same level” (i.e., not nested), they’re evaluated left to
right.
* Multiplication If there are several, they’re evaluated left to right.
/ Division
% Remainder
+ Addition If there are several, they’re evaluated left to right.
- Subtraction
= Assignment Evaluated last

Preprocessor directives
3.8 The first C program
main()
The C is a procedural (function) oriented programming {
language. A function is a block of statements to perform Statement 1;
a given task. Each function would have a series of Statement 2;
statements equipped with the variables, constants, ……………………………
keywords and operators. Each instruction in C program is Statement n;
regarded and written as a separate statement. The general }
structure of C program would be of following types.

/* A program to print a message */ // comment


#include<stdio.h> // preprocessor directive
main() // beginning of a program
{
printf(“Hello World”); // print function
} // end of a program
Output: Hello World

1. Comments
A comment is a descriptive sentence written in a program. Although a comment is not
compulsory in a program but it is good practice as it makes a program simpler to understand
the logic, more readable, correcting the future errors, and enhancements features. A
comments are not executable statements therefore the compilers ignore the comments.
Comments in C can be written in two ways:
 Single line comment: The comment starts with //. For e.g. main() // start the
program
 Multiline comment: The comment starts with /* and end with */.
For e.g. main() /* Main function
start the program */
Programming in C - Compiled by Yagya Raj Pandeya, NAST, Dhangadhi ©yagyapandeya@gmail.com Page 7

Print to PDF without this message by purchasing novaPDF (http://www.novapdf.com/)


2. Preprocessor directive
A preprocessor is built into the c compiler. When a program is to be compiled, then the
source code is first preprocessed. A preprocessor has following format:
#include<header file>
Thus, the preprocessor directive line must not end with any symbol and only one
preprocessor directive can appear in one line.
#include<stdio.h>
#include<stdio.h>;
#include<conio.h>
#include<stdio.h>#include<math.h>
#include<math.h>
(Invalid Statements)
(Valid Statements)

 stdio.h (standard input output header file) used for standard function like
o printf() – output function used to print a message or value of a variable on the
screen.
o scanf() – input function used to input a value through the keyboard.
o putchar() – output function used to print a single character
o getchar() – input function used to input a single character
o fprintf() – output function used to print a file
o fscanf() – input function used to input a file
 string.h: Includes the string functions like
o puts() –output function used to print a string
o gets() – input function used to input a string
o strlen() – find the length of given sting
o strcpy() – cppy string from variable to another
 math.h: Includes the mathematical functions like
o sqrt() – to find the square root of a number
o pow() – to calculate a value raised to power
o abs() – to find the absolute value of an integer
o log() – to calculate the natural logarithm, etc.
 conio.h (console input output header file) used for display controlling function like
o getch(); – Function used to hold the output value of program until pressing any
key. It doesn’t echo character.
o getche(); – It echoes (show)the character.
o clrscr(); – Function used to clear the screen holding the previous values.

Console: A user typically interacts with a console application using only a keyboard and
display screen, as opposed to GUI applications, which normally require the use of a mouse or
other pointing device.

3. main( )
C program may consist of more than one function. If a program consists only one function it
must be main() that shows the beginning of a program. It is not possible to write a program
without main() because program execution always starts with this function. The main( ) is not
followed by comma or semicolon.

The function main( ) encloses all the statements within a pairs of braces {…..}. Each line
written between the braces is a statement. Every statement is terminated by semicolon. The
semicolon at the end of a statement separates one statement from other. More than one
statement can be written on the same line by separating them with individual semi column.
However, it is good practice to write one statement per line.

Programming in C - Compiled by Yagya Raj Pandeya, NAST, Dhangadhi ©yagyapandeya@gmail.com Page 8

Print to PDF without this message by purchasing novaPDF (http://www.novapdf.com/)


4. Escape Sequences (Backslash Character Constants)
An escape sequence always begins with a backward slash and is followed by one or more
special characters.
Escape sequence Description
\' (single quote) Output the single quote (') character.
\" (double quote) Output the double quote (") character.
\? (question mark) Output the question mark (?) character.
\\ (backslash) Output the backslash (\) character.
\a (alert or bell) Cause an audible (bell) or visual alert.
\b (backspace) Move the cursor back one position on the current line.
\f (new page or form feed) Move the cursor to the start of the next logical page.
\n (newline) Move the cursor to the beginning of the next line.
\r (carriage return) Move the cursor to the beginning of the current line.
\t (horizontal tab) Move the cursor to the next horizontal tab position.
\v (vertical tab) Move the cursor to the next vertical tab position.

5. Input and Output Build-In Functions

A. Unformatted I/O statements


In the unformatted I/O function, we do not need to specify the format of data used by the
variable. The C-compiler automatically knows the format according to the function name.
Generally, the unformatted I/O functions are of char or string type. For example:
getchar(): Input single character putchar(): Output single character
Syntax:char-variable=getchar(); Syntax:putchar(char variable);
Example: char ch; Example: char ch;
ch = getchar(); ch = getchar();
Note: The getchar() and putchar() functions are defined on the stdio.h header file of C
gets(): Input a string puts(): Output a string
Syntax: gets(stringvariable); Syntax: puts(str);
Example: char ch[5]; Example: char ch[5];
gets(ch); gets(ch);
puts(ch);
Note: The gets() and puts() functions are defined on the string.h header file of C

B. Formatted I/O Statements


C has a special formatting character (%) to arrange the input data in a particular format. Some
of the format specifies are: %c – character, %d – integer, %f – float, %s – string, %ld – long
integer, %o – octal, %x – hexadecimal, %hd – short integer, and %u – unsigned.
 scanf(): scanf () function is used to read or input to a variable. Address operator (&) is
used before the variables which store the value in the specified variable name.
General Form: scanf (“format string”, & list of variables);

Example:
scanf (“%c %d %f”, &ch, &i, &x);
scanf (“%[^\n]s”, str); /*accepts all inputs including space. Stops when it
encounters new line.*/
scanf (“%d=%d”, &a, &b); /*delimiter between two input is = (10=20)*/
scanf (“%2d%5d”,&a,&b); /*if the input is 12345 & 10, a=12 & b=345 if the
input is 12 & 3456, a= 12 & b=3456*/
scanf (“%d%d”, &a,&b); /*if the input is 12345 & 10, a=12345 & b=10*/

Programming in C - Compiled by Yagya Raj Pandeya, NAST, Dhangadhi ©yagyapandeya@gmail.com Page 9

Print to PDF without this message by purchasing novaPDF (http://www.novapdf.com/)


Note: If we put: scanf (“%c,%d,%f”, &ch, &i, &x); Then give the input with
comma, for example: A, 6, 3.99

 printf(): The library function printf () is used to output the values. This function can be
used in variety of form as described below:
a. Character that will be printed on the screen as they appeared
Syntax: printf (“control string”, arg1, ..argn);
Example: printf (“Enter name of student”);

b. Format specification that define the output format for display of each item
Syntax: printf (“control string”);
Example: printf (“%d”, a);

c. Escape sequence character such as new line (\n), tab (\t) bell (\b) etc.
Example: printf (“\t”);

d. It is also used for space writing


i. For integer numbers
Syntax: printf (“%wd”, variable);
Where, w = field width and data type for integer is d
For example: int a = 345;
Format Output
printf (“%d”, a); 3 4 5
printf (“%2d”, a); 3 4 5
printf (“%5d”, a); 3 4 5
printf (“%-5d”, a); 3 4 5
printf (“%05d”, a); 0 0 3 4 5
For example: int a = 3, b = 4, c = 5;
printf (“%4d, %4d, %4d”, a, b, c);
Output:
3 4 5

ii. For float numbers


Syntax: printf (“%wpf”, variable);
Where, w = field width, p = number of digits after decimal points and f =
data type for float
For example: float a = 98.6754;
Format Output
printf (“%f”, a); 9 8 . 6 7 5 4
printf (“%7.2f”, a); 9 8 . 6 7
printf (“%-7.2f”, a); 9 8 . 6 7

iii. For characters


Syntax: printf (“%wc”, variable); //for single character
Syntax: printf (“%wps”, variable); //for string
Where, w = field width, p = number of character display, s= data type for
string and c = data type for character
For example: char a = x;
Format Output
printf (“%c \n %3c \n x
%5c \n”, a, a, a); x
x
Programming in C - Compiled by Yagya Raj Pandeya, NAST, Dhangadhi ©yagyapandeya@gmail.com Page 10

Print to PDF without this message by purchasing novaPDF (http://www.novapdf.com/)


LAB SECTION

1. WAP to display the text “Hello World” on the screen.


#include<stdio.h>
#include<conio.h>
void main()
{
printf(“Hello World”);
getche();
}

2. Program to convert pound into kilogram.


#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
float kg, pound;
printf("ENTER THE VALUE OF POUND:");
scanf("%f",&pound);
kg = 0.4536*pound;
printf("THE EQUIVALENT KG IS : %f", kg);
getch();
}

3. If a number is input through the keyboard. WAP to find its square root.
#include<stdio.h>
#include<conio.h>
#include<math.h>
main( )
{
int n, z;
clrscr( );
printf(“enter a number”);
scanf(“%d”, &n);
z=sqrt(n);
printf(“\nthe square root of %d is %d”, n, z);
getch( );
}

4. Three quantities principle, time and rate are input through the keyboard. WAP to find
simple interest.
#include<stdio.h>
#include<conio.h>
main( )
{
float p, t, r, si;
clrscr( );
printf(“enter the principle, time and rate”);
scanf(“%f%f%f”, &p, &t, &r);
si=(p*t*r)/100;
printf(“\n the simple interest=%f”,si);
getch( );
}
Programming in C - Compiled by Yagya Raj Pandeya, NAST, Dhangadhi ©yagyapandeya@gmail.com Page 11

Print to PDF without this message by purchasing novaPDF (http://www.novapdf.com/)


5. Program to calculate the compound interest
#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
float p,t,r, a, i;
printf("ENTER PRINCIPAL, RATE:");
scanf("%f%f",&p,&r);
printf("ENTER THE TIME IN YEAR:");
scanf("%f",&t);
a = p * pow(((100+r)/100),t);
i = a - p;
printf("\n THE AMOUNT IN TIME :%f Year is: %f",t, a);
printf("\n The interest is: %f",i);
getch();
}

6. Two numbers are input through the keyboard. WAP to find addition, subtraction,
multiplication, quotient and remainder after division.
#include<stdio.h>
#include<conio.h>
main( )
{ int x, y, z1, z2, z3, z4, z5;
clrscr( );
printf(“Enter two numbers”);
scanf(“%d %d”, &x, &y);
z1=x+y;
z2=x-y;
z3=x*y;
z4=x/y;
z5=x%y;
printf(“summation=%d, difference=%d, multiplication=%d,
quotient=%d and remainder=%d”, z1, z2, z3, z4, z5);
getch( );
}

7. Temperature of a city in centigrade is input through the keyboard. WAP to convert this
temperature into Fahrenheit degree.
#include<stdio.h>
#include<conio.h>
main( )
{
float c, f;
clrscr( );
printf(“enter the temperature in centigrade”);
scanf(“%f”, &c);
f=1.8*c+32;
printf(“the temperature in Fahrenheit = %f”, f);
getch( );
}

Programming in C - Compiled by Yagya Raj Pandeya, NAST, Dhangadhi ©yagyapandeya@gmail.com Page 12

Print to PDF without this message by purchasing novaPDF (http://www.novapdf.com/)


8. If the marks obtained by a student in 5 different subjects are input through the keyboard.
WAP to find his/her total and percentage mark.
#include<stdio.h>
#include<conio.h>
main( )
{
int m1, m2, m3, m4, m5, total, per;
clrscr( );
printf(“enter marks”);
scanf(“%f%f%f%f%f”, &m1, &m2, &m3, &m4, &m5);
total=m1+m2+m3+m4+m5;
per=total/5;
printf(“total marks=%f and percentage=%f”, total, per);
getch( );
}

9. Write a program to find the area & perimeter of a rectangle and area & circumference of
a circle.
#include<stdio.h>
#include<conio.h>
#include<math.h> /*for function pow( )*/
main( )
{
float l, b, area1, perimeter, rad, area2, cir;
clrscr( );
printf(“Enter length, breadth of a rectangle:”);
scanf(“%f%f”, &l, &b);
printf(“Enter radius of a circle:”);
scanf(“%f”, &rad);
area1 = l*b;
perimeter = 2*(l+b);
area2 = 3.14*pow(rad, 2);
cir = 2*3.14*rad;
printf(“Results of rectangle:”);
printf(“\nAarea = %f m2\n”,area1);
printf(“ Perimeter = %f”, perimeter);

printf(“\nResults of circle:”);
printf(“\nArea = %f”, area2);
printf(“\nCircumference = %f”, cir);
getch();
}

10. If a 4 digit number is input through the keyboard. WAP to find reverse.
#include<stdio.h>
#include<conio.h>
main( )
{
int n, d1, d2, d3, d4, rev=0;
clrscr( );
printf(“Enter a 4 digit number”);
scanf(“%d”, &n);
d1=n%10;
Programming in C - Compiled by Yagya Raj Pandeya, NAST, Dhangadhi ©yagyapandeya@gmail.com Page 13

Print to PDF without this message by purchasing novaPDF (http://www.novapdf.com/)


rev=rev*10+d1;

n=n/10;
d2=n%10;
rev=rev*10+d2;

n=n/10;
d3=n%10;
rev=rev*10+d3;

n=n/10;
rev=rev*10+n;
printf(“reverse=%d”, rev);
getch( );
}

11. The baic salary of an employee is input through the keyboard. WAP to determine his gross
salary if his dearness allowence is 40% of his basic salary, houserent allowness is 20% of
his basic salary.
#include<stdio.h>
#include<conio.h>
void main()
{
float bs,da,hr,gs;
printf(“ENTER THE BASIC SALARY:”);
scanf(“%f”,&bs);
ds=(40/100)*bs;
hr=(20/100)*bs;
gs=bs+ds+hr;
printf(“THE BASIC SALARY OF THE EMPLYOEE IS %f”,gs);
getche();
}

12. Write a Program to find the area of triangle when three sides of a triangle are given.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,s,area;
printf ("\nENTER THE THREE SIDES OF A TRINAGLE:");
scanf ("%f%f%f",&a,&b,&c);
s = (a+b+c)/2;
area = sqrt((s* (s-a)*(s-b)*(s-c)));
printf("\n Sides of triangle are: \na=%f\nb=%f\nc=%f",a,b,c);
printf("\n The area of triangle is:%f sq units",area);
getch();
}

Programming in C - Compiled by Yagya Raj Pandeya, NAST, Dhangadhi ©yagyapandeya@gmail.com Page 14

Print to PDF without this message by purchasing novaPDF (http://www.novapdf.com/)

You might also like