Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

C Program lab Manual

Manual
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

C Program lab Manual

Manual
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

C Program Lab Manual

1.To read the radius of the circle and to find area and circumference.

#include<stdio.h>

void main()

int radius;

float PI=3.14,area,circumference;

clrscr();

printf("\n Enter the radius of circle");

scanf("%d",&radius);

area=PI*radius*radius;

circumference=2*PI*radius;

printf("\n Area of a circle is:%f",area);

printf("\n Circumference of a circle is:%f",Circumference);

getch();

Output:

2. To read the numbers and find the biggest of three.

#include<stdio.h>

void main()

int num1,num2,num3;

clrscr();

printf("\n Enter any three numbers");

scanf("%d %d %d",&num1,&num2,&num3);

if(num1>=num2 && num1>=num3)

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


else if(num2>=num3)

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

else

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

getch();

Output:

3. To check whether the number is prime or not.

#include<stdio.h>

void main()

int num,count=0,i;

clrscr();

printf("\n enter a number \n");

scanf("%d",&num);

for(i=1;i<=num;i++)

if(num%i==0)

count++;

if(count==2)

printf("\n %d is a prime number\n",num);

else

printf("\n %d is not a prime number\n",num);

getch();

}
Output:

4. To find the root of quadratic equation.

#include<stdio.h>

#include<math.h>

void main()

int a,b,c,choice;

double disc,r1,r2,real,img;

clrscr();

printf("\n Enter a,b,c \n");

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

disc=(b*b)-(4*a*c);

if(disc>0)

choice=1;

else if(disc<0)

choice=2;

else

choice=3;

switch(choice)

case 1:

printf("\n Real and Distict Roots \n");

r1=(-b+sqrt(disc))/(2.0*a);

r2=(-b-sqrt(disc))/(2.0*a);

printf("\n root1=%0.2lf",r1);
printf("\n root2=%0.2lf",r2);

break;

case 2:

printf("\n Roots are Complex and Distict Roots \n");

real=-b/(2.0*a);

img=sqrt(abs(disc))/(2.0*a);

printf("\n root1=%0.2lf+i%0.2lf",real,img);

printf("\n root2=%0.2lf-i%0.2lf",real,img);

break;

case 3:

printf("\n Roots are Equal \n");

r1=-b/(2.0*a);

r2=-b/(2.0*a);

printf("\n root1=%0.2lf",r1);

printf("\n root1=%0.2lf",r2);

break;

default:printf("\n invalid inputs");

getch();

}
Output:

5. To read a number, find the sum of the digits, reverse the number and check it for palindrome.

#include<stdio.h>

#include<conio.h>

void main()

int num,orgnum;

int sum=0,rev=0,rem;

clrscr();

printf("Enter a number");

scanf("%d",&num);

orgnum=num;

while(num>0)

rem=num%10;

sum=sum+rem;

rev=rev*10+rem;

num=num/10;

printf("\n Sum of digits=%d",sum);

printf("\n Reversed number=%d",rev);


if(orgnum==rev)

printf("\n Number is palindrome");

else

printf("\n Number is not a palindrome");

getch();

Output:

6.To read the numbers from keyboard continuously till the user presses 999 and to find the sum of
only positive numbers.

#include<stdio.h>

#include<conio.h>

void main()

int num,sum=0;

clrscr();

do{

printf("\n Enter the number:\n");

scanf("%d",&num);

if(num>0 && num!=999)

sum=sum+num;

printf("\n Sum=%d",sum);

}while(num!=999);

printf("\n you have pressed 999:STOP\n");

getch();

}
Output:

7. To read percentage of marks and to display appropriate message. If a percentage is 70 and above-
Distinction, 60-69 – First Class, 50-59 – Second Class, 40-49 Pass, below 40 – Fail. (Demonstrate of if-
else ladder)

#include<stdio.h>

#include<conio.h>

void main()

float per;

clrscr();

printf("Enter percentage");

scanf("%f",&per);

if(per>=90)

printf("\n Grade=EXEMPLARY");

else if(per>=80&&per<90)

printf("\n Grade=OUTSTANDING");

else if(per>=70&&per<80)

printf("\n Grade=FIRST DIVISION WITH DISTINCTION");

else if(per>=60&&per<70)

printf("\n Grade=FIRST DIVISION");

else if(per>=50&&per<60)
printf("\n Grade=SECOND DIVISION");

