Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
11 views13 pages

C Lab Practicals

The document contains a series of C programming exercises covering various topics such as line patterns, electricity bill calculation, histogram generation, finding the largest number in an array, string manipulation, factorial computation using recursion, and file handling. Each section includes code snippets that demonstrate the implementation of these concepts. Additionally, it covers structures and unions for storing student information and managing student records.

Uploaded by

CS Archana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views13 pages

C Lab Practicals

The document contains a series of C programming exercises covering various topics such as line patterns, electricity bill calculation, histogram generation, finding the largest number in an array, string manipulation, factorial computation using recursion, and file handling. Each section includes code snippets that demonstrate the implementation of these concepts. Additionally, it covers structures and unions for storing student information and managing student records.

Uploaded by

CS Archana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

C LAB PRACTICALS

1.LINE PATTERN
#include <stdio.h>
#include<conio.h>
void main()
{
int i,j,n;
printf("\n Enter the number of lines for
pattern");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
{
printf("%d",j+1);
}
printf("\n");
}
getch();
}

2.ELECTRICITY BILL
#include<stdio.h>
void main()
{
int pu,cu,ac;
float price;
printf("\n Electricity Invoice");
printf("\n Enter the Pevious uint:");
scanf("%d",&pu);
printf("\n Enter the Current Unit:");
scanf("%d",&cu);
ac=cu-pu;
if(ac<100)
{
price=ac*0.4;
}
else if(ac>=100&&ac<200)
{
price=ac*1;
}
else if(ac>=200&&ac<500)
{
price=ac*3;
}
else
{
price=ac*5;
}
printf("\n Your Current Unit=%d",ac);
printf("\n Total Amount=%f",price);
}

3.HISTOGRAM
#include<stdio.h>
int main()
{
int n=10;
int arr[]={1,2,3,4,5,6,7,8,9,10};
printf("%s %13s %17s\
n","Element","Value","Histogram");
for(int i=0;i<n-1;i++)
{
printf("%7d %13d\t\t",i+1,arr[i]);
for(int j=0;j<arr[i];j++)
{
printf("*");
}
printf("\n");
}
return 0;
}

4.FIND THE LARGEST NUMBER IN AN ARRAY


#include<stdio.h>
int main()
{
int i,n;
float arr[100];
printf("Enter the number of elements(1 to
100):");
scanf("%d",&n);
for(i=0;i<n;++i){
printf("Enter number%d:",i+1);
scanf("%f",&arr[i]);
}
for(i=1;i<n;++i){
if(arr[0]<arr[i])
arr[0]=arr[i];
}
printf("Largest element =%.2f",arr[0]);
return 0;
}

5.CHECK AND PRINT THE STRING


#include<stdio.h>
int main(){
char line[150];
printf("Enter a string:");
fgets(line,sizeof(line),stdin);
for(int i=0,j;line[i]!='\0';++i)
{
while(!(line[i]>='a'&&
line[i]<='z')&&!
(line[i]>='A'&&line[i]<'Z')&& !(line[i]=='\
0'))
{
for(j=i;line[j]!='\0';++j){
line[j]=line[j+1];
}
line[j]='\0';
}
}
printf("Output String:");
puts(line);
return 0;
}

6.COPY STRING WITHOUT USING STRCPY()


#include<stdio.h>
int main()
{
char s1[100],s2[200],i;
printf("Enter string s1:");
fgets(s1,sizeof(s1),stdin);
for(i=0;s1[i]!='\0';++i){
s2[i]=s1[i];
}
s2[i]='\0';
printf("String s2:%s",s2);
return 0;
}

7.FACTORIAL OF A NUMBER USING RECURSION


