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

CYCLE 1 GE23121- C PROGRAMMING LAB MANUAL AI & DS

important notes for college students especially for 1st year students

Uploaded by

nidhin.cs0607
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

CYCLE 1 GE23121- C PROGRAMMING LAB MANUAL AI & DS

important notes for college students especially for 1st year students

Uploaded by

nidhin.cs0607
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 29

EX.

NO:1A INPUT & OUTPUT OPERATIONS

DATE:

AIM:

To write C Program to take a user's name and age as input and display a personalized greeting

to demonstrate input and output statements.

ALGORITHM:

 Start the program.


 Declare variables: name and age.
 Get input from the user.
 Display the inputs
 End the program.

PROGRAM:

#include <stdio.h>
#include <string.h>
int main()
{
char name[50];
int age;
printf("Enter your name: ");
scanf("%s", &name);
printf("Enter your age: ");
scanf("%d", &age);
printf("\nHello, %s! You are %d years old.\n", name, age);
return 0;
}
OUTPUT:

Enter your name: RamKumar


Enter your age: 20

Hello, RamKumar! You are 20 years old.

RESULT:

Thus the C Program to take a user's name and age as input and display a personalized greeting

has been written & executed successfully.


EX.NO:1B OPERATORS

DATE

AIM

To write C Program to perform basic arithmetic operations. Logical operations, Bitwise Operations and
ternary operator

ALGORITHM

1. Start.

2. Declare variables for two numbers (num1, num2), and other required variables (result, max).

3. Get Two Numbers to perform operations from the user.

4. Perform Arithmetic Operations: like Add, subtract, multiply, and divide the two numbers.

5.Perform Logical Operations by using logical operators (&&, ||, !) on two predefined boolean values (e.g., 1
and 0).

6.Perform Bitwise Operations: Using bitwise operators (&, |, ^, <<, >>) on num1 and num2.

7. Perform Ternary Operator: And Find the greater number using the ternary operator.

11.Display the result.

12.Stop

PROGRAM

#include <stdio.h>
int main()
{
int a, b;
int result;
int choice;
int x, y;
printf("Enter two integers for arithmetic and bitwise operations: ");
scanf("%d %d", &a, &b);
printf("\nChoose an operation to perform:\n");
printf("1. Arithmetic Operations\n");
printf("2. Bitwise Operations\n");
printf("3. Logical Operations\n");
printf("4. Ternary Operator\n");
printf("Enter your choice (1-4): ");
scanf("%d", &choice);

if (choice == 1)
{
printf("\n--- Arithmetic Operations ---\n");
printf("Addition (a + b): %d\n", a + b);
printf("Subtraction (a - b): %d\n", a - b);
printf("Multiplication (a * b): %d\n", a * b);
if (b != 0) {
printf("Division (a / b): %d\n", a / b);
printf("Modulus (a %% b): %d\n", a % b);
}
else
{
printf("Division and Modulus operations cannot be performed (division by zero).\n");
}
}
else if (choice == 2)
{
printf("\n--- Bitwise Operations ---\n");
printf("Bitwise AND (a & b): %d\n", a & b);
printf("Bitwise OR (a | b): %d\n", a | b);
printf("Bitwise XOR (a ^ b): %d\n", a ^ b);
printf("Left Shift (a << 1): %d\n", a << 1);
printf("Right Shift (a >> 1): %d\n", a >> 1);
}
else if (choice == 3)
{
printf("\nEnter two boolean values (0 or 1) for logical operations: ");
scanf("%d %d", &x, &y);
printf("\n--- Logical Operations ---\n");
printf("Logical AND (x && y): %d\n", x && y);
printf("Logical OR (x || y): %d\n", x || y);
printf("Logical NOT (!x): %d\n", !x);
}
else if (choice == 4)
{
printf("\n--- Ternary Operator ---\n");
result = (a > b) ? a : b;
printf("Maximum of a and b: %d\n", result);
}
else {
printf("Invalid choice! Please select a valid operation.\n");
}

return 0;
}

OUTPUT
Enter two integers for arithmetic and bitwise operations: 10 2

Choose an operation to perform:


1. Arithmetic Operations
2. Bitwise Operations
3. Logical Operations
4. Ternary Operator
Enter your choice (1-4): 1

--- Arithmetic Operations ---


Addition (a + b): 12
Subtraction (a - b): 8
Multiplication (a * b): 20
Division (a / b): 5
Modulus (a % b): 0

Enter two integers for arithmetic and bitwise operations: 3 6

Choose an operation to perform:


1. Arithmetic Operations
2. Bitwise Operations
3. Logical Operations
4. Ternary Operator
Enter your choice (1-4): 2

--- Bitwise Operations ---


Bitwise AND (a & b): 2
Bitwise OR (a | b): 7
Bitwise XOR (a ^ b): 5
Left Shift (a << 1): 6
Right Shift (a >> 1): 1

Enter two integers for arithmetic and bitwise operations: 3 6

Choose an operation to perform:


1. Arithmetic Operations
2. Bitwise Operations
3. Logical Operations
4. Ternary Operator
Enter your choice (1-4): 3

Enter two boolean values (0 or 1) for logical operations: 4 6

--- Logical Operations ---


Logical AND (x && y): 1
Logical OR (x || y): 1
Logical NOT (!x): 0

Enter two integers for arithmetic and bitwise operations: 3 6

Choose an operation to perform:


1. Arithmetic Operations
2. Bitwise Operations
3. Logical Operations
4. Ternary Operator
Enter your choice (1-4): 4

--- Ternary Operator ---


Maximum of a and b: 6

RESULT
Thus the C Program to perform basic arithmetic operations. Logical operations,

Bitwise Operations and ternary operator executed successfully.


EX.NO:1C SQUARE AND CUBE

DATE

AIM
To write a C Program to input a number and calculate its square and cube. Display the results.

ALGORITHM

1. Start.
2. Declare a variable num to store the input number and variables square and cube to store the results.
3. Prompt the user to enter a number.
4. Read the number and store it in num.
5. Calculate the square of the number: square = num * num.
6. Calculate the cube of the number: cube = num * num * num.
7. Display the calculated square and cube.
8. End.

PROGRAM
#include <stdio.h>
int main()
{
double num, square, cube;
printf("Enter a number: ");
scanf("%lf", &num);
square = num * num;
cube = num * num * num;
printf("\nThe square of %.2lf is: %.2lf\n", num, square);
printf("The cube of %.2lf is: %.2lf\n", num, cube);
return 0;
}

OUTPUT
Enter a number: 3
The square of 3.00 is: 9.00
The cube of 3.00 is: 27.00

RESULT
Thus the C Program to input a number and calculate its square and cube executed successfully.
EX.NO:1D SIMPLE INTEREST

DATE

AIM
To write a C Program to calculate simple interest using the formula

ALGORITHM

1. Start.
2. Declare variables P, R, T, and SI for the principal amount, rate of interest, time, and simple interest,
respectively.
3. Prompt the user to enter the values for:amount, rate of interest and time
4. Read the input values for P, R, and T.
5. Calculate the simple interest using the formula:
6. Display the calculated simple interest.
7. End.

PROGRAM
#include <stdio.h>
int main() {
float principal, rate, time, simpleInterest;
printf("Enter the principal amount: ");
scanf("%f", &principal);
printf("Enter the rate of interest (in percentage): ");
scanf("%f", &rate);
printf("Enter the time (in years): ");
scanf("%f", &time);
simpleInterest = (principal * rate * time) / 100;
printf("\nThe Simple Interest is: %.2f\n", simpleInterest);
return 0;
}
OUTPUT
Enter the principal amount: 2500
Enter the rate of interest (in percentage): 3
Enter the time (in years): 3
The Simple Interest is: 225.00
RESULT

To write a C Program to calculate simple interest using the formula has been written and executed
successfully.
EX.NO:2A FINDING NUMBERS: POSITIVE,NEGATIVE OR ZERO

DATE

AIM:
To write a C Program to find whether a number is positive, negative, or zero.

ALGORITHM

