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

C Programming Lab Manual

Uploaded by

fathimathzulfah
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)
9 views

C Programming Lab Manual

Uploaded by

fathimathzulfah
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/ 26

/**********************************************************************************

Part A -1 Program to find the roots of quadratic equation using else if


ladder.
**********************************************************************************/
# include<stdio.h>
# include<conio.h>
# include<math.h>
void main ()
{
float a,b,c,r1,r2,d;
clrscr();
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");
getch ();
} Output:
Enter the values of a b c 1 4 3
The real roots =-3.0000 -5.000

Enter the values of a b c 3 4 5


Roots are imaginary

Enter the values of a b c 1 2 1


Roots are equal
Mr K Praveen Kumar, SMC, SHIRVA P a g e 3 | 28
*********************************************************************************
Part A -2 Program to read two integer values & an operator as character
and perform basic arithmetic operations on them using switch case (+, -,
*, / operations)
********************************************************************************/
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
char ope;
clrscr();
printf("Enter 2 numbers\n");
scanf("%d%d",&a,&b);
printf("------Menu----------\n");
printf("Press + symbol to perform Addition\n");
printf("Press - symbol to perform subtraction\n");
printf("Press * symbol to perform Multiplication\n");
printf("Press / symbol to perform Division\n");
printf("Press % symbol to perform Modulo\n");
printf ("Enter your operator\n");
fflush(stdin);
scanf("%c",&ope);
switch(ope)
{
case '+': c=a+b;
printf("Addition of two number is=%d\n",c);
break;
case '-': c=a-b;
printf("Subtraction of two number is=%d\n",c);
break;
case '*': c=a*b;
printf("Multiplication of two number is=%d\n",c);
break;
case '/': c=a/b;
printf("Division of two number is=%d\n",c);
break;
case '%': c=a%b;
Mr K Praveen Kumar, SMC, SHIRVA P a g e 4 | 28
printf("Modulo of two number is=%d\n",c);
break;
default: printf("wrong operator\n");
break;
}
printf("End of the program\n");
getch();
}
Output:

Output2:

Mr K Praveen Kumar, SMC, SHIRVA P a g e 5 | 28


/*********************************************************************************
Part A -3 Program to reverse a number and find the sum of individual
digits. Also check for palindrome.
**********************************************************************************/
#include<stdio.h>
#include<conio.h>
void main()
{
int r,n,sum=0,rev=0, temp;
clrscr();
printf("\n*****OUTPUT*****\n");
printf("Enter value for n:");
scanf("%d",&n);
temp=n;
while(n!=0)
{
r=n%10;
sum=sum+r;
rev=rev*10+r;
n=n/10;
}
printf("rev=%d\n",rev);
printf("sum=%d\n",sum);
if(temp==rev)
printf("\n %d is palindrome",temp);
else
printf("\n %d is not palindrome",temp);
getch();
}
*****OUTPUT1*****
Enter value for n:111
rev=111
sum=3
111 is palindrome
*****OUTPUT2*****
Enter value for n:123
rev=321
sum=6
123 is not palindrome

Mr K Praveen Kumar, SMC, SHIRVA P a g e 6 | 28


/********************************************************************************
Part A -4 Program to calculate and display the first ‘n’ Fibonacci numbers
**********************************************************************************/
#include<stdio.h>
#include<conio.h>
void main()
{
int a=0,b=1,c,n,count=0;
clrscr();
printf("Enter the n value\n");
scanf("%d",&n);
printf("First %d fibonacci numbers are\n",n);
printf("%d\t %d\t",a,b);
count=2;/* a and b already used*/
while(count<=n)
{
c=a+b;
count++;
printf("%d\t",c);
a=b;
b=c;
}
getch();
}
Output:

Mr K Praveen Kumar, SMC, SHIRVA P a g e 7 | 28


/****************************************************************
Part A -5 Program to find given number is a prime or not.
*******************************************************************/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,m,flag=0;
clrscr();
printf("Enter the number to check prime:");
scanf("%d",&n);
m=n/2;
for(i=2;i<=m;i++)
{
if (n%i==0)
{
printf("%d number is not prime\n",n);
flag=1;
break;
}
}
if(flag==0)
printf("%d number is prime\n",n);
getch();
}
Output:
Enter the number to check prime:56

56 number is not prime

Output:
Enter the number to check prime:23

23 number is prime

Mr K Praveen Kumar, SMC, SHIRVA P a g e 8 | 28


/*************************************************************
Part A -6 Program to count occurrences of a character in a string.
*************************************************************/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[100],ch;
int i,count=0;
clrscr();
printf("Enter any string\n");
gets(str);
printf("Enter character that you want to search for :");
scanf("%c",&ch);
for(i=0;i<=strlen(str);i++)
{
if(str[i]==ch)
{
count++;
}
}
printf("\n The Total Number of times %c has occured=%d",ch,count);
getch();
}
Output:

