0% found this document useful (0 votes)
9 views

c Handbook

Uploaded by

manyalegendpatel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

c Handbook

Uploaded by

manyalegendpatel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 66

Made by :- Hari patel C programming

Made by :- Hari patel C programming

Made by:- Hari Patel INDEX C Programming

No Aim
Practical Set-1 Draw a flowchart and algorithm for the
1 Find out sum of two numbers.

2 Find out average of five numbers.

3 Find out area of Circle (a=3.14*r*r)

4 Find out area of Rectangle (a=l*b)

5 Find out area of Square (a=l*l)

6 Find out area of Triangle (a=(1/2)*l*b)

7 Find out largest number from given three number.

8 Find out given number is odd or even

9 Flowchart and algorithm to Print series 1 2 3 4 5


Practical Set-2 Simple C Program
10 Write a c program to print “hello world”.

11 Write a c program to print your address.


12 Write a c program to print following pattern(Rectangle).
Practical Set-3 Write a program to declare following type of
variable and initialize each of these variables.
13 Write a C program to print Two integer type variable
14 Write a C program to print Two float type variable
15 Write a C program to print Single character type variable
16 Write a C program to print Single double type variable
Practical Set-4 Write a program to solve following type of
equations(Using Scanf)
17 Write a c program for addition.
18 Write a c program for subtraction.
Made by :- Hari patel C programming

Made by:- Hari Patel C Programming

19 Write a c program for multiplication.


20 Write a c program for division.
Practical Set-5 Write a program to solve following type
of(using Scanf)
21 Write a c program to find simple interest.((I=p*r*n)/100)
22 Write a c program to convert Celsius to Fahrenheit.
23 Write a c program to convert rupees into paisa
24 Write a c program to swap value of input variable

No. Write following program using ( Scanf )


25 Write a c program to convert kilometer to
meter,cm,feet,inch.
26 Write a c program to find area of circle.(area=3.14*r*r)
27 Write a c program to find area of rectangle.(area=l*b)
28 Write a c program to find area of triangle.(area=h*b*0.5)
29 Write a c program to interchange two numbers.
Practical Set-6 Type Casting
30 Write a C program to print ASCII value of any character.

31 Write a C program using enum keyword.


32 Write a C program for type casting the value from integer to
float
Practical Set-7 Operator
33 Write a C program to take character from user using
getchar() function and
34 Write a program to perform different operators as listed
below.
1. Arithmetic Operators
2. Assignment Operators
3. Relational Operators
4. Logical Operators

35 Write a program to find maximum number from two


numbers using conditional operator(Ternary operator).
Made by :- Hari patel C programming

Made by:- Hari Patel C Programming


36 Write a program to perform Postfix increment and Postfix
Decrement using Increment/Decrement Operators.

Practical Set-8 Decision Making with Branching


37 Write a c program to check number is even using simple if.
38 Write a c program to check number is odd using simple if.
39 Write a c program to check number is positive or not using if-
else
40 Write a c program to check number is even or odd using if-else
41 Write a c program to print day according to number entered
by user using if-else ladder.
42 Write program to check weather given input is upper,lower
digit and symbol using-if else ladder
Practical Set-9 Decision Making with Looping

No. Aim
43 Write a c program to find maximum number from three
numbers using if-else ladder.
44 Write a c program to check year is leap year or not.
45 Write a c program to make calculator using menu driven
programming using switch case.
46 Write a program to find day of the week switch case.

47 Write a c program to print series like (1,2,3,4) (2,4,6,8) using


while loop.
48 Write a program to print factorial of a given number using
while loop.
49 Write a program using while loop to sum of given number
50 Write a program print 1 to 10 natural number using do-while
loop
51 Write a program print 1 to 10 natural number using goto
statement.
52 Write a program to display sum of given number using goto
statement.
Practical Set-10 Write programs on arrays
Made by :- Hari patel C programming

Made by:- Hari Patel C Programming


53 Write a program to declare array of 5 element enter value for
5 element and
54 Write a program to display sum of 5 array elements.
Made by :- Hari patel C programming

PRACTICAL-1

AIM:-Practical Set-1 Draw a flowchart and algorithm for the


1) Find out sum of two numbers.
Ans:-

Algorithm:-

Step:1 Start
Step:2 Read input a,b;
Step:3 Sum = a + b;
Step:4 Print Sum
Step:5 End

Flowchart:-

Start

Read a,b

Sum = a,b

Print Sum

Stop
Made by :- Hari patel C programming

2) Find out average of five numbers.


Ans:-

Algorithm:-

Step:1 Start
Step:2 input a=1, b=2, c=3, d=4, e=5
Step:3 Average (a,b,c,d,e)/5;
Step:4 Print Average
Step:5 End

Flowchart:-

Start

Read a,b,c,d,e

