0% found this document useful (0 votes)
7 views26 pages

Computer Programming Lab Exercises

Notes for c language

Uploaded by

jangrashyam2211
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)
7 views26 pages

Computer Programming Lab Exercises

Notes for c language

Uploaded by

jangrashyam2211
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

PRACTICAL FILE

OF

FUNDAMENTALS OF COMPUTERS & PROGRAMMING (WITH C)

(CSEN1102)

BACHELOR OF TECHNOLOGY
IN
COMPUTER SCIENCE & ENGINEERING

Submitted By: Submitted To:

Name: SHYAM Ms. Anita Khandelwal

Roll no: A40322029 CSE Department

[Link] 1st semester PDMU, Bahadurgarh

1|Page
INDEX

S. No. Experiment Date Signature

1 Write a program to calculate Simple Interest. 10/11/2022

2 Write a program to print Largest of Three numbers (If-Then-Else) 17/11/2022


Write a Program to print whether given number is Prime or not. 24/11/2022
3
Write Basic program illustrating Switch Case Statement. 01/12/2022
4
Write a program to print Largest of ten numbers (For Statement). 08/12/2022
5
Write a program to implement Matrix Multiplication. 15/12/2022
6
Write a program to print Fibonacci Series. 12/01/2023
7
Write a program to print Factorial of a number. 19/01/2023
8
Write a program to implement different String Functions. 02/02/2023
9
Write a program to check whether a string is palindrome or not. 16/02/2023
10

2|Page
Program -1
Aim: Write a program to calculate Simple Interest.

# include <stdio.h>

int main()

int p, time;

float rate;

float si;

printf ("enter value of principle: \n");

scanf ("%d",&p);

print f ("enter value of time: \n");

scan f ("%d",& time);

printf("enter rate: \n");

scanf("%f",& rate);

si = (p*rate*time)/100;

print f ("simple interest is %f",si);

return 0;

3|Page
OUTPUT:

4|Page
Program -2

Aim: Write a program to print largest of three numbers (if-then- else).

# include <stdio.h>

int main()

int n1,n2,n3;

printf ("enter three no. \n");

