KEMBAR78
C Lab - Main | PDF | Computer Science | Computer Standards
0% found this document useful (0 votes)
54 views25 pages

C Lab - Main

The document contains a series of C programming exercises grouped into two sections, Group A and Group B. Each exercise includes a specific task, such as calculating the sum of digits, checking for prime numbers, generating Fibonacci series, and handling strings and files. The document provides complete C code implementations for each task, demonstrating various programming concepts and techniques.

Uploaded by

gnana.svga
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)
54 views25 pages

C Lab - Main

The document contains a series of C programming exercises grouped into two sections, Group A and Group B. Each exercise includes a specific task, such as calculating the sum of digits, checking for prime numbers, generating Fibonacci series, and handling strings and files. The document provides complete C code implementations for each task, demonstrating various programming concepts and techniques.

Uploaded by

gnana.svga
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/ 25

GROUP – A

1. Write a C program to find the Sum of Digits.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,s;
clrscr();
printf("\nEnter an Integer Value:\n");
scanf("%d",&n);
r=s=0;
while (n>0)
{
r = n % 10;
s = s + r;
n = n / 10;
}
printf("Sum of the Digit : %d",s);
getch();
}
2. Write a C program to find whether a given number is Armstrong or not.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,tmp,r,s=0;
clrscr();
printf("Enter an Integer Value : \n");
scanf("%d",&n);
tmp=n;
while ( n > 0)
{
r = n % 10;
s = s + (r * r * r);
n = n / 10;
}
if (tmp == s)
printf("The given number %d is an Armstrong number",tmp);
else
printf("The given number %d is not an Armstrong number",tmp);
getch();
}
3. Write a C program to find whether a given number is Prime or not.

#include<stdio.h>
#include<conio.h>
void main()
{
int n, i, flag = 0;
clrscr();
printf("Enter an Integer Value : \n");
scanf("%d", &n);
for(i=1; i<=n; i++)
{
if (n % i == 0)
flag++;
}
if (flag == 2)
printf("%d is Prime", n);
else
printf("%d is NOT Prime", n);
getch();
}
4. Write a C program to generate the Fibonacci Series.

#include <stdio.h>
#include<conio.h>
void main()
{
int i, n;
int t1 = 0, t2 = 1;
int sum = t1 + t2;
clrscr();
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: %d, %d, ", t1, t2);
for (i = 3; i <= n; i++)
{
printf("%d, ", sum);
t1 = t2;
t2 = sum;
sum = t1 + t2;
}
getch();
}
5. Write a C program to find whether a given number is Adam or not.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int r,p,m,b,n,rev,a,c,i,z;
r=0;
rev=0;
clrscr();
printf("Enter an Integer Value : \n");
scanf("%d",&z);
n = z;
c = n * n;
while (n != 0)
{
m = n % 10;
r = r * 10 + m;
n = n / 10;
}
p = r * r;
while ( c != 0)
{
a = c % 10;
rev = rev * 10 + a;
c = c / 10;
}
if (rev == p)
printf("\n%d is an Adam Number",z);
else
printf("\n%d is not an Adam Number",z);
getch();
}
6. Write a C program to print reverse of the given number and string.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int n,s,r;
char x[100];
clrscr();
printf("\nEnter an Integer Value : \n");
scanf("%d",&n);
printf("\nEnter a String Value : \n");
scanf("%s",x);
s=r=0;
while(n > 0)
{
r = n % 10;
s = ( s * 10 + r);
n = n / 10;
}
printf("\nReversed Number : %d",s);
printf("\nReversed String : %s",strrev(x));
getch();
}
7. Write a C program to find minimum and maximum of N numbers using
array.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10];
int i,n,min,max;
clrscr();
printf("How many element do you insert?\n");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
min = max = a[0];
for(i=1; i<n; i++)
{
if (min > a[i])
min = a[i];
if (max < a[i])
max = a[i];
}
printf("Minimum Value in Array : %d\n",min);
printf("Maximum Value in Array : %d\n",max);
getch();
}
8. Write a C program to arrange the given number in ascending order.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,tmp,a[20];
clrscr();
printf("How many elements do you enter : \n");
scanf("%d",&n);
printf("Enter %d Elements : \n",n);
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1; j<n; j++)
{
if(a[i] > a[j])
{
tmp = a[j];
a[j] = a[i];
a[i] = tmp;
}
}
}
printf("\nElements in Ascending Order\n");
for(i=0; i<n; i++)
{
printf("%d\n",a[i]);
}
getch();
}
9. Write a C program to add and multiply two matrices.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3], b[3][3], c[3][3]={0}, d[3][3]={0};
int i,j,k,m,n,p,q;
clrscr();
printf("Enter No.of Rows and Columns for Matrix-A : \n");
scanf("%d %d",&m, &n);
printf("Enter No.of Rows and Columns for Matrix-B : \n");
scanf("%d %d",&p, &q);
if (m!=p || n!=q)
{
printf("Matrix Addition is not possible");
return;
}
else if(n!=p)
{
printf("Matrix Multiplication is not possible");
return;
}
else
{
printf("Enter Elements For Matrix-A :\n");
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("Enter Elements For Matrix-B :\n");
for(i=0; i<p; i++)
{
for(j=0; j<q; j++)
{
scanf("%d", &b[i][j]);
}
}
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
c[i][j] = a[i][j] + b[i][j];
}
}
printf("\nResult of Addition Matrix : \n");
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
printf("%d ", c[i][j]);
}
printf("\n");
}
for(i=0; i<p; i++)
{
for(j=0; j<q; j++)
{
for(k=0; k<p; k++)
{
d[i][j] += a[i][k] * b[k][j];
}
}
}
printf("\nResult of Multiplication Matrix : \n");
for(i=0; i<m; i++)
{
for(j=0; j<q; j++)
printf("%d ", d[i][j]);
printf("\n");
}
}
getch();
}
10. Write a C program to calculate NCR and NPR.