else if(per>=35&&per<50)

printf("\n Grade=PASS CLASS");

else

printf("\n Grade=FAILS:RE-APPEAR");

getch();

Output:

8. To simulate a simple calculator with addition, subtraction, multiplication, division and it should
display the error message for division of zero using switch case.

#include<stdio.h>

int main()

int a,b,choice;

clrscr();

printf("Enter two integers");

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

printf("Enter choice:\n1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n");

scanf("%d",&choice);

switch(choice)

case 1:printf("Result:%d",a+b);

break;

case 2:printf("Result:%d",a-b);

break;

case 3:printf("Result:%d",a*b);

break;

case 4:if(b!=0)
printf("Result=%2f",(float)a/b);

else

printf("Error! Division by zero not possible");

break;

default:printf("Error!, Invalid choice");

break;

getch();

Output:

9. To read marks scored by n students and find the average of mark (Demonstration of single
dimensional array)

#include<stdio.h>

#include<conio.h>

void main()

int n,i;

float marks[10],tot_marks=0.0,avg_marks=0.0;

clrscr();

printf("\n Enter students counts(n):");

scanf("%d",&n);

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

printf("\n Enter marks scored by %d student:",i+1);

scanf("%f",&marks[i]);

tot_marks=tot_marks+marks[i];
}

avg_marks=tot_marks/n;

printf("\n Total marks=%0.2f",tot_marks);

printf("\n Average marks=%0.2f",avg_marks);

getch();

Output:
10.To remove duplicate elements in a single dimensional array
#include<stdio.h>
#include<conio.h>
Void main ()
{
int a[20],n,i,j,ele;
clrscr();
printf(“\n Enter arraylimit(n):”);
scanf(“%d”,&n);
for(i=0;i<n-1;i++)
{
printf(“\n Enter a[%d]:”,i);
scanf(“%d”,&a[i]);
}
//removal of duplicate
for(i=0;i<n-1;i++)
{
ele=a[i];
for(j=j+1;j<n;j++)
{
if(ele==a[j] &&a[j]!=-111)
{
printf(“\n %d duplicate entry!!!”,a[j]);
a[j]=-111;//set -111 for duplicate entry
}
}
}
printf(“\n Final Array List \n”);
for(i=0;i<n;i++)
{
If(a[i]!=-111)
printf(“%5d”,a[i]);
}
getch ();
}
Output:

11.To find the factorial of a number


#include<stdio.h>
#include<conio.h>
void main()
{
int num,i;
long int fact=1;
clrscr();
printf("\n Enter a number:");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
fact=fact*i;
}
printf("\n Factorial of %d=%ld",num,fact);
getch();
}
Output:

12. To generate Fibonacci Series


#include<stdio.h>
#include<conio.h>
void main()
{
int NXT,F=0,S=1;
int i,n;
clrscr();
printf("\n Enter series limit:");
scanf("%d",&n);
printf("\n ******FIBONACCI SERIES*****\n");
printf("%d\t%d",F,S);
for(i=3;i<=n;i++)
{
NXT=F+S;
printf("\t %d",NXT);
F=S;
S=NXT;
}
getch();
}
Output:

13.To demonstrate string functions (String Length, String Copy, String Concatenate, String
Comparison)
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char *str1="Bengaluru";
char *str2="Nagara";
clrscr();
printf("\n String1=%s Strin2=%s\n",str1,str2);
printf("\n 1.Length of %s=%d",str1,strlen(str1));
printf("\n 2.String copy in str2=%s",strcpy(str2,"city"));
printf("\n 3.Concatenation =%s",strcat(str1,str2));
printf("\n 4.Compare str1 & str2 %d",strcmp(str1,str2));
printf("\n 5.String in lowercase=%s",strlwr(str1));
printf("\n 6.String in Uppercase=%s",strupr(str1));
printf("\n 7.substring search=%s",strchr(str1,'N'));
printf("\n 8.Duplicate string=%s",strdup(str1));
printf("\n 9.String reverse=%s",strrev(str1));
printf("\n 10.set all characters to #=%s",strset(str1,'#'));
getch();
}
Output:

14. To find the length of a string without using built-in function.


#include<stdio.h>
#include<conio.h>
void main()
{
int len=0,i=0;
char *str;
clrscr();
printf("\n Enter a string");
scanf("%s",str);
while(str[i]!='\0')
{
len++;
i++;
}
printf("\n Length of a string=%d",len);
getch();
}
Output:

15. To read, display and add two n x m matrices using function.


#include <stdio.h>

int rows, columns;

/* adds two matrices and stores the output in third matrix */


void matrixAddition(int mat1[][10], int mat2[][10], int mat3[][10]) {
int i, j;

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


for (j = 0; j < columns; j++) {
mat3[i][j] = mat1[i][j] + mat2[i][j];
}
}
return;
}

int main() {
int matrix1[10][10], matrix2[10][10];
int matrix3[10][10], i, j;
clrscr();
/* get the number of rows and columns from user */
printf("Enter the no of rows and columns(<=10):");
scanf("%d%d", &rows, &columns);
if (rows > 10 || columns > 10) {
printf("No of rows/columns is greater than 10\n");
return 0;
}

/* input first matrix */


printf("Enter the input for first matrix:");
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
scanf("%d", &matrix1[i][j]);
}
}

/* input second matrix */


printf("Enter the input for second matrix:");
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
scanf("%d", &matrix2[i][j]);
}
}

/* matrix addtion */
matrixAddition(matrix1, matrix2, matrix3);

/* print the results */


printf("\nResult of Matrix Addition:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
printf("%5d", matrix3[i][j]);
}
printf("\n");
}
getch();
}

Output:

16.To read a string and to find the number of alphabets,digits,vowels,consonants,space and


special characters.
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char str[100],ch;
int acount=0,dcount=0,vcount=0,ccount=0,scount=0,spcount=0,i=0;
clrscr();
printf("Enter a string");
gets(str);
while(str[i]!='\0')
{
if(isalpha(str[i])
{
acount++;
ch=tolower(str[i]);
switch(ch)
{
case 'a':
case 'e':
case 'i':
case 'o';
case 'u': vcount++;
break;
default:ccount++;
}
}
else if(isdigit(str[i]))
dcount++;
else if(isspace(str[i]))
scount++;
else
spcount++;
i=i+1;
}
printf("\n no of Alphabets=%d",acount);
printf("\n no of vowels=%d",vcount);
printf("\n no of constants=%d",ccount);
printf("\n no of spaces=%d",scount);
printf("\n no of digits=%d",dcount);
printf("\n no of special symbols=%d",spcount);
getch();
}
Output:

17.To swap two numbers using pointers


#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,*ptr1,*ptr2,temp;
clrscr();
printf("Enter two numbers");
scanf("%d%d",&n1,&n2);
printf("\n Before swapping n1=%d n2=%d\n",n1,n2);
ptr1=&n1;
ptr2=&n2;
temp=*ptr1;
*ptr1=*ptr2;
*ptr2=temp;
printf("\n After swapping n1=%d n2=%d\n",n1,n2);
getch();
}
Output:

18.To demonstrate student structure to read & display records of n students


#include<stdio.h>
struct student{
char name[50];
int regno;
char grade;
} S[50];
void main()
{
int n,i;
clrscr();
printf("\n Enter student count \n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n enter %d student(regno,name,grade):",i+1);
scanf("%d %s %c",&S[i].regno,&S[i].name,&S[i].grade);
}
printf("\n Regno \t Student name \t Grade);
for(i=0;i<n;i++)
{
printf("\n %d \t %s \t %c",S[i].regno,S[i].name,S[i].grade);
}
getch();
}
Output:

19. To demonstrate the difference between structure and union for the following Student
name (String), Student roll no(integer), Student mark(float)
#include<stdio.h>
#include<conio.h>
struct Stud1
{
int regno;
char name[50];
float marks;
};
union Stud2
{
int regno;
char name[50];
float marks;
};
void main()
{
clrscr();
printf("\n size of stucture of Stud1=%d",sizeof(struct Stud1));
printf("\n size of stucture of Stud2=%d",sizeof(union Stud2));
getch();
}
Output:

20. To design the following pattern using nested for loop:


*
* *
* * *
* * * *
* * * * *
#include <stdio.h>
#include<conio.h>
int main()
{
int rows = 5,n,i,j,k;
clrscr();
// first loop to print all rows
for ( i = 0; i < rows; i++) {

// inner loop 1 to print white spaces


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

// inner loop 2 to print star * character


for ( k = 0; k < 2 * i + 1; k++)
{
printf("* ");
}
printf("\n");
}
getch();
}
Output:

You might also like