Average=(1+2+3+4+5)/5

Print Average

Stop
Made by :- Hari patel C programming

3) Find out area of Circle (a=3.14*r*r)


Ans:-

Algorithm:-

Step:1 Start
Step:2 Read input Radius
Step:3 Area = 3.14 * R * R
Step:4 Print Area
Step:5 End

Flowchart:-

Start

Read input 3.14*R*R

Area = 3.14*R*R

Print Area

Stop
Made by :- Hari patel C programming

4) Find out area of Rectangle (a=l*b)


Ans:-

Algorithm:-

Step:1 Start
Step:2 Read input a
Step:3 a = l*B
Step:4 Print a
Step:5 End

Flowchart:-

Start

Read input l*b

a = l*b

Print a

Stop
Made by :- Hari patel C programming

5) Find out area of Square (a=l*l)


Ans:-

Algorithm:-

Step:1 Start
Step:2 Read input area
Step:3 area = l*l
Step:4 Print area
Step:5 End

Flowchart:-

Start

Read input l*l

a = l*l

Print area

Stop
Made by :- Hari patel C programming

6) Find out area of Triangle (a=(1/2)*l*b)


Ans:-

Algorithm:-

Step:1 Start
Step:2 Read input area
Step:3 area = [(1/2)*l*b]
Step:4 Print area
Step:5 End

Flowchart:-

Start

Read input l*b

area = [(1/2)*l*b]

Print area

Stop
Made by :- Hari patel C programming

7) Find out largest number from given three number.


Ans:-

Algorithm:-

Step:1 Start
Step:2 Read input a,b,c
Step:3 if a>b and if a>c So, a is max
Step:4 if b>a and if b>c So, b is max
Step:5 if c>a and if c>b So, c is max
Step:6 print x that a or b or c is max
Step7: End

Flowchart:-

Start

Read a,b,c

TRUE FALSE
a>b
TRUE FALSE FALSE TRUE
a>c b>c

A is max B is max

C is max

Stop
Made by :- Hari patel C programming

8) Find out given number is odd or even


Ans:-

Algorithm:-

Step:1 Start
Step:2 Read input number
Step:3 if (num % 2 == 0) So, number is even
Step:4 if (num % 2 == 0) So, number is odd
Step:5 print even or odd number
Step:6 End

Flowchart:-

Start

Read num

TRUE FALSE

If num%2==0

Print even num Print odd num

Stop
Made by :- Hari patel C programming

9) Flowchart and algorithm to Print series 1 2 3 4 5


Ans:-

Algorithm:-

Step:1 Start
Step:2 i=0
Step:3 print i
Step:4 i = i + 1
Step:5 if (i< = 5) then go to line 3;
Step:6 End

Flowchart:-

Start

Print i = 1

FALSE
If i<=5

TRUE
Print i
Stop

i=i +1
Made by :- Hari patel C programming

PRACTICAL-2

AIM:-Simple C Program

10) Write a c program to print “hello world”.


Ans:-

program:-

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

void main()
{
clrscr();

printf(“Hello World”);

getch();
}

Output:-

Hello World
Made by :- Hari patel C programming

11) Write a c program to print your address.


Ans:-

program:-

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

void main()
{
clrscr();

printf(“\n Address:- XYZ”);


printf(“\n Pin code:-384151”);

getch();
}

Output:-

Address:- XYZ
Pin code:-384151
Made by :- Hari patel C programming

12) Write a c program to print following pattern(Rectangle).


Ans:-

program:-

#include <conio.h>
#include <stdio.h>
void main() {

int height, width;


clrscr();

printf("Enter number of height : ");


scanf("%d", &height);
printf("Enter number of width : ");
scanf("%d", &width);

for (int i = 0; i < height; i++) {


for (int j = 0; j < width; j++) {
printf("* ");
}
printf("\n");
}

getch();
}

Output:-
Enter number of height : 3
Enter number of width : 6

******
******
******
Made by :- Hari patel C programming

PRACTICAL-3

AIM:-Write a program to declare following type of variable and initialize


each of these variables.

13) Write a C program to print Two integer type variable


Ans:-

program:-

#include <conio.h>
#include <stdio.h>
void main() {
clrscr();

int X,Y;

X=10;
Y=20;

printf(“\n The value of X: %d”,X);


printf(“\n The value of Y: %d”,Y);

getch();
}

Output:-

The value of X: 10
The value of Y: 20
Made by :- Hari patel C programming

14) Write a C program to print Two float type variable


Ans:-

program:-

#include <conio.h>
#include <stdio.h>
void main() {
clrscr();

float X,Y;

X=23.2;
Y=32.3;

printf(“\n The value of X: %f”,X);


printf(“\n The value of Y: %f”,Y);

getch();
}

Output:-

The value of X: 23.2


The value of Y: 32.3
Made by :- Hari patel C programming