#include<stdio.h>
#include<conio.h>
int fact (int);
void main()
{
int n,r;
float ncr,npr;
clrscr();
printf("\nEnter Value for N and R ;");
scanf("%d %d",&n, &r);
ncr = fact(n) / (fact(r) * fact(n-r));
npr = fact(n) / fact(n-r);
printf("\nNCR Value : %f",ncr);
printf("\nNPR Value : %f",npr);
getch();
}
int fact (int n)
{
int i,f=1;
for(i=1; i<=n; i++)
{
f = f * i;
}
return(f);
}
GROUP - B

1. Write a C Program to find the grade of a student using else if ladder.

#include<stdio.h>
#include<conio.h>
void main()
{
int mark;
clrscr();
printf("Enter your Mark\n");
scanf("%d",&mark);
if( mark>0 && mark<100)
{
if (mark >= 35)
{
if (mark >= 80)
printf("First Class with Distinction");
else if (mark >= 60)
printf("First Class");
else if (mark >= 50)
printf("Second Class");
else if (mark >= 35)
printf("Third Class");
}
else
printf("Not Eligible for Class");
}
else
printf("Invalid Mark. Mark range from 0 to 100.");
getch();
}
2. Write a C program to implement the various string handling functions.

#include<stdio.h>
#include<conio.h>
void main()
{
char str1[25],str2[25];
int l;
clrscr();
printf("Enter two Strings\n");
scanf("%s %s", str1, str2);
l = strlen(str1);
printf("Length of the String1 : %d\n",l);
printf("String Reverse : %s\n", strrev(str1));
printf("Upper Case : %s\n", strupr(str2));
printf("Lower Case : %s\n", strlwr(str2));
if(strcmp(str1,str2) == 0)
printf("Strings are Equal\n");
else
printf("Strings are NOT Equal\n");
printf("String Concatination : %s\n", strcat(str1,str2));
strcpy(str2,str1);
printf("After String Copy\n");
printf("String1 Value : %s\n", str1);
printf("String2 Value : %s\n", str2);
getch();
}
3. Write a C program to create an integer file and displaying the even
numbers only.