Mr K Praveen Kumar, SMC, SHIRVA P a g e 9 | 28


/*************************************************************************
Part A -7 Program to read string with alphabets, digits and special
characters and convert upper case letters to lower case and vice a versa
and retain the digits and special characters as it is
*****************************************************************************/
#include <stdio.h>
#include <string.h>
void main() {
char s[100];
int i;
clrscr();
printf("\nEnter a string : ");
gets(s);

for (i = 0; s[i]!='\0'; i++) {


if(s[i] >= 'a' && s[i] <= 'z') {
s[i] = s[i] - 32;
}
}
printf("\nString in Upper Case = %s", s);
getch();
}

Mr K Praveen Kumar, SMC, SHIRVA P a g e 10 | 28


/***************************************************************************
Part A -8 Program to search for a number in a list of numbers using one-
dimensional array.
******************************************************************************
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],i,n,search, count=0;
clrscr();
printf("Enter the total num of elements: ");
scanf("%d",&n);
printf("Enter the %d elements:\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the searching element:");
scanf("%d",&search);
for(i=0;i<n;i++)
{
if(search==a[i])
{
printf("%d is present at location %d\n",search,i+1);
count++;
}
}
if(count==0)
{
printf(" %d is not present in the list",search);
}
else
{
printf("%d is present %d times in the list",search,count);
}
getch();
Mr K Praveen Kumar, SMC, SHIRVA P a g e 11 | 28
}
Output1:
Enter the total num of elements: 10
Enter the 10 elements:
1
2
3
4
5
1
2
3
4
5
Enter the searching element:4
4 is present at location 4
4 is present at location 9
4 is present 2 times in the list

Output2:
Enter the total num of elements: 5
Enter the 5 elements:
1
2
3
4
5
Enter the searching element:6
6 is not present inthe list

Mr K Praveen Kumar, SMC, SHIRVA P a g e 12 | 28


Part- B
/****************************************************************
Part B -1 Program to find the largest and smallest elements with their
position in a one-dimensional array
**********************************************************************/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20];
int c,size,big,low,pos,loc;
pos=loc=0;
clrscr();
printf("Enter n number to store in an array:\n");
scanf("%d",&size);
printf(" Enter %d values :\n",size);
for(c=0;c<size;c++)
scanf("%d",&a[c]);//90,50,120,10,75
printf("\n Elements of array a:\n");
for(c=0;c<size;c++)
printf("%d\n",a[c]);
big=low=a[0];
for(c=1;c<size;c++)
{
if(a[c]>big)
{
big=a[c];
pos=c;
}

Mr K Praveen Kumar, SMC, SHIRVA P a g e 13 | 28


if(a[c]<low)
{
low=a[c];
loc=c;
}
}
printf("\nThe biggest value : %din the postion:%d", big,pos+1);
printf("\nThe lowest value :%din the postion:%d",low,loc+1);
getch();
}
OUTPUT:
Enter dimension :5

Enter 5 values :
50
40
1
2
10

Elements of array a:
50
40
1
2
10

The biggest value : 50in the postion:1


The lowest value :1in the postion: 3

Mr K Praveen Kumar, SMC, SHIRVA P a g e 14 | 28


/****************************************************************
Part B -2 Program to read ‘n’ integer values into a single dimension array
and arrange them in ascending order using bubble sort method
*****************************************************************************/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100];
int i, n,j,temp;
clrscr();
printf("Enter total no of elements:");
scanf("%d",&n);
printf("Enter the array elements one by one:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Given list:\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
/*Bubble sort*/
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
output:
}
Enter total no of elements:5
printf("\n Sorted list:\n");
Enter the array elements one by one:
for(i=0;i<n;i++)
45 12 11 98 56
printf("%d\t",a[i]);
Given list:
getch();
45 12 11 98 56
}
Sorted list:
11 12 45 56 98

Mr K Praveen Kumar, SMC, SHIRVA P a g e 15 | 28


/****************************************************************
Part B -3 Menu driven Program to perform addition and multiplication of
two Matrices
****************************************************************/
#include <stdio.h>
#include<conio.h>
void main()
{
int C[10][10], A[10][10], B[10][10];
int i, j, ch, rows, cols,l;
clrscr();
printf("Enter number of Rows:");
scanf("%d", &rows);
printf("Enter number of Columns:");
scanf("%d", &cols);
printf("Enter elements in matrix A:\n");
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
printf("\t");
scanf("%d", &A[i][j]);
}
}
printf("Matrix A:\n");
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
printf("%d", A[i][j]);
printf("\t");
}
printf("\n");
}
printf("\n Enter elements in the matrix B:\n");
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
Mr K Praveen Kumar, SMC, SHIRVA P a g e 16 | 28
{
printf("\t");
scanf("%d", &B[i][j]);
}
}
printf("Matrix B:\n");
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
printf("%d", B[i][j]);
printf("\t");
}
printf("\n");
}
printf(" 1. Addition of Matrix A & B\n ");
printf("2. Multiplication of Matrix A & B\n");
printf("Enter your choice (1 ,2):\n");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("A+B=\n");
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
C[i][j] = A[i][j] + B[i][j];
printf("%d", C[i][j]);
printf("\t");
}
printf("\n");
}
break;