scanf ("%d %d %d",&n1,&n2,&n3); if(n1>n2 && n1>n3){

printf ("%d is the largest number\n",n1);

else if (n2>n1 && n2>n3){

printf ("%d is the largest number\n",n2);

else

printf ("%d is the largest number\n",n3);

return 0;

5|Page
OUTPUT:

6|Page
Program -3

Aim: Write a program to print whether given number is prime or not.


# include <stdio.h>

int main()

int n, m=0, flag=0;

printf ("Enter the number to check prime:\n");

scanf("%d",&n);

m=n/2;

for (int i=2; i<=m; i++ )

if (n%i==0)

printf ("number is not prime \n");

flag=1;

break;

if (flag==0){

printf("number is prime \n");

return 0;

7|Page
Output:

8|Page
Program -4

Aim: Write basic programs illustrating Switch Case statement .

# include <stdio.h>

int main()

int n1,n2;

int choice;

int A,S,M,D;

printf("enter the 1st number: \n"); scanf("%d",&n1);

printf("enter the 2nd number: \n"); scanf("%d",&n2);

printf("\nenter choice: \n");

printf("\[Link]\[Link]\[Link]\[Link]\n");

scanf("%d",&choice);

switch(choice)

case 1: A=n1+n2;

printf("Addition is %d",A);

break;

case 2: S=n1-n2;

printf("Subtraction is %d",S);

break;

9|Page
case 3: M = n1*n2;

printf ("multiplicaion is %d",M);

break;

case 4: D= n1/n2;

printf ("division is %d",D);

break;

default : printf ("\ninvalid choice");

return 0;

10 | P a g e
Output:

11 | P a g e
Program -5

Aim: Write a program to print largest of ten numbers (for statement).


#include<stdio.h>

int main()

int a[10];

int i;

int greatest;

printf("enter the value: "); for (i=0;i<10;i++)

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

greatest = a[10];

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

if (a[i]>greatest)

greatest = a[i];

printf("greatest of ten numbers is :%d \n",greatest);

return 0;

12 | P a g e
OUTPUT:

13 | P a g e
Program -6
Aim : Write a program to implement matrix multiplication.
#include<stdio.h>

#include<stdlib.h>

int main()

int a[10][10],b[10][10],c[10][10]; int r1,c1,r2,c2;

printf("enter the row and column of 1st matrix:\n"); scanf("%d %d",&r1,&c1);

printf("enter the row and column of 2nd matrix: \n"); scanf("%d %d",&r2,&c2);

if (c1!=r2){

printf("\n matrix is not multiplicable \n");

exit(0);

printf("enter the element of 1st matrix: \n"); for (int i=0;i<r1;i++)

for (int j=0;j<c1;j++){

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

printf("enter the element of 2nd matrix: \n");

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

for (int j=0;j<c2;j++){

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

14 | P a g e
}

printf("\n1st matrix:\n");

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

for (int j=0;j<c1;j++)

printf("%d\t",a[i][j]); }

printf("\n");

printf("\n2st matrix:\n");

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

for (int j=0;j<c2;j++){

printf("%d\t",b[i][j]); }

printf("\n");

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

for(int j=0;j<c2;j++){

int s=0;

for(int k=0;k<c1;k++){

s+=a[i][k]*b[k][j];

c[i][j]=s;

printf("\nproduct of matrix\n"); for(int i=0;i<r1;i++){

for (int j=0;j<c2;j++){


printf("%d\t",c[i][j]);

15 | P a g e
}

printf("\n");
}

return 0;
}

16 | P a g e
OUTPUT:

OUTPUT:

17 | P a g e
Program -7

Aim: Write a program to print Fibonacci Series.


#include<stdio.h>

int main()

int n1=0,n2=1,n3,i,n;

printf("enter the term of fiboncci no. \n");

scanf("%d\n",&n);

printf("%d %d",n1,n2);

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

n3=n1+n2;

printf(" %d",n3);

n1=n2;

n2=n3;

return 0;

18 | P a g e
OUTPUT :

19 | P a g e
Program -8
Aim: Write a program to print factorial of a number.

#include<stdio.h>

int main()

int fact=1,n;

printf("Enter the number to find its factorial: \n");

scanf("%d",&n);

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

fact*=i;

printf("factorial of entered %d number is: %d",n,fact);

return 0;

20 | P a g e
OUTPUT:

21 | P a g e
Program -9

Aim: Write a program to implement different string functions


#include <stdio.h>

#include <string.h>

int main()

char a[20]="Program";

/*strlen(a)*/

char b[20]={'P','r','o','g','r','a','m','\0'};

printf("Length of string a = %zu \n",strlen(a));

printf("Length of string b = %zu \n",strlen(b));

char str1[100] = "This is ", str2[] = "good boy";

/strcat/

strcat(str1, str2);

puts(str1);

puts(str2);

return 0;

22 | P a g e
OUTPUT:

OUTPUT:

23 | P a g e
Program -10
Aim: Write a program to check whether a string is palindrome or not.

#include <stdio.h>

#include <string.h>

int main()

char string1[20];

int i, length;

int flag = 0;

printf("Enter a string:");

scanf("%s", string1);

length = strlen(string1);

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

if(string1[i] !=

string1[length-i-1])

flag = 1;

break;

if (flag)

24 | P a g e
{

printf ("%s is not a palindrome", string1);

else

printf ("%s is a palindrome", string1);

return 0;

25 | P a g e
OUTPUT:

26 | P a g e

Common questions

Powered by AI

The switch-case structure in the C program is utilized to perform different arithmetic operations by taking a user input choice to determine which case to execute among addition, subtraction, multiplication, or division. Each operation is delineated within its case block, and the corresponding arithmetic operation is executed based on the chosen case. The main advantage of using switch-case over if-else constructs is its clarity in coding and execution for situations with multiple discrete choices. Switch-case reduces code complexity, making it easier to read and maintain compared to multiple nested if-else statements .

The program employs an iterative method using a loop to find the largest number out of ten entries. It initializes a variable `greatest` with the first number of the array. It then iterates through the rest of the array, comparing each value with the current `greatest` and updating `greatest` if a larger number is found. This ensures accuracy by guaranteeing that `greatest` holds the maximum value by the end of the loop .

A palindrome is a sequence of characters that reads the same forward and backward, ignoring letter case. The C program verifies if a string is a palindrome by comparing characters from the start and end of the string moving towards the middle. If any pair of characters differ, the flag is set, and the program declares the string is not a palindrome. If all characters match symmetrically from both ends, the string is declared a palindrome. This comparison uses a loop that iterates up to half the length of the string .

The C program uses an iterative algorithm to print the Fibonacci series. It starts with initializing the first two numbers of the sequence, 0 and 1. For generating the next number in the sequence, each subsequent number is obtained by summing the last two numbers. The loop iteratively updates the two preceding numbers and computes the next Fibonacci number, continuing this process for the specified range by the user input .

The computation of simple interest is determined by three factors: the principal amount (p), the rate of interest (rate), and the time (time) for which the principal is borrowed or invested. These are utilized in a C program by first obtaining the values from the user through inputs. The formula for calculating simple interest is (p * rate * time) / 100. In the C program, after inputting these values, the calculation is done using the formula and the result is printed as output .

The program to determine the largest of three numbers in C uses a series of conditional statements. It compares the first number (n1) with the other two numbers (n2, n3) using logical AND operators (&&) in an if-else structure. If n1 is greater than both n2 and n3, it is declared the largest. Otherwise, it checks if n2 is greater than both n1 and n3, declaring n2 as the largest if true. If neither of these conditions is met, the program defaults to declaring n3 as the largest number, ensuring that one of the three numbers is always designated as the largest .

The program calculates the factorial of a number using an iterative approach with a loop structure that multiplies a running product `fact` initialized at 1 by each integer from 1 up to the number `n`. This iteratively accumulated product results in the factorial of `n`. Potential limitations include integer overflow for large values of `n` due to the limited range of standard integer types in C, which can cause incorrect results if the product exceeds the maximum value representable by an integer .

The program demonstrates the use of string manipulation library functions by implementing `strlen` to calculate the length of strings, and `strcat` to concatenate strings. It initializes strings and arrays, uses `strlen` for obtaining the number of characters in each string, and `strcat` to append one string to another. These standard library functions simplify string handling, abstracting the complexities of manipulating character arrays manually in C .

The implementation of matrix multiplication in C first ensures that the matrices are conformable by checking that the number of columns in the first matrix is equal to the number of rows in the second matrix. If this condition is not met, the program outputs that the matrices cannot be multiplied and exits without proceeding further. This ensures that each element in a row of the first matrix can be accurately multiplied with the corresponding element in a column of the second matrix to compute the products required for the resulting matrix .

Testing for matrix conformability is crucial in programming mathematical operations because it directly determines the feasibility of operations like matrix multiplication. Conformability checks—notably the condition that the number of columns in the first matrix equals the number of rows in the second—ensure that the mathematical basis for matrix multiplication is met, allowing for the dot products to be calculated consistently across rows and columns. This verification prevents runtime errors and ensures the logical correctness of applied mathematical operations, forming the foundational step in developing robust, error-resistant numerical software .

You might also like