I Sem Lab Manul C Programming 23-24

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 41

DEPARTMENT OF COMPUTER

SCIENCE LAB MANUAL

DSCSC1.1P: C Programming Lab

YEAR: 2023-24

Name of the Student:

Register Number :

Signature of the Faculty Signature of the HoD


TABLE OF CONTENTS
PART – A

S.No Name of the Program Page Date of


Number Submission
1 Write a C Program to read radius of a circle and to find area and 1
circumference
2 Write a C Program to read three numbers and find the biggest of 2
three
3 Write a C Program to demonstrate library functions in math.h 3

4 Write a C Program to check for prime 4

5 Write a C Program to generate n primes 5

6 Write a C Program to read a number, find the sum of the digits, 6


reverse the number andcheck it for palindrome
7 Write a C Program to read numbers from keyboard continuously 7
till the user presses 999 and to find the sum of only positive
numbers
8 Write a C Program to read percentage of marks and to display 8
appropriate message(Demonstration of else-if ladder)
9 Write a C Program to find the roots of quadratic 10
equation (demonstration of switch-casestatement)
10 Write a C program to read marks scored by n students and find the 12
average of marks(Demonstration of single dimensional array)
11 Write a C Program to remove Duplicate Element in a single 13
dimensional Array
12 Program to perform addition and subtraction of Matrices 15

13 Write a C program to find the sum of individual digits of a positive 18


integer
14 Write a program to print whether a given number is even or odd. 19

15 Write a program to display the following pattern. 20

*
**
***
****
*****
TABLE OF CONTENTS
PART – B

S.No Name of the Program Page Date of


Number Submission
1 Write a C Program to find the length of a string without using built in 21
function
2 Write a C Program to demonstrate string functions. 22

3 Write a C Program to demonstrate pointers in C 23

4 Write a C Program to check a number for prime by using function 24

5 Write a C Program to read, display and to find the trace of a square 25


matrix
6 Write a C Program to read, display and multiply two m x n matrices 26

7 Write a C Program to read a string and to find the number of 29


alphabets, digits, vowels, consonants, spaces and special characters.
8 Write a C Program to Reverse a String using Pointer 31

9 Write a C Program to Swap Two Numbers using Pointers 32

10 Write a C Program to demonstrate student structure 33


to read & display records of nstudents.
11 Write a C Program to demonstrate structure. 35

12 Write a C Program to demonstrate union. 36


1. Write and execute C program to read radius of a circle and to find areaand
circumference