15) Write a C program to print Single character type variable


Ans:-

program:-

#include <conio.h>
#include <stdio.h>
void main() {
clrscr();

char X,Y;

X=’a’;
Y=’b’;

printf(“\n The value of X: %c”,X);


printf(“\n The value of Y: %c”,Y);

getch();
}

Output:-

The value of X: a
The value of Y: b
Made by :- Hari patel C programming

16) Write a C program to print Single double type variable


Ans:-

program:-

#include <conio.h>
#include <stdio.h>
void main() {
clrscr();

double X,Y;

X= 35.676767;
Y= 26.121212;

printf(“\n The value of X: %lf”,X);


printf(“\n The value of Y: %lf”,Y);

getch();
}

Output:-

The value of X: 35.676767


The value of Y: 26.121212
Made by :- Hari patel C programming

PRACTICAL-4

AIM:- Write a program to solve following type of equation(Using Scanf)

17) Write a c program for addition.


Ans:-

program:-

#include <conio.h>
#include <stdio.h>
void main() {
clrscr();

int num1,num2,sum;

printf(“Enter the first number:”);


scanf(“%d”, & num1);
printf(“Enter the second number:”);
scanf(“%d”, & num2);

sum = num1 + num2;


printf(“The sum of %d and %d is %d”, num1,num2,sum);

getch();
}

Output:-

Enter the first number:2


Enter the second number:3
The sum of 2 and 3 is 5
Made by :- Hari patel C programming

18) Write a c program for subtraction.


Ans:-

program:-

#include <conio.h>
#include <stdio.h>
void main() {
clrscr();

int num1,num2,sub;

printf(“Enter the first number:”);


scanf(“%d”, & num1);
printf(“Enter the second number:”);
scanf(“%d”, & num2);

sub = num1 - num2;


printf(“The sub of %d and %d is %d”, num1,num2,sub);

getch();
}

Output:-

Enter the first number:2


Enter the second number:4
The sub of 2 and 4 is -2
Made by :- Hari patel C programming

19) Write a c program for multiplication.


Ans:-

program:-

#include <conio.h>
#include <stdio.h>
void main() {
clrscr();

int num1,num2,multi;

printf(“Enter the first number:”);


scanf(“%d”, & num1);
printf(“Enter the second number:”);
scanf(“%d”, & num2);

multi = num1 * num2;


printf(“The multi of %d and %d is %d”, num1,num2,multi);

getch();
}

Output:-

Enter the first number:3


Enter the second number:2
The multi of 3 and 2 is 6
Made by :- Hari patel C programming

20) Write a c program for division.


Ans:-

program:-

#include <conio.h>
#include <stdio.h>
void main() {
clrscr();

int num1,num2,div;

printf(“Enter the first number:”);


scanf(“%d”, & num1);
printf(“Enter the second number:”);
scanf(“%d”, & num2);

div = num1 / num2;


printf(“The div of %d and %d is %d”, num1,num2,div);

getch();
}

Output:-

Enter the first number: 20


Enter the second number: 2
The div of 20 and 2 is 10
Made by :- Hari patel C programming

PRACTICAL-5

AIM:- Write a program to solve following type of(using Scanf)


21) Write a c program to find simple interest.((I=p*r*n)/100)
Ans:-

program:-

#include <stdio.h>
#include <conio.h>
void main() {
float principal, rate, time,interest;
clrscr();
printf("Enter Principal Amount (P): ");
scanf("%f", &principal);
printf("Enter Rate of Interest (R): ");
scanf("%f", &rate);
printf("Enter Time Period (N in years): ");
scanf("%f", &time);
interest = (principal * rate * time) / 100;
printf("Simple Interest (I) = %.2f\n", interest);
getch();
}
Output:-

Enter Principal Amount (P): 1000


Enter Rate of Interest (R): 5
Enter Time Period (N in years): 3
Simple Interest (I) = 150.00
Made by :- Hari patel C programming

22) Write a c program to convert Celsius to Fahrenheit.


Ans:-

program:-

#include <stdio.h>
#include <conio.h>
void main() {
float celsius, fahrenheit;
clrscr();
printf("Enter temperature in Celsius: ");
scanf("%f", &celsius);
fahrenheit = (celsius * 9/5) +32;
printf("Temperature in Fahrenheit: %.2f\n", fahrenheit);
getch();
}
Output:-

Enter temperature in Celsius: 25

Temperature in Fahrenheit: 77.00


Made by :- Hari patel C programming

23)Write a c program to convert rupees into paisa


Ans:-

program:-

#include <stdio.h>
#include <conio.h>
void main() {

float rupees, paisa;


clrscr();

printf("Enter amount in Rupees:");


scanf("%f", &rupees);

paisa = rupees * 100;

printf("Amount in Paisa: %.2f\n", paisa);

getch();
}
Output:-

