C Lab Report
C Lab Report
1. Program to perform
arithmetic operation.
2. Program to compute area of circle
#include <stdio.h>
int main()
{
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);
return 0;
}
Output:
a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b=1
A program to compute area of circle.
=>
#include <stdio.h>
#include<conio.h>
int main() {
int radius;
scanf("%d",&radius);
return 0;
# include <conio.h>
# include <stdio.h>
# include <stdlib.h>
int main(){
return 0;
Output:
Enter the principal: 1000
Enter the rate: 2
Enter the time: 23
The Simple interest is 460
Program to find power of a given number.
=>
#include<stdio.h>
#include<math.h>
int main()
{
Float n, m, p;
Printf(“Enter two numbers:\n”);
Scanf(“%f%f”,&n,&m);
p = pow(n,m);
printf(“power = %f”,p);
return 0;
}
Output:
Enter two numbers:
5 4
Power = 625.000000
Program to print 6 digit number in reverse order.
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
scanf("%d",&num);
temp = num%10;
printf("%d",temp);
temp = num/10;
temp = temp%10;
printf("%d",temp);
temp = num/10;
temp = temp/10;
printf("%d",temp);
getch();
Output
ENTER THE THREE DIGIT NUMBER:- 386
AFTER REVERS NUMBER IS:- 683
Program to demonstrate whether the given number is odd or even.
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
return 0;
}
Enter an integer: -7
-7 is odd
Program to find roots of quadratic equation.
#include <math.h>
#include <stdio.h>
int main() {
double a, b, c, discriminant, root1, root2, realPart, imagPart;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf", &a, &b, &c);
discriminant = b * b - 4 * a * c;
return 0;
}
int main() {
char op;
double first, second;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &op);
printf("Enter two operands: ");
scanf("%lf %lf", &first, &second);
switch (op) {
case '+':
printf("%.1lf + %.1lf = %.1lf", first, second, first + second);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf", first, second, first - second);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf", first, second, first * second);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf", first, second, first / second);
break;
// operator doesn't match any case constant
default:
printf("Error! operator is not correct");
}
return 0;
}
#include<stdio.h>
#include<conio.h>
int main()
{
Int marks, total, I;
Float average;
total=0;
i=1;
while(i<=5)
{
Printf(“\n Enter marks of %d subject: ”,i);
Scanf(“%d”,&marks);
total = total + marks;
i++;
}
average =(float)total/5;
printf(“/n The sum =%d\t and average of marks of 5 Subjects is : %f”, total, average);
return 0;
}
Output:
Enter marks of 1 subject: 88
Enter marks of 2 subject:67
Enter marks of 3 subject:80
Enter marks of 4 subject: 94
Enter marks of 5 subject: 55
The sum= 384 and average of marks of five subjects is: 76.800003
Program to print Half Pyramid of *.
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= i; ++j) {
printf("* ");
}
printf("\n");
}
return 0;
}
Output
*
* *
* * *
* * * *
* * * * *
Program to get marks of 5 students in an array and find the sum of the marks and average.
Program to Add Two Matrices
#include <stdio.h>
int main() {
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d", &c);
return 0;
}
Output:
10 8 6
Program to perform multiplication of matrix.
#include <stdio.h>
int main()
int m, n, p, q, c, d, k, sum = 0;
scanf("%d", &first[c][d]);*/
if ( n != p )
{
printf("\nMatrices with entered orders can't be
multiplied with each other.\n");
else
scanf("%d", &first[c][d]);
scanf("%d", &second[c][d]);
multiply[c][d] = sum;
sum = 0;
printf("%d\t", multiply[c][d]);
printf("\n");
return 0;
}
Output:
Program to display the transpose of a matrix.
#include <stdio.h>
int main() {
int a[10][10], transpose[10][10], r, c;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
Entered matrix:
1 4 0
-5 2 7
#include<stdio.h>
int main() {
int num1, num2, res;
Output:
Enter the two numbers : 12 15
Addition of two number is : 27
Program to compute 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 = %ld", n, multiplyNumbers(n));
return 0;
}
Output:
int main() {
return 0;
}
Output:
struct complex {
int imag;
float real;
};
struct number {
struct complex comp;
int integer;
} num1;
int main() {
return 0;
}
Output:
Imaginary Part: 11
Real Part: 5.25
Integer: 6
Program for illustrating use of pointers.
#include <stdio.h>
int main()
{
int* pc, c;
c = 22;
printf("Address of c: %p\n", &c);
printf("Value of c: %d\n\n", c); // 22
pc = &c;
printf("Address of pointer pc: %p\n", pc);
printf("Content of pointer pc: %d\n\n", *pc); // 22
c = 11;
printf("Address of pointer pc: %p\n", pc);
printf("Content of pointer pc: %d\n\n", *pc); // 11
*pc = 2;
printf("Address of c: %p\n", &c);
printf("Value of c: %d\n\n", c); // 2
return 0;
}
Output:
Address of c: 2686784
Value of c: 22
Address of c: 2686784
Value of c: 2
Program to read and print information of student using structure
#include <stdio.h>
struct student {
char firstName[50];
int roll;
float marks;
} s[5];
int main() {
int i;
printf("Enter information of students:\n");
// storing information
for (i = 0; i < 5; ++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 < 5; ++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;
}
Output:
Enter information of students:
Roll number: 1
Name: Tom
Marks: 98
.
.
.
Program to write to a text file
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
FILE *fptr;
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
fprintf(fptr,"%d",num);
fclose(fptr);
return 0;
}
Program to read from a text file
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
FILE *fptr;
fscanf(fptr,"%d", &num);
return 0;
}
C Program to read name and marks of students and store it in file
#include <stdio.h>
int main() {
char name[50];
int marks,i,n;
scanf("%d",&n);
FILE *fptr;
fptr=(fopen("C:\\student.txt","w"));
if(fptr==NULL) {
printf("Error!");
exit(1);
for (i=0;i<n;++i) {
scanf("%s",name);
scanf("%d",&marks);
fclose(fptr);
return 0;