I Sem Lab Manul C Programming 23-24
I Sem Lab Manul C Programming 23-24
I Sem Lab Manul C Programming 23-24
YEAR: 2023-24
Register Number :
*
**
***
****
*****
TABLE OF CONTENTS
PART – B
#include <stdio.h>
int main()
{
int radius;
float PI_VALUE=3.14, area, 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);
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)
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;
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;
/* Calculate percentage */
per = (phy + chem + bio + math + comp) / 5.0;
printf("Percentage = %.2f\n", per);
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;
/* Calculate discriminant */
discriminant = (b * b) - (4 * a * c);
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;
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;
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);
/*
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];
/*
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];
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
#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);
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 Tra_arr[rows][columns];
if(Tra_arr[rows] == Tra_arr[columns]){
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);
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];
}
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");
}
}
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;
y); a = &x;
b = &y;
temp = *b;
*b = *a;
*a = temp;
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