Enter amount in Rupees: 10


Amount in Paisa: 1000.00
Made by :- Hari patel C programming

24)Write a c program to swap value of input variable


Ans:-

program:-

#include <stdio.h>
#include <conio.h>
void main() {
int a, b, temp;
clrscr();
printf("Enter first number (a):");
scanf("%d", &a);
printf("Enter second number (b):");
scanf("%d", &b);

printf("\n Before swapping: \n");


printf("a = %d\n", a);
printf("b = %d\n", b);

temp = a;
a = b;
b = temp;
printf("\n After swapping:\n");
printf("a = %d\n", a);
printf("b = %d\n", b);
getch();
}
Output:-
Enter first number (a): 5
Enter second number (b): 10
Sample Output:
Before swapping:
a=5
b = 10
After swapping:
a = 10
b=5
Made by :- Hari patel C programming

25)Write a c program to convert kilometers to meter, cm, feet, inch.


Ans:-

program:-
#include <stdio.h>
#include <conio.h>

void main() {
float kilometers, meters, centimeters, feet, inches;

printf("Enter distance in kilometers: ");


scanf("%f", &kilometers);

meters = kilometers * 1000;


centimeters = kilometers * 100000;
feet = kilometers * 3280.84;
inches = kilometers * 39370.1;

printf("Distance in meters: %.2f m\n", meters);


printf("Distance in centimeters: %.2f cm\n", centimeters);
printf("Distance in feet: %.2f ft\n", feet);
printf("Distance in inches: %.2f in\n", inches);

getch();
}

Output:-
Enter distance in kilometers: 2.5
Distance in meters: 2500.00 m
Distance in centimeters: 250000.00 cm
Distance in feet: 8202.10 ft
Distance in inches: 98425.25 in
Made by :- Hari patel C programming

26)Write a c program to find area of circle.(area=3.14*r*r)


Ans:-

program:-
#include <stdio.h>
#include <conio.h>

void main() {
float radius, area;

printf("Enter the radius of the circle: ");


scanf("%f", &radius);

area = 3.14 * radius * radius;

printf("Area of the circle: %.2f\n", area);

getch();
}

Output:-

Enter the radius of the circle: 5


Area of the circle: 78.50
Made by :- Hari patel C programming

27)Write a c program to find area of rectangle.(area=l*b)


Ans:-

program:-
#include <stdio.h>
#include <conio.h>

void main() {
float length, breadth, area;

printf("Enter the length of the rectangle: ");


scanf("%f", &length);

printf("Enter the breadth of the rectangle: ");


scanf("%f", &breadth);

area = length * breadth;

printf("Area of the rectangle: %.2f\n", area);

getch();
}

Output:-
Enter the length of the rectangle: 5
Enter the breadth of the rectangle: 3
Area of the rectangle: 15.00
Made by :- Hari patel C programming

28)Write a c program to find area of triangle.(area=h*b*0.5)


Ans:-

program:-
#include <stdio.h>
#include <conio.h>

void main() {
float height, base, area;

printf("Enter the height of the triangle: ");


scanf("%f", &height);

printf("Enter the base of the triangle: ");


scanf("%f", &base);

area = 0.5 * height * base;

printf("Area of the triangle: %.2f\n", area);

getch();
}

Output:-

Enter the height of the triangle: 4


Enter the base of the triangle: 6
Area of the triangle: 12.00
Made by :- Hari patel C programming

29)Write a c program to interchange two numbers.


Ans:-

program:-
#include <stdio.h>
#include <conio.h>

void main() {
int a, b, temp;

printf("Enter the first number (a): ");


scanf("%d", &a);

printf("Enter the second number (b): ");


scanf("%d", &b);

// Swapping logic
temp = a;
a = b;
b = temp;

printf("After swapping:\n");
printf("a = %d\n", a);
printf("b = %d\n", b);

getch();
}

Output:-

Enter the first number (a): 5


Enter the second number (b): 10
After swapping:
a = 10
b=5
Made by :- Hari patel C programming

PRACTICAL-6

AIM:- type casting


30) Write a C program to print ASCII value of any character.
Ans:-

program:-

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

void main() {
char ch;

printf("Enter a character: ");


scanf("%c", &ch);

printf("The ASCII value of '%c' is %d\n", ch, ch);

getch();
}
Output:-

Enter a character: A
The ASCII value of 'A' is 65
Made by :- Hari patel C programming

31) Write a C program using enum keyword.


Ans:-

program:-
#include <stdio.h>
#include <conio.h>

enum Days { Sunday = 1, Monday, Tuesday, Wednesday,


Thursday, Friday, Saturday };

