KEMBAR78
Practicals | PDF | Integer (Computer Science) | Computing
0% found this document useful (0 votes)
29 views16 pages

Practicals

Pratical of cp

Uploaded by

jsaurav2468
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)
29 views16 pages

Practicals

Pratical of cp

Uploaded by

jsaurav2468
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/ 16

Module 1:- Programming with C

1. Practical 1:-

a. To calculate simple interest taking principal, rate of interest and number of years as input

from user. Write algorithm & draw flowchart for the same.

Algorithm:

Name of Algorithm: To calculate the simple interest.

Step 1: Start

Step 2: Read principal amount as ‘p’, time as ‘t’ and rate of interest as ‘r’

Step 3: SI = p*t*r/100

Step 4: Display SI

Step 5: Stop

#include <stdio.h>

int main() {

float principal, rate, time, interest;

// Take user input for principal, rate, and time

printf("Enter principal amount: ");

scanf("%f", &principal);

printf("Enter interest rate: ");

scanf("%f", &rate);

printf("Enter time period (in years): ");

scanf("%f", &time);

// Calculate simple interest

interest = (principal * rate * time) / 100;

// Display the result

printf("Simple Interest is: %.2f\n", interest);

return 0;

}
Output:

Enter principal amount: 1000

Enter interest rate: 6

Enter time period (in years): 2

Simple Interest is: 120.00

b. Write a program to find greatest of three numbers using conditional operator. Write

algorithm & draw flowchart for the same.

Algorithm

Step1:

// using nested if-else

#include <stdio.h>

int main()

int c = 10, b = 22, a = 9;

// Finding largest by comparing using relational operators