#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp, *fp1;
int n,i;
clrscr();
fp = fopen("data.txt","w");
printf("Enter 10 Integer Values\n");
for(i=1; i<=10; i++)
{
scanf("%d",&n);
putw(n,fp);
}
fclose(fp);
fp=fopen("data.txt","r");
fp1=fopen("even.txt","w");
while( (n=getw(fp)) != EOF)
{
if (n % 2 == 0)
putw(n,fp1);
}
fclose(fp);
fclose(fp1);
fp1=fopen("even.txt","r");
printf("\nEven Numbers\n");
printf("*************\n");
while( (n=getw(fp1)) != EOF)
{
printf("%4d",n);
}
printf("\n\n");
fcloseall();
getch();
}
4. Write a C program to calculate quadratic equation.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a, b, c, d, r1,r2, real, imag;
clrscr();
printf("Enter coefficients a, b and c:\n");
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("Roots are: %.2f and %.2f",r1 , r2);
}
else if (d==0)
{
r1 = r2 = -b / (2*a);
printf("Roots are: %.2f and %.2f", r1, r2);
}
else
{
real = -b / (2*a);
imag = sqrt(-d) / (2*a);
printf("Roots are: %.2f+%.2fi and %.2f-%.2fi", real, imag, real,
imag);
}
getch();
}
5. Write a C program to count number of characters, words and lines in a
text file.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
FILE *fp;
char path[100], ch;
int c,w,l;
clrscr();
fp = fopen("file.dat","r");
c = w = l = 0;
while ((ch = fgetc(fp)) != EOF)
{
c++;
if (ch == '\n' || ch == '\0')
l++;
if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\0')
w++;
}
if (c > 0)
{
w++;
l++;
}
printf("\n");
printf("Total characters = %d\n", c);
printf("Total words = %d\n", w);
printf("Total lines = %d\n", l);
fclose(fp);
getch();
}
6. Write a C program to generate student mark list using array of
structures.

#include<stdio.h>
#include<conio.h>
struct student
{
char sname[50];
int sno, m1,m2;
};
void main()
{
struct student s[10];
int i,n;
clrscr();
printf("Enter Student Details to store:\n");
scanf("%d",&n);
for(i=0; i<n; i++)
{
s[i].sno = i+1;
printf("Enter Name:\n");
scanf("%s",s[i].sname);
printf("Enter Mark1 and Mark2:\n");
scanf("%d %d",&s[i].m1, &s[i].m2);
printf("\n");
}
printf("Student Information");
for(i=0; i<n; i++)
{
printf("\nInformation for Roll Number %d:\n",s[i].sno);
printf("Name : ");
puts(s[i].sname);
printf("Mark-1 : %.1d\n",s[i].m1);
printf("Mark-2 : %.1d\n",s[i].m2);
}
getch();
}
7. Write a C program to create and process the student mark list using file.