void main() {
int day;

printf("Enter a day (1-7) where 1=Sunday, 2=Monday, ...,


7=Saturday: ");
scanf("%d", &day);

if (day == Sunday) {
printf("You entered: Sunday\n");
} else if (day == Monday) {
printf("You entered: Monday\n");
} else if (day == Tuesday) {
printf("You entered: Tuesday\n");
} else if (day == Wednesday) {
printf("You entered: Wednesday\n");
} else if (day == Thursday) {
printf("You entered: Thursday\n");
} else if (day == Friday) {
printf("You entered: Friday\n");
Made by :- Hari patel C programming

} else if (day == Saturday) {


printf("You entered: Saturday\n");
} else {
printf("Invalid input! Please enter a number between 1 and
7.\n");
}

getch();
}
Output:-

Enter a day (1-7) where 1=Sunday, 2=Monday, ..., 7=Saturday: 5


You entered: Thursday
Made by :- Hari patel C programming

32) Write a C program for type casting the value from integer to float
Ans:-

program:-
#include <stdio.h>
#include <conio.h>

void main() {
int intValue;
float floatValue;

printf("Enter an integer value: ");


scanf("%d", &intValue);

// Type casting from integer to float


floatValue = (float)intValue;

printf("The integer value: %d\n", intValue);


printf("The float value after type casting: %.2f\n", floatValue);

getch();
}

Output:-

Enter an integer value: 10


The integer value: 10
The float value after type casting: 10.00
Made by :- Hari patel C programming

PRACTICAL-7

AIM:-Operator
33) Write a C program to take character from user using getchar() function
and
Ans:-
program:-
#include <stdio.h>
#include <conio.h>

void main() {
char ch;

printf("Enter a character: ");


ch = getchar(); // Get a character from user input

printf("You entered: '%c'\n", ch);


printf("The ASCII value of '%c' is %d\n", ch, ch);

getch();
}

Output:-

Enter a character: A
You entered: 'A'
The ASCII value of 'A' is 65
Made by :- Hari patel C programming

34) Write a program to perform different operators as listed below.


1.Arithmetic Operators
2.Assignment Operators
3.Relational Operators
4.Logical Operators
Ans:-
program:-
#include <stdio.h>
#include <conio.h>

void main() {
int a, b;
int result;

// Input two integers


printf("Enter two integers:\n");
printf("a: ");
scanf("%d", &a);
printf("b: ");
scanf("%d", &b);

// 1. Arithmetic Operators
printf("\nArithmetic Operators:\n");
printf("a + b = %d\n", a + b);
printf("a - b = %d\n", a - b);
printf("a * b = %d\n", a * b);
printf("a / b = %d\n", a / b);
printf("a %% b = %d\n", a % b);

// 2. Assignment Operators
printf("\nAssignment Operators:\n");
Made by :- Hari patel C programming

result = a; // Simple assignment


printf("result = a: %d\n", result);
result += b; // Add and assign
printf("result += b: %d\n", result);
result -= a; // Subtract and assign
printf("result -= a: %d\n", result);
result *= 2; // Multiply and assign
printf("result *= 2: %d\n", result);
result /= b; // Divide and assign
printf("result /= b: %d\n", result);

// 3. Relational Operators
printf("\nRelational Operators:\n");
printf("a == b: %d\n", a == b);
printf("a != b: %d\n", a != b);
printf("a > b: %d\n", a > b);
printf("a < b: %d\n", a < b);
printf("a >= b: %d\n", a >= b);
printf("a <= b: %d\n", a <= b);

// 4. Logical Operators
printf("\nLogical Operators:\n");
printf("a > 0 && b > 0: %d\n", (a > 0 && b > 0));
printf("a > 0 || b > 0: %d\n", (a > 0 || b > 0));
printf("!(a == b): %d\n", !(a == b));

getch();
}
Made by :- Hari patel C programming

Output:-

Enter two integers:


a: 10
b: 5

Arithmetic Operators:
a + b = 15
a-b=5
a * b = 50
a/b=2
a%b=0

Assignment Operators:
result = a: 10
result += b: 15
result -= a: 5
result *= 2: 10
result /= b: 2

Relational Operators:
a == b: 0
a != b: 1
a > b: 1
a < b: 0
a >= b: 1
a <= b: 0

Logical Operators:
a > 0 && b > 0: 1
a > 0 || b > 0: 1
!(a == b): 1
Made by :- Hari patel C programming

35) Write a program to find maximum number from two numbers using
conditional operator(Ternary operator).
Ans:-
program:-
#include <stdio.h>
#include <conio.h>

void main() {
int a, b, max;

printf("Enter two numbers:\n");


printf("a: ");
scanf("%d", &a);
printf("b: ");
scanf("%d", &b);

// Using conditional operator to find the maximum


max = (a > b) ? a : b;

printf("The maximum number is: %d\n", max);

getch();
}
Output:-

Enter two numbers:


a: 10
b: 20
The maximum number is: 20
Made by :- Hari patel C programming

36) Write a program to perform Postfix increment and Postfix Decrement


using Increment/Decrement Operators.
Ans:-
program:-
#include <stdio.h>
#include <conio.h>

void main() {
int a, b;

// Initialize variables
printf("Enter an integer: ");
scanf("%d", &a);

b = a++; // Postfix increment: uses a then increments


printf("After Postfix Increment:\n");
printf("Value of a: %d\n", a); // a is incremented
printf("Value of b: %d (value of a before increment)\n\n", b);

b = a--; // Postfix decrement: uses a then decrements


printf("After Postfix Decrement:\n");
printf("Value of a: %d\n", a); // a is decremented
printf("Value of b: %d (value of a before decrement)\n", b);

getch();
}
Output:-
Enter an integer: 5
After Postfix Increment:
Value of a: 6
Value of b: 5 (value of a before increment)

After Postfix Decrement:


Value of a: 5
Value of b: 6 (value of a before decrement)
Made by :- Hari patel C programming

PRACTICAL-8

AIM:-Decision Making with Branching


37) Write a c program to check number is even using simple if.
Ans:-
program:-
#include <stdio.h>
#include <conio.h>

void main() {
int num;

printf("Enter an integer: ");


scanf("%d", &num);

// Check if the number is even


if (num % 2 == 0) {
printf("%d is an even number.\n", num);
} else {
printf("%d is an odd number.\n", num);
}

getch();
}

Output:-

Enter an integer: 4
4 is an even number.
Made by :- Hari patel C programming

38) Write a c program to check number is odd using simple if.


Ans:-
program:-
#include <stdio.h>
#include <conio.h>

void main() {
int num;

printf("Enter an integer: ");


scanf("%d", &num);

// Check if the number is odd


if (num % 2 != 0) {
printf("%d is an odd number.\n", num);
} else {
printf("%d is an even number.\n", num);
}

getch();
}

Output:-
Enter an integer: 7
7 is an odd number.
Made by :- Hari patel C programming

39) Write a c program to check number is positive or not using if- else
Ans:-
program:-
#include <stdio.h>
#include <conio.h>

void main() {
int num;

printf("Enter an integer: ");


scanf("%d", &num);

// Check if the number is positive


if (num > 0) {
printf("%d is a positive number.\n", num);
} else {
printf("%d is not a positive number.\n", num);
}

getch();
}
Output:-

Enter an integer: 5
5 is a positive number.
Made by :- Hari patel C programming

40) Write a c program to check number is even or odd using if-else


Ans:-
program:-
#include <stdio.h>
#include <conio.h>

void main() {
int num;

printf("Enter an integer: ");


scanf("%d", &num);

// Check if the number is even or odd


if (num % 2 == 0) {
printf("%d is an even number.\n", num);
} else {
printf("%d is an odd number.\n", num);
}

getch();
}
Output:-

Enter an integer: 8
8 is an even number.
Made by :- Hari patel C programming

41) Write a c program to print day according to number entered by user using
if-else ladder.
Ans:-
program:-
#include <stdio.h>
#include <conio.h>

void main() {
int day;

printf("Enter a number (1-7) to get the corresponding day of the


week:\n");
scanf("%d", &day);

// Using if-else ladder to determine the day


if (day == 1) {
printf("Day 1 is Sunday.\n");
} else if (day == 2) {
printf("Day 2 is Monday.\n");
} else if (day == 3) {
printf("Day 3 is Tuesday.\n");
} else if (day == 4) {
printf("Day 4 is Wednesday.\n");
} else if (day == 5) {
printf("Day 5 is Thursday.\n");
} else if (day == 6) {
printf("Day 6 is Friday.\n");
} else if (day == 7) {
printf("Day 7 is Saturday.\n");
} else {
Made by :- Hari patel C programming

printf("Invalid input! Please enter a number between 1 and


7.\n");
}

getch();
}

Output:-

Enter a number (1-7) to get the corresponding day of the week:


3
Day 3 is Tuesday.
Made by :- Hari patel C programming

42) Write program to check weather given input is upper,lower digit and
symbol using-if else ladder
Ans:-
program:-
#include <stdio.h>
#include <conio.h>

void main() {
char ch;

printf("Enter a character: ");


scanf("%c", &ch);

// Using if-else ladder to check the type of character


if (ch >= 'A' && ch <= 'Z') {
printf("'%c' is an uppercase letter.\n", ch);
} else if (ch >= 'a' && ch <= 'z') {
printf("'%c' is a lowercase letter.\n", ch);
} else if (ch >= '0' && ch <= '9') {
printf("'%c' is a digit.\n", ch);
} else {
printf("'%c' is a symbol or special character.\n", ch);
}

getch();
}
Output:-

Enter a character: G
'G' is an uppercase letter.
Made by :- Hari patel C programming

PRACTICAL-9

AIM:-Decision Making with Looping


43) Write a c program to find maximum number from three numbers
using if-else ladder.
Ans:-
program:-
#include <stdio.h>
#include <conio.h>

