0% found this document useful (0 votes)
12 views9 pages

C Programming 2 and 3

The document outlines various C programming exercises, including the use of operators and expressions, control statements, and character handling. It provides detailed algorithms and sample code for tasks such as demonstrating logical, relational, arithmetic, assignment, increment/decrement, and bitwise operations, as well as finding the largest of three numbers and checking if a number is odd or even. Additionally, it includes a simple calculator implementation using switch-case statements.

Uploaded by

snethra017
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)
12 views9 pages

C Programming 2 and 3

The document outlines various C programming exercises, including the use of operators and expressions, control statements, and character handling. It provides detailed algorithms and sample code for tasks such as demonstrating logical, relational, arithmetic, assignment, increment/decrement, and bitwise operations, as well as finding the largest of three numbers and checking if a number is odd or even. Additionally, it includes a simple calculator implementation using switch-case statements.

Uploaded by

snethra017
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/ 9

Ex.No.

2 OPERATORS AND EXPRESSION IN C

Write a C program to show the results using printf() statement for the operations
given below from 1 to 6 in a single program.
a) logical operator
(a==b) && (c<b)
!(a!=b)|| (c<b)
b) Relational operaor
x>=y
c) Arithmetic Operator
d) Assignment operator
e) Increment and Decrement operator
f) Bitwise operator

Aim: To Write a C program to show the results using printf() statement for the logical, relational,
arithmetic, assignment, increment and decrement, bitwise operator.

Algorithm:
Step 1: Start
Step 2: Declare variables of different type as needed.
Step 3: Get the input values for the variables to demonstrate operations.
Step 4: Perform addition, subtraction, multiplication, division and modulus operations for arithmetic
operators.
Step 5: Comapare variables using >,<, >=,<=, ==, and != to demonstrate relational operators.
Step 6: Use Logical && , ||, ! to demonstrate logical operators.
Step 7: Use Assignment operator == for performing assignment operation.
Step 8: Use ++,-- operators for performing pre increment, post increment, pre decrement and post
decrement operation.
Step 9: Use Bitwise AND, OR, XOR, NOT, left shift, right shift for performing bitwise operations. Step
10: Print the result of each operation.
Step 11: Stop

Program:
#include<stdio.h>
#include<conio.h>

int main()
{
int a = 5, b = 10, c = 8;
int x = 15, y = 20;
int num = 6;
int p = 6, q = 3;
int result = 0;
clrscr();
// Logical operations
printf("Logical Operations:\n");
printf("(a == b) && (c < b) = %d\n", (a == b) && (c < b));
printf("!(a != b) || (c < b) = %d\n", !(a != b) || (c < b));

// Relational operations
printf("\nRelational Operations:\n");
printf("x >= y = %d\n", x >= y);
printf("x < y = %d\n", x < y);

Department of CSE, BIHER 5


// Arithmetic operations
printf("\nArithmetic Operations:\n");
printf("a + b = %d\n", a + b);
printf("b - c = %d\n", b - c);
printf("a * c = %d\n", a * c);
printf("b / a = %d\n", b / a);
printf("b %% a = %d\n", b % a);

// Assignment operator
printf("\nAssignment Operations:\n");
result = a + b;
printf("result = a + b; result = %d\n", result);

// Increment and Decrement operators


printf("\nIncrement and Decrement Operations:\n");
printf("num = %d\n", num);
printf("num++ = %d\n", num++);
printf("After num++: num = %d\n", num);
printf("++num = %d\n",
++num);
printf("num-- = %d\n", num--);
printf("After num--: num = %d\n", num);
printf("--num = %d\n", -- num);

// Bitwise operations
printf("\nBitwise Operations:\n");
printf("p & q = %d\n", p & q); // AND
printf("p | q = %d\n", p | q); // OR
printf("p ^ q = %d\n", p ^ q); // XOR
printf("~p = %d\n", ~p); // NOT
printf("p << 1 = %d\n", p << 1); // Left shift
printf("p >> 1 = %d\n", p >> 1); // Right shift
getch();
return 0;
}

Output :
Logical Operations:
(a == b) && (c < b) = 0
!(a != b) || (c < b) = 1
Relational Operations:
x >= y = 0 x < y = 1
Arithmetic Operations:
a + b = 15
b-c=2
a * c = 40
b/a=2b%a=0
Assignment Operations:
result = a + b; result = 15
Department of CSE, BIHER 6
Increment and Decrement Operations: num = 6
num++ = 6 After
num++: num = 7
++num = 8
num-- = 8
After num-- :num = 7
--num = 6
Bitwise Operations:
p&q=2
p|q=7
p^q=5
~p = -7
p << 1 = 12
p >> 1 = 3

RESULT:
Thus, the above C program for various operator functions was written, executed and verified
successfully.

Department of CSE, BIHER 7


Ex. No. 3a CONTROL STATEMENTS DECISION MAKING AND
BRANCHING STATEMENTS

Write a C program to find the biggest of three numbers using ladder if else
statement.