case 2:
printf("A*B=\n");
for (i = 0; i < rows; i++)
Mr K Praveen Kumar, SMC, SHIRVA P a g e 17 | 28
{
for (j = 0; j < rows; j++)
{
C[i][j] = 0;
for (l = 0; l < cols; l++)
{
C[i][j] = C[i][j] + A[i][l] * B[l][j];
}
printf("%d", C[i][j]);
printf("\t");
}
printf("\n");
}
break;
default:
printf("\n Wrong Choice");
}
getch();
}
Output:

Mr K Praveen Kumar, SMC, SHIRVA P a g e 18 | 28


/****************************************************************
Part B -4 Program to find nCr and nPr using recursive function to
calculate factorial
****************************************************************
#include<stdio.h>
#include<conio.h>
int main ()
{
int n,r,ncr,npr;
clrscr ();
printf ("Enter value of n and r:");
scanf ("%d%d",&n,&r);
npr = factorial(n)/factorial(n-r);
ncr = npr / factorial(r);
printf ("npr value is:%d\n",npr);
printf ("ncr value is:%d\n",ncr);
getch ();
}
/* Function to calculate factorial */
int factorial (int x)
{
int i,f=1;
for (i=2 ; i<=x ; i++)
f = f * i;
return (f);
}
****output***
Enter value of n and r: 3 2
npr value is:6
ncr value is:3

Mr K Praveen Kumar, SMC, SHIRVA P a g e 19 | 28


/********************************************************
Name :
Class : 1 BCA
Reg no :
PART B-5 Program to read a string and count number of letters, digits,
vowels, consonants, spaces and special characters present in it using user
defined function.
********************************************************** */
#include<stdio.h>
#include<conio.h>
int isVowel(char c);
int isConsonant(char c);
int isDigit(char c);
int isWhitespace(char c);
int isSpecial(char c);
void main()
{
char str[500];
int V = 0, C = 0, D = 0, W = 0,S=0,M=0,i;
clrscr();
printf("Enter a string\n");
gets(str);
for(i = 0;str[i] != '\0'; i++)
{
V += isVowel(str[i]);
C += isConsonant(str[i]);
D += isDigit(str[i]);
W += isWhitespace(str[i]);
S += isSpecial(str[i]);
M += isWhitespace(str[i]);

}
printf("Vowels: %d\n",V);
printf("Consonants: %d\n",C);
printf("Digits: %d\n",D);
printf("White spaces: %d\n",W);
printf("Total letters:%d\n",S);
printf("Special character:%d",M);
Mr K Praveen Kumar, SMC, SHIRVA P a g e 20 | 28
getch();
}

int isVowel(char c)
{

if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='I'||c=
='O'||c=='U')
{
return 1;
}
else
{
return 0;
}
}

int isConsonant(char c)
{
if(((c>='a'&& c<='z') || (c>='A'&& c<='Z')) && !isVowel(c))
{
return 1;
}
else
{
return 0;
}
}
int isDigit(char c)
{
if(c>='0'&&c<='9')
{
return 1;
}
else
{
return 0;
}
Mr K Praveen Kumar, SMC, SHIRVA P a g e 21 | 28
}
int isWhitespace(char c)
{
if(c == ' ')
{
return 1;
}
else
{
return 0;
}
}
int isSpecial(char c)

{
if(c==' ')
{
return 0;
}
else
{
return 1;
}
}
Output:
Enter a string
Smc 123 shirva @@@ BCA’s
Vowels:3
Consonants:10
Digits:3
White spaces:4
Total letters:20
Special character:4

Mr K Praveen Kumar, SMC, SHIRVA P a g e 22 | 28