1.Start
2. Declare an integer variable number to store the input number.
3. Get the input
4. Check if number > 0 (i.e., if the number is positive):
5. Else, check if number is less then 0 (i.e., if the number is negative):
6. Else (this means the number is zero), display "The number is zero."
7. End

PROGRAM:

#include<stdio.h>
int main()
{
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (number > 0)
{
printf("The number is positive.\n");
}
else if (number < 0)
{
printf("The number is negative.\n");
}
else
{
printf("The number is zero.\n"); }
return 0;
}
OUTPUT:
Enter a number: 5
The number is positive.
Enter a number: -5
The number is negative.
Enter a number: 0
The number is zero.

RESULT:
Thus the C Program to find whether a number is positive, negative, or zero is written and executed successfully
EX.NO:2B EVEN OR ODD NUMBER

DATE

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

ALGORITHM:
1.Start
2.Declare an integer variable number to store the input number.
3.Display the message to Enter a number
4.Input the value of number from the user.
5.Check if the number is divisible by 2
6.Display even
7.Otherwise Display Odd
8.End
PROGRAM:
#include<stdio.h>
int main()
{
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (number % 2 == 0)
{
printf("The number is even.\n");
}
else
{
printf("The number is odd.\n");
}
return 0;
}
OUTPUT:
Enter a number: 4
The number is even.
Enter a number: 3
The number is odd

RESULT:
Thus the C Program to check whether a given number is even or odd is written and executed successfully.
EX.NO:2C LARGEST OF THREE NUMBERS

DATE

AIM:
To write a C Program to input three integers and find the largest among them using nested if-else.

ALGORITHM:
1. Start
2. Declare three integer variables: num1, num2, num3.
3. Display the message: "Enter three integers:" to prompt the user for input.
4. Input the values for num1, num2, and num3 from the user.
5. Check if num1 is greater than or equal to num2 and num1 and num3
6. If Greater then display num1 is greater
7. Otherwise check num2 is greater then num3
8. If greater display num2 is greater
9. Otherwise display num3 is greater
10.End

PROGRAM:
#include<stdio.h>
int main()
{
int num1, num2, num3;
printf("Enter three integers:\n");
scanf("%d %d %d", &num1, &num2, &num3);
if (num1 >= num2) { if (num1 >= num3)
{
printf("%d is the largest number.\n", num1);
}
else
{
printf("%d is the largest number.\n", num3);
}}
else
{
if (num2 >= num3)
{
printf("%d is the largest number.\n", num2);
}
else
{
printf("%d is the largest number.\n", num3);
}
}
return 0;
}

OUTPUT:
Enter three integers:
1
2
3
3 is the largest number.

RESULT:
Thus the C Program to input three integers and find the largest among them using nested if-else is written and
executed successfully.
EX.NO:2D STUDENT GRADE CALCULATOR

DATE

AIM:
To write a c program to check the Student Grade Calculator (if-else ladder): Accept a student's marks and assign
a grade using the following scale:
Marks >= 90: A
Marks >= 80: B
Marks >= 70: C
Marks < 70: D

Algorithm:
1. Start
2. Declare a variable marks to store the student's marks.
3. Display the message: "Enter the student's marks:" to prompt the user for input.
4. Input the student's marks into the variable marks.
5. Check if is greater than or equal to 90
6. Then display Grade A
7. If mark is greater than or equal to 80
8. Then display Grade B
9. If mark is greater than or equal to 70
10.Then display Grade C
11.Otherwise Display Grade D
12.End

PROGRAM:
#include int main()
{
int marks;
printf("Enter the student's marks: ");
scanf("%d", &marks);
if (marks >= 90)
{
printf("Grade: A\n");
}
else if (marks >= 80)
{
printf("Grade: B\n");
}
else if (marks >= 70)
{
printf("Grade: C\n");
}
Else
{
printf("Grade: D\n");
}
return 0;
}

OUTPUT:
Enter the student's marks: 95
Grade: A
Enter the student's marks: 82
Grade: B
Enter the student's marks: 76
Grade: C
Enter the student's marks: 42
Grade: D

RESULT:
Thus the c program to check the Student Grade Calculator (if-else ladder) has been written and executed
successfully
EX.NO:2E ELECTRICITY BILL CALCULATOR