if (a >= b) {

if (a >= c)

printf("%d is the largest number.", a);

else

printf("%d is the largest number.", c);

else {

if (b >= c)

printf("%d is the largest number.", b);

else

printf("%d is the largest number.", c);

return 0;
}

Output:

The numbers A, B and C are: 10, 22, 9

22 is the largest number.

c. Write a program to check if the year entered is leap year or not. Write algorithm & draw

flowchart for the same.

Algorithm

START

Step 1 → Take integer variable year

Step 2 → Enter the year

Step 3 → Check if year is divisible by 4 but not 100, DISPLAY "leap year"

Step 4 → Check if year is divisible by 400, DISPLAY "leap year"

Step 5 → Otherwise, DISPLAY "not leap year"

Flowchart

#include <stdio.h>

int main() {

int year;

printf("Enter a year: ");

scanf("%d", &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);

return 0;
}

2. Practical 2:-

a. Write a program to calculate roots of a quadratic equation.

Algorithm

Start

Read a, b, c values

Compute d = b2 4ac

if d > 0 then

r1 = b+ sqrt (d)/(2*a)

r2 = b sqrt(d)/(2*a)

Otherwise if d = 0 then

compute r1 = -b/2a, r2=-b/2a

print r1,r2 values

Otherwise if d < 0 then print roots are imaginary

Stop

#include <stdio.h>

#include <math.h>

int main()

float a, b, c, r1, r2, d;

printf("Enter the values of a b c: ");

scanf(" %f %f %f", & a, & b, & c);

d = b * b - 4 * a * c;

if (d > 0) { r1 = -b + sqrt(d) / (2 * a);

r2 = -b - sqrt(d) / (2 * a);

printf("The real roots = %f %f", r1, r2);

else if (d == 0) { r1 = -b / (2 * a); r2 = -b / (2 * a);


printf("Roots are equal =%f %f", r1, r2);

Else

printf("Roots are imaginary");

return 0;

Output:

Case 1:

Enter the values of a b c: 1 4 3

The real roots = -3.000000 -5.000000

Case 2:

Enter the values of a b c: 1 2 1

Roots are equal =-1.000000 -1.000000

Case 3:

Enter the values of a b c: 1 1 4

Roots are imaginary

b. Write a menu driven program using switch case to perform add / subtract / multiply /

divide based on the users choice.

#include<stdio.h>

#include<conio.h>

int main()

int a,b;

int op;

printf(" 1.Addition\n 2.Subtraction\n 3.Multiplication\n 4.Division\n");

printf("Enter the values of a & b: ");

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

printf("Enter your Choice : ");


scanf("%d",&op);

switch(op)

case 1 :

printf("Sum of %d and %d is : %d",a,b,a+b);

break;

case 2 :

printf("Difference of %d and %d is : %d",a,b,a-b);

break;

case 3 :

printf("Multiplication of %d and %d is : %d",a,b,a*b);

break;

case 4 :

printf("Division of Two Numbers is %d : ",a/b);

break;

default :

printf(" Enter Your Correct Choice.");

break;

return 0;

Output:

1.Addition

2.Subtraction

3.Multiplication

4.Division

Enter the values of a & b: 20 15

Enter your Choice : 1


Sum of 20 and 15 is : 35

c. Write a program to print the pattern of asterisks

#include <stdio.h>

int main()

int rows = 5;

// This loop to print all rows

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

// Inner loop 1 to print

// white spaces for each row

for (int j = 0; j < 2 * (rows - i) - 1; j++) {

printf(" ");

// Inner loop 2 to print star

// (*) character for each row

for (int k = 0; k < 2 * i + 1; k++) {

printf("* ");

printf("\n");

return 0;

Output:

***

*****

*******

*********
3. Practical 3

a. Write a program using while loop to reverse the digits of a number

#include <stdio.h>

int main() {

int num, reversedNum = 0, remainder;

// Prompt the user to enter a number

printf("Enter a number: ");

scanf("%d", &num);

// Reverse the number using a while loop

while (num != 0) {

remainder = num % 10;

reversedNum = reversedNum * 10 + remainder;

num /= 10; }

// Print the reversed number

printf("Reversed number: %d\n", reversedNum);

return 0;

Output:

Enter a number: 2345

Reversed number = 5432

. b. Write a program to calculate the factorial of a given number.

#include <stdio.h>

int main() {

int n, i;

unsigned long long fact = 1;

printf("Enter an integer: ");

scanf("%d", &n);

// shows error if the user enters a negative integer


if (n < 0)

printf("Error! Factorial of a negative number doesn't exist.");

else {

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

fact *= i;

printf("Factorial of %d = %llu", n, fact);

return 0;

Output:

Enter an integer: 10

Factorial of 10 = 3628800

c. Write a program to print the Fibonacci series.

#include <stdio.h>

int main() {

int i, n;

// initialize first and second terms

int t1 = 0, t2 = 1;

// initialize the next term (3rd term)

int nextTerm = t1 + t2;

// get no. of terms from user

printf("Enter the number of terms: ");

scanf("%d", &n);

// print the first two terms t1 and t2

printf("Fibonacci Series: %d, %d, ", t1, t2);

// print 3rd to nth terms

for (i = 3; i <= n; ++i) {


printf("%d, ", nextTerm);

t1 = t2;

t2 = nextTerm;

nextTerm = t1 + t2;

return 0;

Output:

Enter the number of terms: 10

Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

4. Practical 4

a. Write a program to print area of square using function.

#include <stdio.h>

// Function to calculate the area of a square

// It takes one argument: 'side' (the length of a side of the square)

// It returns the calculated area as a float

float calculateAreaOfSquare(float side) {

float area = side * side;

return area;

int main()

float sideLength;

float areaResult;

printf("Enter the length of the side of the square: ");

scanf("%f", &sideLength);

areaResult = calculateAreaOfSquare(sideLength);

printf("The area of the square with side length %.2f is: %.2f\n", sideLength,
areaResult);

return 0;

Output:

b. Write a program using recursive function

#include <stdio.h>

int sum(int k);

int main() {

int result = sum(10);

printf("%d", result);

return 0;

int sum(int k) {

if (k > 0) {

return k + sum(k - 1);

} else {

return 0;

Output:

55

c. Write a program to square root, abs() value using function.

#include <stdio.h>

#include <math.h>

#include <stdlib.h> //

int main() {

int integer_input;

double double_input;

printf("Enter an integer number: ");


scanf("%d", &integer_input);

int absolute_value = abs(integer_input);

printf("Absolute value of %d is: %d\n", integer_input, absolute_value);

printf("Enter a non-negative number (double): ");

scanf("%lf", &double_input);

if (double_input >= 0) {

double square_root_value = sqrt(double_input);

printf("Square root of %.2lf is: %.4lf\n", double_input, square_root_value);

} else {

printf("Cannot calculate square root of a negative number.\n");

return 0;

Output:

d. Write a program using goto statement .

#include <stdio.h>

int main ()

int i = 11;

if (i % 2 == 0)

EVEN:

printf("The number is even \n");

goto END;

Else

ODD:
printf("The number is odd \n");

END:

printf("End of program");

return 0;

Output:

The number is odd

End of program

5. Practical 5

a. Write a program to print rollno and names of 10 students using array.

#include <stdio.h>

struct student {

char firstName[50];

int roll;

} s[10];

int main() {

int i;

printf("Enter information of students:\n");

// storing information

for (i = 0; i < 10; ++i) {

s[i].roll = i + 1;

printf("\nFor roll number%d,\n", s[i].roll);

printf("Enter first name: ");

scanf("%s", s[i].firstName);

}
printf("Displaying Information:\n\n");

// displaying information

for (i = 0; i < 10; ++i) {

printf("\nRoll number: %d\n", i + 1);

printf("First name: ");

puts(s[i].firstName);

return 0;

Output:

Enter information of students:

For roll number1,

Enter first name:

For roll number2,

Enter first name:

For roll number3,

Enter first name:

b. Write a program to sort the elements of array in ascending or descending order

#include <stdio.h>

void main (){

int num[20];

int i, j, a, n;

printf("Enter number of elements in an array");

scanf("%d", &n);
printf("Enter the elements");

for (i = 0; i < n; ++i)

scanf("%d", &num[i]);

for (i = 0; i < n; ++i){

for (j = i + 1; j < n; ++j){

if (num[i] > num[j]){

a = num[i];

num[i] = num[j];

num[j] = a;

printf("The numbers in ascending order is:");

for (i = 0; i < n; ++i){

printf("%d", num[i]);

Enter number of elements in an array

Enter the elements

12

23

89

11

22

The numbers in ascending order is:

11

12

22

23
89

6. Practical 6

a. Write a program to extract the portion of a character string and print the extracted part.

b. Write a program to find the given string is palindrome or not.

c. Write a program to using strlen(), strcmp() function .

7. Practical 7

Write a program to swap two numbers using a function. Pass the values to be swapped to

this function using call-by-value method and call-byreference method.

8. Practical 8 a. Write a program to read a matrix of size m*n.

b. Write a program to multiply two matrices using a function.

9. Practical 9 Write a program to print the structure using Title Author Subject Book ID Print

the details of two students.

10. Practical 10

Create a mini project on “Bank management system”. The program should be menu driven.

You might also like