/*************************************************************************
PARTB- 6 Program sort a list of strings in ascending order using Pointers
*************************************************************************/
#include<stdio.h>
#include<conio.h>
void main()
{
char *T;
int I,J,K,n;
char *ARRAY[100];
clrscr();
printf("Enter how many strings you want\n");
scanf("%d",&n);
for(I=0;I<n;I++)
{
printf("enter %d string\n",I+1);
ARRAY[I]=(char *)malloc(20*sizeof(char));
scanf("%s",ARRAY[I]);
}
printf("Entered strings are\n");
for(I=0;I<n;I++)
{
printf("%s\t",ARRAY[I]);
}
printf("\n sorted list are \n");
for(I=0;I<n;I++)
{
for(J=0;J<n-I;J++)
{
K=strcmp(ARRAY[J],ARRAY[J+1]);
if(K>0)
{
T=ARRAY[J];
ARRAY[J]=ARRAY[J+1];
ARRAY[J+1]=T;
}
}
}
Mr K Praveen Kumar, SMC, SHIRVA P a g e 23 | 28
for(I=0;I<5;I++)
{
printf("%s \t",ARRAY[I]);
}
getch();
}
OUTPUT:

/********************************************************
PARTB- 7 Program to enter the information of a student like name, register
number, marks in three subjects into a structure and display total,
average and grade Display details in a neat form.
********************************************************** */
#include<stdio.h>
#include<conio.h>
struct student
{
int regno;
char name[10];
int m1;
int m2;
int m3;
};
Mr K Praveen Kumar, SMC, SHIRVA P a g e 24 | 28
void main()
{
struct student s[10];
int n,i,total;
float avg;
clrscr();
printf("**********OUTPUT**********");
printf("\nEnter the number of students:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the regno :");
scanf("%d",&s[i].regno);
fflush(stdin);
printf("\nEnter the student name:");
gets(s[i].name);
printf("\nEnter the marks in 3 subjects:");
scanf("%d %d %d",&s[i].m1,&s[i].m2,&s[i].m3);
printf("\n...............................\n");
}
printf("\nRNO\tNAME\tM1\tM2\tM3\tTOTAL\tAVERAGE\t\tGRA
DE\n ");
for(i=0;i<n;i++)
{
total=s[i].m1+s[i].m2+s[i].m3;
avg=(total/3.0);
printf("%d\t",s[i].regno);
printf("%s\t",s[i].name);
printf("%d\t %d\t %d\t %d \t %f\t",
s[i].m1,s[i].m2,s[i].m3,total,avg);
if(s[i].m1<35||s[i].m2<35||s[i].m3<35)
{
printf("fail");
}
else if(avg>=70)
{
printf("distinction");
}
Mr K Praveen Kumar, SMC, SHIRVA P a g e 25 | 28
else if(avg>=60)
{
printf("first class");
}
else if(avg>=50)
{
printf("second class");
}
else {
printf("third class");
}
printf("\n");
}
getch();
}
Output:
Enter the number of students:2
enter the regno :1
Enter the student name:naveen
Enter the marks in 3 subjects:
85 85 85
...............................
enter the regno :2
Enter the student name:sudeep
Enter the marks in 3 subjects:
45 25 35
RNO NAME M1 M2 M3 TOTAL AVERAGE GRADE
1 naveen 85 85 85 255 85.000000 distinction
2 sudeep 45 25 35 105 35.000000 fail

Mr K Praveen Kumar, SMC, SHIRVA P a g e 26 | 28


/***********************************************************************************
PARTB- 8 Program to input Name of the branches, Total sales of company
into an array of structures. Display branch details in a tabular format.
Also display the branch name that recorded the highest sales.
*********************************************************************************/
#include<stdio.h>
#include<conio.h>
struct cominfo
{
char bname[10];
int sales;
};
void main()
{
struct cominfo com[10],max;
// declaring structure and array of structure variable
int n,i;
clrscr();
printf("***********output***********\n");
printf("Enter the number of branches:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
fflush(stdin);
printf("Enter branch name:\n");
gets(com[i].bname);
printf("Enter total sales:\n");
scanf("%d",&com[i].sales);
}
max=com[0];
for(i=1;i<n;i++)
{
if(com[i].sales>max.sales)
max=com[i];
}
printf("BRANCH\tTOTAL SALES\n");
printf("--------------------------------\n");
for(i=0;i<n;i++)
Mr K Praveen Kumar, SMC, SHIRVA P a g e 27 | 28
{
printf("%s\t%d\n",com[i].bname,com[i].sales);
}
printf("\nMaximum sales of");
printf("\nBranch name=%s",max.bname);
printf("\nTotal sales=%d",max.sales);
getch();
}

***********output***********
Enter the number of branches:3
Enter branch name:
MYSORE
Enter total sales:
5266
Enter branch name:
BANGALORE
Enter total sales:
5690
Enter branch name:
MANGALORE
Enter total sales:
9526
BRANCH TOTAL SALES
--------------------------------
MYSORE 5266
BANGALORE 5690
MANGALORE 9526

Maximum sales of
Branch name=MANGALORE
Total sales=9526

Mr K Praveen Kumar, SMC, SHIRVA P a g e 28 | 28

You might also like