#include<stdio.h>
long int multiplyNumbers(int n);
int main(){
int n;
printf("Enter a positive integer;");
scanf("%d",&n);
printf("Factorial of %d=
%d",n,multiplyNumbers(n));
return 0;
}
long int multiplyNumbers(int n){
if(n>=1)
return n*multiplyNumbers(n-1);
else
return 1; }
8.SUM AND AVERAGE USING USER-DEFINED FUNCTIONS
#include <stdio.h>
int sumTwoNum(int,int);
float averageTwoNum(int,int);
int main()
{
int number1,number2;
int sum;
float avg;
printf("\n Enter the first integer
number:");
scanf("%d",&number1);
printf("\n Enter the second integer
number:");
scanf("%d",&number2);
sum=sumTwoNum(number1,number2);
avg=averageTwoNum(number1,number2);
printf("Number1:%d,Number2:%d\
n",number1,number2);
printf("Sum:%d,Average:%f\n",sum,avg);
return 0;
}
int sumTwoNum(int x,int y)
{
int sum;
sum=x+y;
return sum;
}
float averageTwoNum(int x,int y)
{
float average;
return ((float)(x)+(float)(y))/2;
}
11.FILES
#include<stdio.h>
#include<conio.h>
void main()
{
char name[50];
int n,i,m,m1,m2,a,t;
FILE*fptr;
printf("\n\t STUDENT MARKLIST");
printf("\n\t ENTER THE NAME");
scanf("%s",name);
printf("\n\t ENTER THE NUMBER OF STUDENT
MARKLIST:");
scanf("%d",&n);
fptr=fopen("c:\\student.txt","w");
for(i=0;i<n;i++)
{
printf("\n\t**ENTER THE MARKS BELOW**");
printf("\n MARK IN PHYSICS:");
scanf("%d",&m);
printf("\n MARK IN CHEMISTRY:");
scanf("%d",&m1);
printf("\n MARK IN COMPUTER SCIENCE:");
scanf("%d",&m2);
t=m+m1+m2;
a=(m+m1+m2)/3;
printf("\n TOTAL:%d",t);
printf("\n AVERAGE MARKS:%d",a);
fprintf(fptr,"\n STUDENT NAME:%s",name);
fprintf(fptr,"\n PHYSICS
%d",m);fprintf(fptr,"\n CHEMISTRY %d",m1);
fprintf(fptr,"\n COMPUTER SCIENCE %d",m2);
fprintf(fptr,"\n TOTAL %d",t);
fprintf(fptr,"\n AVERAGE MARKS %d",a);
}
fclose(fptr);
getch();
}

12.POINTER
#include<stdio.h>
#include<conio.h>
int main()
{
int a;
int*ptr,*var;
ptr=&a;
var=ptr;

printf("enter the values of a:");


scanf("%d",&a);
printf("\n value of a:%d",a);
printf("\n address of a:%p",&a);
printf("\n address of ptr:%p",ptr);
printf("\n value at ptr:%p",&ptr);
printf("\n value at a:%d",*ptr);
printf("\n value at a:%d",*(&a));
printf("\n value at a:%d",**(&ptr));
printf("\n value at a:%d",*(ptr));
printf("\n value at a:%p",ptr);
printf("\n value at a:%p",&var);
printf("\n value at a:%d",*var);
printf("\n value at a:%p",*(&var));
printf("\n value at a:%d",(*(var)));
getch();
}

10.DISPLAY STUDENT’S RECORD UNION


#include<stdio.h>
#include<string.h>
union student
{
char name[20];
char subject[20];
float percentage;
};
int main()
{
union student record1;
union student record2;
printf("Union record1 values example\n");
strcpy(record1.name,"Raju");
printf("Name:%s\n",record1.name);
strcpy(record1.subject,"Maths");
printf("Subject:%s\n",record1.subject);
record1.percentage=86.50;
printf("Percentage:%f\
n",record1.percentage);
printf("Union record2 values example\n");
strcpy(record2.name,"Mani");
printf("Name:%s\n",record2.name);
strcpy(record2.subject,"Physics");
printf("Subject:%s\n",record2.subject);
record2.percentage=99.50;
printf("Percentage:%f\
n",record2.percentage);
return 0;
}
9.STORING STUDENT’S INFORMATION USING
STRUCTURE
#include<stdio.h>
struct student
{
char name[50];
int roll;
float marks;
}s;
void main()
{
printf("Enter information\n");
printf("Enter name:");
scanf("%s",s.name);
printf("Enter roll number:");
scanf("%d",&s.roll);
printf("Enter marks:");
scanf("%f",&s.marks);
printf("Display information:\n");
printf("Name:");
puts(s.name);
printf("Roll number:%d\n",s.roll);
printf("Marks:%f\n",s.marks);
}

You might also like