DATE

AIM:
To write a c program to check Electricity Bill Calculator (switch-case): Based on the number of units consumed,
calculate the electricity bill as follows:
1-100 units: ₹1.5/unit
101-200 units: ₹2.0/unit
Above 200 units: ₹3.0/unit
Use switch-case for slab selection.

ALGORITHM:
1. Start the program.
2.Declare a variable units to store the number of electricity units.
3.Declare a variable bill to store the calculated electricity bill.
4.Ask the user to enter the number of units consumed.
5.Input the number of units consumed.
6.Check the number of units:
○ If the units are between 1 and 100, charge ₹1.5 per unit.
○ If the units are between 101 and 200, charge ₹1.5 for the first 100 units, then ₹2.0 for the rest.
○ If the units are more than 200, charge ₹1.5 for the first 100 units, ₹2.0 for the next 100 units, and ₹3.0 for the
remaining units.
7.Calculate the total bill based on the conditions.
8.Display the total bill to the user.
9.End the program

PROGRAM:
#include<stdio.h>
int main()
{
int units;
float bill;
printf("Enter the number of units consumed: ");
scanf("%d", &units);
switch (units / 100)
{
case 0:
bill = units * 1.5;
break;
case 1:
bill = 100 * 1.5 + (units - 100) * 2.0;
break;
default:
bill = 100 * 1.5 + 100 * 2.0 + (units - 200) * 3.0;
break;
}
printf("The electricity bill for %d units is: Rs %.2f\n", units, bill);
return 0;
}

OUTPUT:
Enter the number of units consumed: 96
The electricity bill for 96 units is: Rs 144.00

RESULT:
Thus the c program to check the Electricity Bill Calculator (switch-case)has been written and executed
successfully.
EX.NO:2F EVEN NUMBERS SKIPPING MULTIPLES OF 5

DATE

AIM:
To write a C program to check the Even Numbers Skipping Multiples of 5 C Program to print all even numbers
from 1 to 50, but skip numbers that are multiples of 5 using the continue statement.

ALGORITHM:
1. Start the program.
2. Declare a variable i to store the current number in the loop.
3. For each number from 1 to 50: Check the condition
4. Repeat until all numbers from 1 to 50 are checked.
5. End the program.
PROGRAM:
#include<stdio.h>
int main()
{
for (int i = 1; i <= 50; i++)
{
if (i % 2 == 0)
{
if (i % 5 == 0)
{
continue;
}
printf("%d ", i);
}}
printf("\n");
return 0;
}
OUTPUT:
2 4 6 8 12 14 16 18 22 24 26 28 32 34 36 38 42 44 46 48

RESULT:
Thus the c program to check the Even Numbers Skipping Multiples of 5 C Program to print all even numbers
from 1 to 50, but skip numbers that are multiples of 5 using the continue statement is written and executed
successfully.
EX.NO:3A FACTORIAL OF NUMBER

DATE

AIM

To write a C Program to calculate the factorial of a number using a for loop.

ALGORITHM

1.Start.

2.Declare the necessary variables:

3.Prompt the user to enter a positive integer num (number for which the factorial is to be calculated)

4.Read the input number num.

5.Display the calculated factorial.

6.End.

PROGRAM

#include <stdio.h>