AIM: To Write a C program to find the biggest of three numbers using ladder if else statement.

ALGORITHM:
Step 1:Start
Step 2:Declare three variables with name a, b, c, largest Step
3:Display “Enter three numbers:”

Step 5:If a is greater than both b and c, set largest = a


Step 6:Else if b is greater than both a and c, set largest = b Step
7:Else, set largest = c
Step 8:Display “Largest number is: largest”
Step 9:End

PROGRAM:

#include<stdio.h>
#include<conio.h> int
main()
{
int a,b,c;
clrscr();
printf("\nEnter a,b, and c\t");
scanf(“%d%d%d”, &a,&b,&c);
if((a>b)&&(a>c))
printf(“ The number a=%d is big”,a);
else if((b>a)&&(b>c))
printf(“ The number b=%d is big”,b);
else
printf(“ The number c=%d is big”,c);
getch();
return 0;
}

Department of CSE, BIHER 8


OUTPUT:

Enter a,b, and c 10


73

The number a=10 is big

RESULT:
Thus, the C program for finding biggest of three numbers using ladder if else statement was written,
executed and verified successfully.

Department of CSE, BIHER 9


EX. NO-3b SIMPLE IF STATEMENTS

Write a C program to check whether a number is odd or even using 2 simple if statements.

AIM:
To write a C program to check whether the given number is odd or even.

ALGORITHM:

1. Start the program.


2. Declare the variable num as integer.
3. Get the input from the user.
4. Check the number as odd or even using mod(%) operator.
5. Print the output
6. Stop the program.

PROGRAM:

#include <stdio.h>
#include <conio.h>
int main()
{
int num;
clrscr();
printf("\nEnter a number:\t");
scanf(“%d”, &num);
if(num%2==0)
{
printf(“ \nThe number is even”);
}
If(num%2!=0)
{
printf(“ \nThe number is odd”);
}
getch();
return 0;
}

OUTPUT:
Enter a number: 4
The number is even

RESULT:
Thus the C Program to check whether the given number is odd or even, was written, executed and verified
successfully.

Department of CSE, BIHER 10


Ex.No-3c CHARACTER HANDLING
Write a C program to find whether entered value is character or not.
AIM:
To write a C program to check the entered input is character or not.
ALGORITHM:
1. Start the program
2. Declare a variable as char.
3. Get the input from the user.
4. Check the entered character lies between lowercase a to z or in uppercase A to Z using IF condition.
5. Print the result.
6. Stop the program.
PROGRAM:
#include <stdio.h>
#include <conio.h>
int main()
{
char ch;
clrscr();
printf("\nEnter a character\t");
scanf(“ %c”, &ch);
if(ch>=’a’ && ch<=’z’)
printf(“ \nThe entered value is a lower case letter”);
if(ch>=’A’ && ch<=’Z’)
printf(“ \nThe entered value is a upper case letter”);
if(ch>=’0’ && ch<=’9’)
printf(“ \nThe entered value is a number”);
getch();
return 0;
}
OUTPUT:
Enter a character d
The entered value is a lower case letter
Enter a character L
The entered value is a upper case letter
Enter a character 7
The entered value is a number

RESULT:
Thus the C program to check the entered value as character or not was written, executed and verified
successfully.

Department of CSE, BIHER 11


Ex.No-3d SWITCH CASE STATEMENT

Write a C program to implement simple calculator using switch case statement

AIM:
To write a C program to implement simple calculator using switch case statement.

ALGORITHM:

1. Start the program.


2. Declare the variables as integer.
3. Get the option and values from the user.
4. Depending upon the option, do the calculation using switch case statement.
5. Print the output.
6. Stop the program.

PROGRAM:

#include <stdio.h>
#include <conio.h>
int main()
{
int opt,a,b,c;
clrscr();
printf("1. Addition \n");
printf("2. Subtraction \n");
printf("3. Multiplication \n");
printf("4. Division \n");
printf("5. Modulus \n");
printf("EXIT \n");
printf("\tEnter an option\t");
scanf(“%d”, &opt);
printf("\nEnter a and b values:\t");
scanf(“%d%d”, &a,&b);
switch(opt)
{
case 1: c=a+b;
printf(“\nAddition a and b is %d”,c);
break;
case 2: c=a-b;
printf(“\nSubtraction of a and b is %d”,c);
break;
case 3: c=a*b;
printf(“\nMultiplication of a and b is %d”,c);
break;
Department of CSE, BIHER 12
case 4: c=a/b;
printf(“\nDivision of a and b is %d”,c);
break;
case 5: c=a%b;
printf(“\nRemainder of a and b is %d”,c);
break;
default: printf(“\nWrong option No operation You exit”);
}
getch();
return 0;
}

OUTPUT:
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulus
EXIT
enter an option 2
enter a and b 11 9
Subtraction of a and b is 2

RESULT:

Thus the C program using switch-case statements for mathematical calculations was written, executed and
verified successfully.

Department of CSE, BIHER 13

You might also like