void main() {
int num1, num2, num3;

printf("Enter three numbers:\n");


printf("Number 1: ");
scanf("%d", &num1);
printf("Number 2: ");
scanf("%d", &num2);
printf("Number 3: ");
scanf("%d", &num3);

// Using if-else ladder to find the maximum number


if (num1 >= num2 && num1 >= num3) {
printf("The maximum number is: %d\n", num1);
} else if (num2 >= num1 && num2 >= num3) {
printf("The maximum number is: %d\n", num2);
} else {
printf("The maximum number is: %d\n", num3);
}

getch();
}
Output:-

Enter three numbers:


Number 1: 10
Number 2: 25
Number 3: 15
The maximum number is: 25
Made by :- Hari patel C programming

44) Write a c program to check year is leap year or not.


Ans:-
program:-
#include <stdio.h>
#include <conio.h>

void main() {
int year;

printf("Enter a year: ");


scanf("%d", &year);

// Check if the year is a leap year


if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
printf("%d is a leap year.\n", year);
} else {
printf("%d is not a leap year.\n", year);
}

getch();
}
Output:-

Enter a year: 2024


2024 is a leap year.
Made by :- Hari patel C programming

45) Write a c program to make calculator using menu driven programming


using switch case.
Ans:-
program:-
#include <stdio.h>
#include <conio.h>

void main() {
char choice;
float num1, num2, result;

do {
// Display the menu
printf("\nSimple Calculator\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
printf("5. Exit\n");
printf("Enter your choice (1-5): ");
scanf(" %c", &choice);

// Perform operations based on user choice


if (choice >= '1' && choice <= '4') {
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
}

switch (choice) {
case '1':
result = num1 + num2;
printf("Result: %.2f + %.2f = %.2f\n", num1, num2, result);
break;
case '2':
result = num1 - num2;
printf("Result: %.2f - %.2f = %.2f\n", num1, num2, result);
break;
case '3':
result = num1 * num2;
printf("Result: %.2f * %.2f = %.2f\n", num1, num2, result);
Made by :- Hari patel C programming

break;
case '4':
if (num2 != 0) {
result = num1 / num2;
printf("Result: %.2f / %.2f = %.2f\n", num1, num2, result);
} else {
printf("Error: Division by zero is not allowed.\n");
}
break;
case '5':
printf("Exiting the calculator. Goodbye!\n");
break;
default:
printf("Invalid choice! Please select a valid option.\n");
}
} while (choice != '5');

getch();
}
Output:-

Simple Calculator
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter your choice (1-5): 1
Enter two numbers: 10 20
Result: 10.00 + 20.00 = 30.00

Simple Calculator
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter your choice (1-5): 5
Exiting the calculator. Goodbye!
Made by :- Hari patel C programming

46)Write a program to find day of the week switch case.


Ans:-
program:-
#include <stdio.h>
#include <conio.h>

void main() {
int day;

printf("Enter a number (1-7) to get the corresponding day of the


week:\n");
scanf("%d", &day);

// Using switch case to determine the day


switch (day) {
case 1:
printf("Day 1 is Sunday.\n");
break;
case 2:
printf("Day 2 is Monday.\n");
break;
case 3:
printf("Day 3 is Tuesday.\n");
break;
case 4:
printf("Day 4 is Wednesday.\n");
break;
case 5:
printf("Day 5 is Thursday.\n");
break;
case 6:
printf("Day 6 is Friday.\n");
break;
case 7:
Made by :- Hari patel C programming

printf("Day 7 is Saturday.\n");
break;
default:
printf("Invalid input! Please enter a number between 1 and 7.\
n");
break;
}

getch();
}
Output:-

Enter a number (1-7) to get the corresponding day of the week: 3


Day 3 is Tuesday.
Made by :- Hari patel C programming

47)Write a c program to print series like (1,2,3,4) (2,4,6,8) using while loop.
Ans:-
program:-
#include <stdio.h>
#include <conio.h>

void main() {
int i = 1;

// Printing the first series (1, 2, 3, 4)


printf("First Series: ");
while (i <= 4) {
printf("%d", i);
if (i < 4) {
printf(", "); // Print a comma after each number except the last
}
i++;
}

printf("\n");

// Resetting i for the second series


i = 2;

// Printing the second series (2, 4, 6, 8)


printf("Second Series: ");
while (i <= 8) {
printf("%d", i);
if (i < 8) {
printf(", "); // Print a comma after each number except the last
}
i += 2; // Increment by 2 for the even numbers
}

printf("\n");

getch();
}
Output:-