int main() {

int num, i;

unsigned long long factorial = 1;

printf("Enter a positive integer: ");

scanf("%d", &num);

if (num < 0) {

printf("Factorial of a negative number doesn't exist.\n");

} else {

for (i = 1; i <= num; i++) {

factorial *= i;
}

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

return 0;

OUTPUT

Enter a positive integer: 8

Factorial of 8 is: 40320

RESULT

Thus the C Program to calculate the factorial of a number using a for loop executed successfully.
EX.NO:3B REVERSE OF A GIVEN NUMBER

DATE

AIM

To write a C Program to reverse a given number

ALGORITHM

1.Start.
2.Declare the variables:
3.Prompt the user to enter a number.
4.Read the input number num.
5.Display the reversed number.
6. End.

PROGRAM

#include <stdio.h>
int main()
{
int num, reversed = 0, remainder;
printf("Enter a number: ");
scanf("%d", &num);
while (num != 0)
{
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}
printf("Reversed number is: %d\n", reversed);
return 0;
}
OUTPUT
Enter a number: 56

Reversed number is: 65

RESULT
Thus the C Program to reverse a given number executed successfully.
EX.NO:3C SUM OF FIRST N NATURAL NUMBERS

DATE

AIM

To write a C Program to find the sum of the first n natural numbers.

ALGORITHM

1. Start
2. Declare the variable n to store the number of natural numbers, and sum to store the calculated sum.
3. Prompt the user to enter a positive integer n.
4. Read the input value n.
5. Display the result
6. End

PROGRAM

#include <stdio.h>
int main()
{
int n, sum = 0;
printf("Enter a positive integer (n): ");
scanf("%d", &n);
if (n <= 0)
{
printf("Please enter a positive integer.\n");
return 1;
}
for (int i = 1; i <= n; i++) {
sum += i;
}
printf("The sum of the first %d natural numbers is: %d\n", n, sum);
return 0;
}

OUTPUT
Enter a positive integer: 8
The sum of the first 8 natural numbers is: 36
RESULT
Thus the C Program to find the sum of the first n natural numbers executed successfully.

EX.NO:3D MULTIPLICATION TABLE

DATE

AIM
To write a C Program to print the multiplication table of a given number.

ALGORITHM

1. Start.
2. Declare the variables
3. Prompt the user to enter a number num
4. Read the input value num.
5. Display a message indicating the start of the multiplication table
6. Use a for loop to iterate from i = 1 to i
7. End.

PROGRAM
#include <stdio.h>
int main() {
int num, i;
printf("Enter a number: ");
scanf("%d", &num);
printf("Multiplication table of %d:\n", num);
for (i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", num, i, num * i);
}
return 0;
}

OUTPUT
Enter a number: 8
Multiplication table of 8:
8x1=8
8 x 2 = 16
8 x 3 = 24
8 x 4 = 32
8 x 5 = 40
8 x 6 = 48
8 x 7 = 56
8 x 8 = 64
8 x 9 = 72
8 x 10 = 80
RESULT
Thus a C Program to print the multiplication table of a given number executed successfully.
EX.NO:3E ARMSTRONG NUMBER

DATE

AIM
To write a C Program to check if a given number is an Armstrong number.

ALGORITHM
1. Start.
2. Declare the variables:
3. Prompt the user to enter a number num.
4. Read the input value num.
5. Initialize originalNum to num.
6. Calculate the number of digits (n) in the number:
7. Reset num back to originalNum.
8. Initialize result to 0.
9. Calculate the sum of the digits raised to the power of
10. Compare result with originalNum:
11. End.
PROGRAM

#include <stdio.h>

#include <math.h>

int main() {

int num, originalNum, remainder, result = 0, n = 0;

printf("Enter a number: ");

scanf("%d", &num);

originalNum = num;

while (originalNum != 0) {

originalNum /= 10;

++n;

originalNum = num;

while (originalNum != 0) {

remainder = originalNum % 10;

result += pow(remainder, n);

originalNum /= 10;

if (result == num) {

printf("%d is an Armstrong number.\n", num);

} else {

printf("%d is not an Armstrong number.\n", num);


}

return 0;
}

OUTPUT
Enter a number: 8
8 is an Armstrong number.

RESULT
Thus the C Program to check if a given number is an Armstrong number executed successfully.

EX.NO:3F RIGHT TRAINGLE

DATE

AIM
To write a C Program to print a right triangle pattern of stars

ALGORITHM

1. Start.

2.Declare the variables:

3. Prompt the user to enter the number of rows for the triangle.

4.Read the input value rows.

5.After printing stars for a row, move to the next line (print a newline).

6.End

PROGRAM
#include <stdio.h>
int main() {
int rows, i, j;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++) {
for (j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
return 0;
}

OUTPUT
Enter the number of rows: 10
*
**
***
****
*****
******
*******
********
*********
**********
RESULT
Thus the C Program to print a right triangle pattern of stars executed successfully.

You might also like