B.C.A: 2025-2026 F.Y. B.C.A.
(SCIENCE)
Assignment 1
Assignment on use of data types, simple operators (expressions)
Data Types in C:
Each variable in C has an associated data type. Each data type requires different amount of
memory and has some specific operations, which can be performed over it. Let us briefly describe
them one by one.
Following are the examples of some very common data types used in C:
1. char: The most basic data type in C. It stores a single character and requires a single byte of
memory in almost all compilers.
2. int: As the name suggests, an int variable is used to store an integer.
3. float: It is used to store decimal numbers (numbers with floating point value) with single
precision.
4. double: It is used to store decimal numbers (numbers with floating point value) with double
precision.
Different data types also have different ranges up to which they can store numbers. These ranges
may vary from compiler to compiler. Below is list of ranges along with the memory requirement
and format specifiers.
MEMORY FORMAT
DATA TYPE RANGE
(BYTES) SPECIFIER
short int 2 -32,768 to 32,767 %hd
unsigned short int 2 0 to 65,535 %hu
unsigned int 4 0 to 4,294,967,295 %u
-2,147,483,648 to
int 4
2,147,483,647 %d
-2,147,483,648 to
long int 4 %ld
2,147,483,647
unsigned long int 4 0 to 4,294,967,295 %lu
long long int 8 -(2^63) to (2^63)-1
%lld
0 to
unsigned long long int 8 %llu
18,446,744,073,709,551,615
B.C.A: 2025-2026 F.Y. B.C.A. (SCIENCE)
signed char 1 -128 to 127 %c
unsigned char 1 0 to 255 %c
float 4
1.2E-38 to 3.4E+38 %f
double 8 %lf
1.7E-308 to 1.7E+308
long double 16
3.4E-4932 to 1.1E+4932 %Lf
We can use the sizeof() operator to check the size of a variable.
Operators in C:
An operator is a symbol that tells the compiler to perform specific mathematical or logical
functions. C language is rich in built-in operators and provides the following types of operators.
1. Arithmetic Operators (+,-,*,/,%)
2. Relational Operators (<, <=, >, >=, ==, !=)
3. Logical Operators (&&, ||, !)
4. Bitwise Operators(&, |, ^, <<, >>, ~)
5. Assignment Operators (=)
6. Increment Operator(++) and Decrement Operator(--)
7. Conditional Operator (expr1?expr2:expr3)
I. Arithmetic Operators:
Operator Meaning of Operator
+ Addition or unary plus
- Subtraction or unary minus
* Multiplication
/ Division
% Remainder after division( modulo division)
II. Increment and Decrement Operators
One uses increment operators to increase the value of the variable and one
in C programs uses decrement operators to decrease the value of the
variable.
B.C.A: 2025-2026 F.Y. B.C.A. (SCIENCE)
Syntax:
Increment operator: ++var_name; (or) var_name++;
Decrement operator: --var_name; (or) var_name--;
Example:
Increment operator: ++ i ; i ++ ;
Decrement operator: – – i ; i––;
Difference between pre/post increment & decrement operators in C:
Operator Description
value of i is incremented before
Pre increment operator (++i)
assigning it to the variable i
value of i is incremented after
Post increment operator (i++)
assigning it to the variable i
value of i is decremented before
Pre decrement operator (- -i)
assigning it to the variable I
value of i is decremented after
Post decrement operator (i- -) assigning it to variable i
Example program for pre – increment operators in C:
//Example for increment operators
#include <stdio.h>
int main()
{
int i=0;
while(++i < 5 )
printf("%d ",i);
return 0;
Output : 1 2 3 4
Step 1: In above program, value of “i” is incremented from 0 to 1 using pre-
increment operator.
Step 2: This incremented value “1” is compared with 5 in while expression.
Step 3: Then, this incremented value “1” is assigned to the variable “i”.
Above 3 steps are continued until while expression becomes false and output is displayed
as “1 2 3 4”.
B.C.A: 2025-2026 F.Y. B.C.A. (SCIENCE)
Example program for post – increment operators in C:
//Example for increment operators
#include <stdio.h>
int main()
{
int i=0;
while(i++ < 5 )
printf("%d ",i);
return 0;
Output : 1 2 3 4 5
Step 1: In this program, value of i “0” is compared with 5 in while expression.
Step 2: Then, value of “i” is incremented from 0 to 1 using post-increment operator.
Step 3: Then, this incremented value “1” is assigned to the variable “i”
Above 3 steps are continued until while expression becomes false and output is displayed as “1 2
3 4 5”.
III. Assignment Operators:
In C programs, values for the variables are assigned using assignment operators.
For example, if the value “10” is to be assigned for the variable “sum”, it can be assigned as
“sum = 10;”
There are 2 categories of assignment operators in C language. They are,
1. Simple assignment operator (Example: = )
2. Compound assignment operators (Example: +=, -=, *=, /=, %=, &=, ^= )
Operators Example/Description
sum = 10;
=
10 is assigned to variablesum
sum += 10;
+=
This is same as sum = sum +10
B.C.A: 2025-2026 F.Y. B.C.A. (SCIENCE)
sum -= 10;
-= This is same as sum = sum –10
sum *= 10;
*= This is same as sum = sum *10
sum /= 10;
/= This is same as sum = sum /10
sum %= 10;
%= This is same as sum = sum %10
sum&=10;
&=
This is same as sum = sum &10
sum ^= 10;
^=
This is same as sum = sum ^10
IV. Relational Operators:
Relational operators are used to find the relation between two variables.
i.e. to compare the values of two variables in a C program
Operators Example/Description
>
x > y (x is greater than y)
< x < y (x is less than y)
x >= y (x is greater than or
>=
equal to y)
<= x <= y (x is less than or equal
to y)
== x == y (x is equal to y)
!= x != y (x is not equal to y)
Example program for relational operators in C:
In this program, relational operator (==) is used to compare 2 values whether they are equal are
not.
If both values are equal, output is displayed as” values are equal”. Else, output is displayed as
“values are not equal”.
Note: double equal sign (==) should be used to compare 2 values.
B.C.A: 2025-2026 F.Y. B.C.A. (SCIENCE)
We should not use single equal sign (=).
#include <stdio.h>
int main()
{
int m=40, n=20;
if (m == n)
{
printf("m and n are equal");
}
else
{
printf("m and n are not equal");
}
}
Output:
m and n are not equal
V. Logical Operators:
These operators are used to perform logical operations on the given expressions.
There are 3 logical operators in C language. They are, logical AND (&&), logical OR (||) and logical NOT (!).
Operators Example/Description
(x>5)&&(y<5)
&& (logical AND)
It returns true when both conditions are true
(x>=10)||(y>=10)
|| (logical OR) It returns true when at-least one of
the condition is true
!((x>5)&&(y<5))
It reverses the state of the operand“((x>5)
! (logical NOT) && (y<5))”
If “((x>5) && (y<5))” is true, logical
NOToperator makes it false
VI. Conditional or Ternary Operators:
Conditional operators return one value if condition is true and returns another value if condition is
false. This operator is also called as ternary operator.
Syntax : (Condition? true_value: false value);
Example : (A > 100 ? 0: 1 );
B.C.A: 2025-2026 F.Y. B.C.A. (SCIENCE)
In above example, if A is greater than 100, 0 is returned else 1 is returned. This is equal to if else
conditional statement.
Example program for conditional/ternary operators in C:
#include <stdio.h>
int main ()
{
int x=1, y ;
y = (x ==1 ? 2 : 0 ) ;
printf("x value is %d\n", x);
printf("y value is %d", y);
}
Output:
x value is 1
y value is 2
VII. Bitwise Operators:
These operators are used to perform bit operations. Decimal values are converted into binary
values which are the sequence of bits and bit wise operators work on these bits.
Bit wise operators in C language are & (bitwise AND), | (bitwise OR), ~ (bitwise NOT), ^ (XOR),
<< (left shift) and >> (right shift).
TRUTH TABLE FOR BIT WISE OPERATION & BIT WISE OPERATORS:
Below are the bit-wise operators and their name in C language.
& – Bitwise AND
| – Bitwise OR
~ – Bitwise NOT
^ – XOR
<< – Left Shift
>> – Right Shift
B.C.A: 2025-2026 F.Y. B.C.A. (SCIENCE)
Consider x=40 and y=80.
Binary form of these values is given below.
x = 00101000
y = 01010000
All bit wise operations for x and y are given below.
x&y = 00000000 (binary) = 0 (decimal)
x|y = 01111000 (binary) = 120 (decimal)
~x = 11010111 = -41 (decimal)
x^y = 01111000 (binary) = 120 (decimal)
x << 1 = 01010000 (binary) = 80 (decimal)
x >> 1 = 00010100 (binary) = 20 (decimal)
VIII. Special Operators:
Below are some of the special operators that the C programming language offers.
Operators Description
This is used to get the address of the
variable.
& Example : &a will give address of a.
This is used as pointer to a variable.
Example : * a where, * is pointer to the
* variable a.
This gives the size of the variable.
Sizeof () Example : size of (char) will give us 1.
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
\b => back space
\r =>carriage return
\” => double quotations
\\ => black slash etc.
B.C.A: 2025-2026 F.Y. B.C.A. (SCIENCE)
Sample Program1:
Write a C Program to demonstrate the working of arithmetic operators.
(Hint: Do not accept input from user. Declare variables with default values)
#include <stdio.h>
int main()
{
int a = 10, b = 4, res;
// printing a and b
printf("a is %d and b is %d\n", a, b);
res = a + b; // addition
printf("a+b is %d\n", res);
res = a - b; // subtraction
printf("a-b is %d\n", res);
res = a * b; // multiplication
printf("a*b is %d\n", res);
res = a / b; // division
printf("a/b is %d\n", res);
res = a % b; // modulus
printf("a%%b is %d\n", res);
return 0;
}
Output:
a is 10 and b is 4
a+b is 14
a-b is 6
a*b is 40
a/b is 2
a%b is 2
B.C.A: 2025-2026 F.Y. B.C.A. (SCIENCE)
Sample Program2:
#include<stdio.h>
int main()
{
int num1,num2,sum,product;
printf("\tThe program takes two numbers as input and prints their sum and product");
printf("\n Enter first number:");
scanf("%d",&num1);
printf("\n Enter second number:");
scanf("%d",&num2);
sum=num1+num2;
product=num1*num2;
printf("\n%d+%d=%d",num1,num2,sum);
printf("\n%d*%d=%d",num1,num2,product);
return 0;;
}
Output:
The program takes two numbers as input and prints their sum and product
Enter first number:9
Enter second number:5
9+5=14
9*5=45
B.C.A: 2025-2026 F.Y. B.C.A. (SCIENCE)
Assignments based on Data Types and Operators:
SET A
1. WAP to find simple interest.
2. WAP to convert temperature from Celsius to Fahrenheit.
B.C.A: 2025-2026 F.Y. B.C.A. (SCIENCE)
3. WAP to calculate sum of 5 subjects and find percentage.
2. WAP to find gross
salary D.A= 10 %
of basic
T.A= 12 % basic
Gross salary = basic+ D.A.+ T.A
B.C.A: 2025-2026 F.Y. B.C.A. (SCIENCE)
SET B
1. WAP to swap two integers –
i) Using temporary variable
ii) Without Using temporary variable
a) Using + and – operator
b) Using / and *
B.C.A: 2025-2026 F.Y. B.C.A. (SCIENCE)
2) Write a C program to find maximum of two numbers using conditional operator.
3) Write a C Program to find maximum of three numbers using logical operators.
Assignment Evaluation
0: Not Done [ ] 1: Incomplete [ ] 2: Late Complete [ ]
3: Needs Improvement [ ] 4: Complete [ ] 5: Well Done [ ]
B.C.A: 2025-2026 F.Y. B.C.A. (SCIENCE)