First Series: 1, 2, 3, 4
Second Series: 2, 4, 6, 8
Made by :- Hari patel C programming

48)Write a program to print factorial of a given number using while loop


Ans:-
program:-
#include <stdio.h>
#include <conio.h>

void main() {
int num;
unsigned long long factorial = 1; // Use long long to handle larger factorials

printf("Enter a positive integer: ");


scanf("%d", &num);

// Check if the input is negative


if (num < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
int i = 1;

// Calculate factorial using a while loop


while (i <= num) {
factorial *= i; // Multiply factorial by i
i++; // Increment i
}

printf("Factorial of %d = %llu\n", num, factorial);


}

getch();
}
Output:-

Enter a positive integer: 5


Factorial of 5 = 120
Made by :- Hari patel C programming

49)Write a program using while loop to sum of given number


Ans:-
program:-
#include <stdio.h>
#include <conio.h>

void main() {
int num, sum = 0;

printf("Enter a positive integer: ");


scanf("%d", &num);

// Check if the input is negative


if (num < 0) {
printf("Please enter a positive integer.\n");
} else {
// Calculate the sum of the digits using a while loop
while (num != 0) {
sum += num % 10; // Add the last digit to sum
num /= 10; // Remove the last digit
}

printf("Sum of the digits = %d\n", sum);


}

getch();
}
Output:-
Enter a positive integer: 12345
Sum of the digits = 15
Made by :- Hari patel C programming

50)Write a program print 1 to 10 natural number using do-while loop


Ans:-
program:-
#include <stdio.h>
#include <conio.h>

void main() {
int i = 1;

// Using a do-while loop to print numbers from 1 to 10


do {
printf("%d\n", i); // Print the current number
i++; // Increment i
} while (i <= 10); // Continue while i is less than or equal to 10

getch();
}
Output:-

1
2
3
4
5
6
7
8
9
10
Made by :- Hari patel C programming

51)Write a program print 1 to 10 natural number using goto statement.


Ans:-
program:-
#include <stdio.h>
#include <conio.h>

void main() {
int i = 1; // Initialize counter

start: // Label to jump to


if (i <= 10) {
printf("%d\n", i); // Print the current number
i++; // Increment i
goto start; // Jump back to the start label
}

getch();
}
Output:-

1
2
3
4
5
6
7
8
9
10
Made by :- Hari patel C programming

52)Write a program to display sum of given number using goto statement.


Ans:-
program:-
#include <stdio.h>
#include <conio.h>

void main() {
int num, sum = 0;

printf("Enter a positive integer: ");


scanf("%d", &num);

// Check if the input is negative


if (num < 0) {
printf("Please enter a positive integer.\n");
getch();
return;
}

start: // Label to jump to


if (num != 0) {
sum += num % 10; // Add the last digit to sum
num /= 10; // Remove the last digit
goto start; // Jump back to the start label
}

printf("Sum of the digits = %d\n", sum);


getch();
}
Output:-
Enter a positive integer: 12345
Sum of the digits = 15
Made by :- Hari patel C programming

PRACTICAL-10

AIM:-Write programs on arrays


53) Write a program to declare array of 5 element enter value for 5 element
Ans:-
program:-
#include <stdio.h>
#include <conio.h>

void main() {
int array[5]; // Declare an array of 5 elements
int i;

// Prompt user to enter values for the array


printf("Enter values for 5 elements:\n");
for (i = 0; i < 5; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &array[i]); // Store the entered value in the array
}

// Display the entered values


printf("\nThe entered values are:\n");
for (i = 0; i < 5; i++) {
printf("Element %d: %d\n", i + 1, array[i]);
}

getch();
}
Made by :- Hari patel C programming

Output:-

Enter values for 5 elements:


Element 1: 10
Element 2: 20
Element 3: 30
Element 4: 40
Element 5: 50

The entered values are:


Element 1: 10
Element 2: 20
Element 3: 30
Element 4: 40
Element 5: 50
Made by :- Hari patel C programming

54)Write a program to display sum of 5 array elements.


Ans:-
program:-
#include <stdio.h>
#include <conio.h>

void main() {
int array[5]; // Declare an array of 5 elements
int sum = 0; // Variable to store the sum
int i;

// Prompt user to enter values for the array


printf("Enter values for 5 elements:\n");
for (i = 0; i < 5; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &array[i]); // Store the entered value in the array
}

// Calculate the sum of the array elements


for (i = 0; i < 5; i++) {
sum += array[i]; // Add each element to the sum
}

// Display the sum


printf("\nThe sum of the array elements is: %d\n", sum);

getch();
}
Output:-
Enter values for 5 elements:
Element 1: 10
Element 2: 20
Element 3: 30
Element 4: 40
Element 5: 50

The sum of the array elements is: 150

You might also like