#include <stdio.h>
int main()
{
int radius;
float PI_VALUE=3.14, area, circumf;

//Ask user to enter the radius of


circle printf("\nEnter radius of circle:
");
//Storing the user input into variable
radius scanf("%d",&radius);

//Calculate and display Area


area = PI_VALUE * radius * radius;
printf("\nArea of circle is:
%f",area);

//Caluclate and display


Circumference circumf = 2 *
PI_VALUE * radius;
printf("\nCircumference of circle is: %f", circumf);

return(0);
}

1
2. Write and execute C program to read three numbers and find the biggestof three.

#include <stdio.h>
int main()
{
double n1, n2, n3;
printf("Enter three different numbers:
"); scanf("%lf %lf %lf", &n1, &n2,
&n3);

// if n1 is greater than both n2 and n3, n1 is the


largest if (n1 >= n2 && n1 >= n3)
printf("%.2f is the largest number.", n1);

// if n2 is greater than both n1 and n3, n2 is the


largest if (n2 >= n1 && n2 >= n3)
printf("%.2f is the largest number.", n2);

// if n3 is greater than both n1 and n2, n3 is the


largest if (n3 >= n1 && n3 >= n2)
printf("%.2f is the largest number.", n3);

return 0;
}

2
3. Write a C program to demonstrate library functions in math.h

#include <stdio.h>
#include <math.h>
int main()
{
printf("%f\n",sqrt(10.0));
printf("%f\n",exp(4.0));
printf("%f\n",log(4.0));
printf("%f\n",log10(100.0));
printf("%f\n",fabs(-5.2));
printf("%f\n",ceil(4.5));
printf("%f\n",floor(-4.5));
printf("%f\n",pow(4.0,.5));
printf("%f\n",fmod(4.5,2.0));
printf("%f\n",sin(0.0));
printf("%f\n",cos(0.0));
printf("%f\n",tan(0.0));
return 0;
}

3
4. Write and execute C program to check whether the number is prime or not

#include <stdio.h>
int main()
{
int num, count=0; //Declare the number
printf("Enter the number\n");
scanf("%d",&num); //Initialize the
number for(int i=2;i<num;i++) //Check for
factors
{
if(num%i==0)
count++;
}
if(count!=0) //Check whether Prime or not
{
printf("Not a prime number\n");
}
else
{
printf("Prime number\n");
}
return 0;
}

4
5. Write a C program generate n primes

#include<stdio.h>
int main()
{
int n,i,fact,j;
printf("Enter the
Number");
scanf("%d",&n);
printf("Prime Numbers are: \n");
for(i=1; i<=n; i++)
{
fact=0;
for(j=1; j<=n; j++)
{
if(i%j==0)
fact++;
}
if(fact==2)

printf("%d " ,i);


}
return 0;
}

5
6. Write and execute C program to read a number, find the sum of the digits,reverse the
number and check it for palindrome

#include <stdio.h>
int main()
{
int n, reversed = 0, remainder,
original; printf("Enter an integer: ");
scanf("%d", &n);
original = n;

// reversed integer is stored in reversed


variable while (n != 0) {
remainder = n % 10;
reversed = reversed * 10 + remainder;
n /= 10;
}

// palindrome if orignal and reversed are equal


if (original == reversed)
printf("%d is a palindrome.",
original); else
printf("%d is not a palindrome.", original);

return 0;
}

6
7. write a c program to read numbers from keyboard continuously till the user presses
999 and to find the sum of only positive numbers

#include<stdio.h>
void main()
{
int num;
int sum = 0;
printf("Enter the positive numbers (enter 999 to quit):\n");
scanf("%d",&num);
while(num != 999)
{
sum = sum + num;
scanf("%d",&num);
}
printf("\n The sum of all positive numbers = %d\n",sum);
}

7
8. Write and execute C program to read percentage of marks and to
display appropriate. (Demonstration of else-if ladder)

#include <stdio.h>
int main()
{
int phy, chem, bio, math, comp;
float per;

/* Input marks of five subjects from user


*/ printf("Enter five subjects marks: ");
scanf("%d%d%d%d%d", &phy, &chem, &bio, &math, &comp);

/* Calculate percentage */
per = (phy + chem + bio + math + comp) / 5.0;
printf("Percentage = %.2f\n", per);

/* Find grade according to the percentage */


if(per >= 90)
{
printf("Grade A");
}
else if(per >= 80)
{
printf("Grade B");
}
else if(per >= 70)
{
printf("Grade C");
}
else if(per >= 60)
{
printf("Grade D");
}
else if(per >= 40)
{
printf("Grade E");
}
else
{
8
printf("Grade F");
}
return 0;
}

9
9. write a c program to find the roots of quadratic equation (demonstration of switch-case
statement)

#include <stdio.h>
#include <math.h> /* Used for sqrt() */

int main()
{
float a, b, c;
float root1, root2,
imaginary; float
discriminant;

printf("Enter values of a, b, c of quadratic equation (aX^2 + bX + c): "); scanf("%f


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

/* Calculate discriminant */
discriminant = (b * b) - (4 * a * c);

/* Compute roots of quadratic equation based on the nature of discriminant */


switch(discriminant > 0)
{
case 1:
/* If discriminant is positive */
root1 = (-b + sqrt(discriminant)) / (2 *
a); root2 = (-b - sqrt(discriminant)) / (2 *
a);

printf("Two distinct and real roots exists: %.2f and %.2f", root1, root2);
break;

case 0:
/* If discriminant is not positive
*/ switch(discriminant < 0)
{
case 1:
/* If discriminant is negative
*/ root1 = root2 = -b / (2 * a);
imaginary = sqrt(-discriminant) / (2 * a);
10
printf("Two distinct complex roots exists: %.2f + i%.2f and %.2f - i%.2f",

11
root1, imaginary, root2,
imaginary); break;

case 0:
/* If discriminant is zero */
root1 = root2 = -b / (2 * a);

printf("Two equal and real roots exists: %.2f and %.2f", root1, root2);

break;
}
}

return 0;
}

12
10. Write and execute C program to read marks scored by n students and find the
average of marks (Demonstration of single dimensional array).

#include <stdio.h>
int main() {
int n, i;
float num[100], sum = 0.0, avg;

printf("Enter the numbers of subjects: ");


scanf("%d", &n);

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


printf("%d. Enter marks: ", i + 1);
scanf("%f", &num[i]);
sum += num[i];
}
avg = sum / n;
printf("Average of marks scored = %.2f",
avg); return 0;
}

13
11. Write and execute C program to remove Duplicate Elements in a
single dimensional Array.

#include <stdio.h>
int main()
{
int arr[10], i, j, k, Size;

printf("\n Please Enter Number of elements in an array : ");


scanf("%d", &Size);

printf("\n Please Enter %d elements of an Array \n", Size);


for (i = 0; i < Size; i++)
{
scanf("%d", &arr[i]);
}

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


{
for(j = i + 1; j < Size; j++)
{
if(arr[i] == arr[j])
{
for(k = j; k < Size; k++)
{
arr[k] = arr[k + 1];
}
Size--;
j--;
}
}
}
printf("\n Final Array after Deleteing Duplicate Array Elements is:\n");
for (i = 0; i < Size; i++)
{
printf("%d\t", arr[i]);
}
return 0;
}

14
15
12. Write and execute C program to perform addition and subtraction of Matrices

#include<stdio.h>
int main()
{
int n, m, c, d, first[10][10], second[10][10], sum[10][10], diff[10][10]; printf("\
nEnter the number of rows and columns of the first matrix \n"); scanf("%d%d",
&m, &n);

printf("\nEnter the %d elements of the first matrix \n", m*n);


for(c = 0; c < m; c++) // to iterate the rows
for(d = 0; d < n; d++) // to iterate the columns
scanf("%d", &first[c][d]);

printf("\nEnter the %d elements of the second matrix \n", m*n);


for(c = 0; c < m; c++) // to iterate the rows
for(d = 0; d < n; d++) // to iterate the columns
scanf("%d", &second[c][d]);

/*
printing the first matrix
*/
printf("\nThe first matrix is: \n");
for(c = 0; c < m; c++) // to iterate the rows
{
for(d = 0; d < n; d++) // to iterate the columns
{
printf("%d\t", first[c][d]);
}
printf("\n");
}

/*
printing the second matrix
*/
printf("\nThe second matrix is: \n");
for(c = 0; c < m; c++) // to iterate the rows
{
for(d = 0; d < n; d++) // to iterate the columns
{
printf("%d\t", second[c][d]);
}
printf("\n");

16
}

/*
finding the SUM of the two matrices
and storing in another matrix sum of the same size
*/
for(c = 0; c < m; c++)
for(d = 0; d < n; d++)
sum[c][d] = first[c][d] + second[c][d];

// printing the elements of the sum matrix printf("\


nThe sum of the two entered matrices is: \n"); for(c =
0; c < m; c++)
{
for(d = 0; d < n; d++)
{
printf("%d\t", sum[c][d]);
}
printf("\n");
}

/*
finding the DIFFERENCE of the two matrices
and storing in another matrix difference of the same size
*/
for(c = 0; c < m; c++)
for(d = 0; d < n; d++)
diff[c][d] = first[c][d] - second[c][d];

// printing the elements of the diff matrix


printf("\nThe difference(subtraction) of the two entered matrices is: \n");
for(c = 0; c < m; c++)
{
for(d = 0; d < n; d++)
{
printf("%d\t", diff[c][d]);
}
printf("\n");
}
return 0;
}

17
18
13. Write a C program to find the sum of individual digits of a positive integer

#include<stdio.h>
#include<conio.h>
void main()
{
int n, sum=0,d ;
printf("Enter any
integer:"); scanf("%d",
&n); while(n>0)
{
d=n%10;
sum=sum+d;
n=n/10;
}
printf("sum of individual digits is %d",sum);
getch();
}

19
14. Write a C program to print whether a given number is even or odd.

#include<stdio.h>
#include<conio.h>
void main()
{
int num;
printf("Enter the number: ");
scanf("%d",&num); if(num
%2==0)
printf("\n %d is even", num);
else
printf("\n %d is odd", num);
getch();
}

20
15. Write a program to display the following pattern.

*
**
***
****
*****

#include<conio.h>
#include<stdio.h>
void main()
{
int i,j;
for(i=1; i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
}
getch();
}

21
PART B

1. Write C program to find the length of a string without


usingbuilt in function

#include <stdio.h>
int main()
{
/* Here we are taking a char array of size
100 which means this array can hold a
string
of 100 chars. You can change this as per requirement
*/
char str[100],i;
printf("Enter a string: \n");
scanf("%s",str);

// '\0' represents end of String


for(i=0; str[i]!='\0'; ++i);
printf("\nLength of input string: %d",i);

return 0;
}

22
2. Write a C Program to demonstrate string functions.

#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
char a[20],b[20],c[20];
int l;
clrscr();
printf("Enter the First String");
scanf("%s",&a);
printf("Enter the Second String");
scanf("%s",&b);
strcat(a,b);
printf("\nConcatenation of String a and b is:
%s",a); l=strlen(a);
printf("\nLength of String is
%d",l); strcpy(c,a);
printf("\nthe Copied String is %s",c);
strrev(a);
printf("\nreverse of String is
%s",a); getch();
}

23
3. Write a C Program to demonstrate pointers in C

#include <stdio.h>
int main()
{
int num = 10;
printf("Value of variable num is: %d", num);
/* To print the address of a variable we use %p
* format specifier and ampersand (&) sign just
* before the variable name like &num.
*/
printf("\nAddress of variable num is: %p",
&num); return 0;
}

24
4. Write a C Program to check a number for prime by using
function

#include <stdio.h>
int checkPrime(int num)
{
int count = 0;
if (num == 1)
{
count = 1;
}
for (int i = 2; i <= num / 2; i++)
{ if (num % i == 0){
count = 1;
break;
}
}
return count;
}

int main(){
int num;
// Asking for Input
printf("Enter a number: ");
scanf("%d", &num);
if (checkPrime(num) == 0){
printf("%d is a Prime Number.", num);
}
else
{
printf("%d is not a Prime Number.", num);
}
return 0;
}

25
5. Write a C Program to read, display and to find the trace of a
square matrix

#include <stdio.h>
int main()
{

int i, j, rows, columns, trace = 0;

printf("Enter Matrix Rows and Columns = ");


scanf("%d %d", &rows, &columns);

int Tra_arr[rows][columns];

if(Tra_arr[rows] == Tra_arr[columns]){

printf("Please Enter the Matrix Items = \n");


i = 0;
do
{
j = 0;
do
{
scanf("%d", &Tra_arr[i][j]);
if (i == j)
{
trace = trace + Tra_arr[i][j];
}
} while (++j < columns);
} while (++i < rows);

printf("The Trace Of the Matrix = %d\n", trace);


}
else
printf("Please enter square matrix only");
}

26
6. Write a C Program to read, display and multiply two m x n matrices

#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int a[10][10],b[10][10],c[10][10],i,j,k,m,n,o,p;
printf("Enter the size of A metrix\n");
scanf("%d%d",&m,&n);

printf("Enter the size of B metrix\n");


scanf("%d%d",&o,&p);
if (n==o)
{
printf("Enter the elements of A metrix\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("Enter the elements of B metrix\n");
for(i=0;i<o;i++)
for(j=0;j<p;j++)
scanf("%d",&b[i][j]);

for(i=0;i<m;i++)
for(j=0;j<p;j++)
{
c[i][j] =0;
for (k=0;k<n;k++)
c[i][j] = c[i][j]+a[i][k]*b[k][j];
}

printf("\nA matrix is :\n\n");


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

27
for(j=0;j<n;j++)
printf("%4d", a[i][j]);
printf("\n");
}
printf("\nB matrix is :\n\n");

for(i=0;i<o;i++)
{
for(j=0;j<p;j++)
printf("%4d", b[i][j]);
printf("\n");
}

printf("\nC matrix is :\n\n");


for(i=0;i<m;i++)
{
for(j=0;j<p;j++)
printf("%4d", c[i][j]);
printf("\n");
}

}
else
printf("Multiplication not possible \n Please Re-enter Correct Size\n");
getch();
}

28
29
7. Write and execute C program to read and to find the number of
digits, vowels, consonants, spaces and special characters.
#include<stdio.h>
void main()
{
char str[200];
int i,vowels=0,consonants=0,digits=0,spaces=0,specialCharacters=0;
printf("Enter a string\n");
gets(str);
for(i=0;str[i]!='\0';i++)
{
if(str[i]=='a' || str[i]=='e' || str[i]=='i' ||str[i]=='o' || str[i]=='u' || str[i]=='A'
||str[i]=='E' || str[i]=='I' || str[i]=='O' ||str[i]=='U')
{
vowels++;
}
else if((str[i]>='a'&& str[i]<='z') || (str[i]>='A'&& str[i]<='Z'))
{
consonants++;
}
else if(str[i]>='0' && str[i]<='9')
{
digits++;
}
else if (str[i]==' ')
{
spaces++;
}
else
{
specialCharacters++;
}
}
printf("\nVowels = %d",vowels);
printf("\nConsonants =
30
%d",consonants);

31
printf("\nDigits = %d",digits);
printf("\nWhite spaces = %d",spaces);
printf("\nSpecial characters = %d",specialCharacters);
}

32
8. Write a C Program to Reverse a String using Pointer

#include <stdio.h>
int main()
{
char str1[50];
char revstr[50];
char *stptr = str1;
char *rvptr = revstr;
int i=-1;
printf("\n\n Pointer : Print a string in reverse order\n");
printf(" \n");
printf(" Input a string : ");
scanf("%s",str1);
while(*stptr)
{
stptr++;
i++;
}
while(i>=0)
{
stptr--;
*rvptr = *stptr;
rvptr++;
--i;
}
*rvptr='\0';
printf(" Reverse of the string is : %s\n\
n",revstr); return 0;
}

33
9. Write a C Program to swap two numbers using Pointers

#include <stdio.h>
int main()
{
int x, y, *a, *b, temp;

printf("Enter the value of x and y\n");


scanf("%d%d", &x, &y);

printf("Before Swapping\nx = %d\ny = %d\n", x,

y); a = &x;
b = &y;

temp = *b;
*b = *a;
*a = temp;

printf("After Swapping\nx = %d\ny = %d\n", x, y);

return 0;
}

34
10. Write a C Program to demonstrate students to read and display
records of n students

#include <stdio.h>
struct student {
char firstName[50];
int roll;
float marks;
} s[];

int main() {
int i, n;
printf("Enter the number of students:\
n"); scanf("%d", &n);
printf("Enter information of students:\n");
// storing information
for (i = 0; i < n; ++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("Enter marks: ");
scanf("%f", &s[i].marks);
}
printf("Displaying Information:\n\n");

// displaying information
for (i = 0; i < n; ++i) {
printf("\nRoll number: %d\n", i +
1); printf("First name: ");
puts(s[i].firstName);
printf("Marks: %.1f",
s[i].marks); printf("\n");
}
return 0;
}
35
36
11. Write a C Program to demonstrate structure.

#include <stdio.h>
struct student {
char name[60];
int roll_no;
float marks;
} sdt;
int main() {
printf("Enter the following information:\
n"); printf("Enter student name: ");
fgets(sdt.name, sizeof(sdt.name), stdin);
printf("Enter student roll number: ");
scanf("%d", & sdt. roll_no);
printf("Enter students marks:
"); scanf("%f", & sdt.marks);
printf("The information you have entered is: \
n"); printf("Student name: ");
printf("%s", sdt.name);
printf("Student roll number: %d\n", sdt.
roll_no); printf("Student marks: %.1f\n",
sdt.marks); return 0;
}

37
12. Write a C Program to demonstrate the union.

#include <stdio.h>

union item
{
int x;
float y;
char ch;
};

int main( )
{
union item
it; it.x = 12;
it.y =
20.2; it.ch
= 'a';

printf("%d\n", it.x);
printf("%f\n", it.y);
printf("%c\n", it.ch);

return 0;
}

38

You might also like