#include<stdio.h>
#include<conio.h>
void main()
{
FILE *F;
int rno,m1,m2,m3,tot,i,num;
char sname[15], res[10];
float avg;
clrscr();
F = fopen("student.dat", "w");
printf("Enter the No.of Students\n");
scanf("%d",&num);
for(i=0; i<num; i++)
{
printf("Enter Rollno, Name, Mark-1, Mark-2 and Mark-3\n");
scanf("%d %s %d %d %d", &rno, sname, &m1, &m2, &m3);
fprintf(F,"%d\n",rno);
fprintf(F,"%s\n",sname);
fprintf(F,"%d\n",m1);
fprintf(F,"%d\n",m2);
fprintf(F,"%d\n",m3);
}
fclose(F);

F = fopen("student.dat", "r");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("\t\tStudent Details\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
for(i=0; i<num; i++)
{
fscanf(F,"%d",&rno);
fscanf(F,"%s",&sname);
fscanf(F,"%d",&m1);
fscanf(F,"%d",&m2);
fscanf(F,"%d",&m3);
if (m1>=35 && m2>=35 && m3>=35)
strcpy(res,"Pass");
else
strcpy(res,"Fail");
tot = m1 + m2 + m3;
avg = tot / 3;
printf("Reg.No : %d\t\t Name : %s\n", rno, sname);
printf("Mark-1 : %d\t\t Mark-2 : %d\n", m1, m2);
printf("Mark-3 : %d\t\t Total : %d\n", m3, tot);
printf("Average: %.2f\t\t Result : %s\n", avg, res);

printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
}
fclose(F);
getch();
}
8. Write a C program to create and process pay bill using file.

#include<stdio.h>
#include<conio.h>
void main()
{
FILE *F;
int i, num, eno, bpay, npay, allow, ded;
char ename[15];

clrscr();
F = fopen("payroll.dat", "w");
printf("Enter the No.of Employees\n");
scanf("%d",&num);
for(i=0; i<num; i++)
{
printf("Enter Employ No, Name, Basic Pay, Allowances and
Deductions\n");
scanf("%d %s %d %d %d", &eno, ename, &bpay, &allow, &ded);
fprintf(F,"%d\n",eno);
fprintf(F,"%s\n",ename);
fprintf(F,"%d\n",bpay);
fprintf(F,"%d\n",allow);
fprintf(F,"%d\n",ded);
}
fclose(F);

F = fopen("payroll.dat", "r");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("\tEmployee Payroll Details\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
for(i=0; i<num; i++)
{
fscanf(F,"%d",&eno);
fscanf(F,"%s",&ename);
fscanf(F,"%d",&bpay);
fscanf(F,"%d",&allow);
fscanf(F,"%d",&ded);
npay = (bpay + allow) - ded;
printf("Emp No : %d\n", eno);
printf("Emp Name : %s\n", ename);
printf("Basic Pay : %d\n", bpay);
printf("Allowances : %d\n", allow);
printf("Deductions : %d\n", ded);
printf("Net Pay : %d\n", npay);

printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
}
fclose(F);
getch();
}
9. Write a C program to create and process inventory control using files.

#include<stdio.h>
#include<conio.h>
void main()
{
FILE *F;
int i, num, price, qty, rate;
char iname[15];

clrscr();
F = fopen("invent.dat", "w");
printf("Enter the No.of Items\n");
scanf("%d",&num);
for(i=0; i<num; i++)
{
printf("Enter Item Name, Rate, Quantity\n");
scanf("%s %d %d", iname, &rate, &qty);
fprintf(F,"%s\n",iname);
fprintf(F,"%d\n",rate);
fprintf(F,"%d\n",qty);
}
fclose(F);

F = fopen("invent.dat", "r");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("\tInventory Control\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
for(i=0; i<num; i++)
{
fscanf(F,"%s",&iname);
fscanf(F,"%d",&rate);
fscanf(F,"%d",&qty);
price = rate * qty;
printf("Item Name : %s\n", iname);
printf("Rate : %d\n", rate);
printf("Quantity : %d\n", qty);
printf("Price : %d\n", price);
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
}
fclose(F);
getch();
}
10. Write a C program to create and process electricity bill using file.

#include<stdio.h>
#include<conio.h>
void main()
{
FILE *F;
int cno, punit, cunit, tunit, amt, i,num;
char cname[15];

clrscr();
F = fopen("EB.dat", "w");
printf("Enter the No. of Consumers\n");
scanf("%d",&num);
for(i=0; i<num; i++)
{
printf("Enter Consumer No, Consumer Name:\n");
scanf("%d %s", &cno, cname);
printf("Enter Current Unit and Previous Unit:\n");
scanf("%d %d", &cunit, &punit);
fprintf(F,"%d\n",cno);
fprintf(F,"%s\n",cname);
fprintf(F,"%d\n",cunit);
fprintf(F,"%d\n",punit);
}
fclose(F);

F = fopen("EB.dat", "r");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("\tEB Bill Calculation\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
for(i=0; i<num; i++)
{
fscanf(F,"%d",&cno);
fscanf(F,"%s",&cname);
fscanf(F,"%d",&cunit);
fscanf(F,"%d",&punit);
tunit = cunit - punit;
if (tunit <=100)
amt = tunit * 0;
else if ( tunit >100 && tunit<=250)
amt = tunit * 2;
else if ( tunit >250)
amt = tunit * 5;

printf("Consumer No : %d\n", cno);


printf("Consumer Name : %s\n", cname);
printf("Current Unit : %d\n", cunit);
printf("Previous Unit : %d\n", punit);
printf("Total Unit : %d\n", tunit);
printf("Bill Amount : %d\n", amt);
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
}
fclose(F);
getch();
}

You might also like