0% found this document useful (0 votes)
44 views31 pages

C Lab Report

ufuyf

Uploaded by

prajwalbikram731
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
44 views31 pages

C Lab Report

ufuyf

Uploaded by

prajwalbikram731
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 31

INDEX

S.N Programs Page No Date Signature Remarks

1. Program to perform
arithmetic operation.
2. Program to compute area of circle

3. Program to find simple interest

4. Program to find power of a given


number.

5. Program to find 6 digit number in


reverse order.
6. Program to demonstrate whether a
given number is odd or even
7. Program to find roots of quadratic
equation
8. Write a program for calculator using
switch statement.
9. Program to find sum and average of
the 5 students using while loop.
10. Program to print half pyramid of *.

11. Program to get marks of 5 students


in an array and find the sum of the
marks and average.
12. Program to add two matrix

13. Program to perform multiplication


of matrix.
14. Program to transpose of matrix.

15, Program to perform sum of number


using function.
16. Program to compute factorial of a
number using recursion.
17. Write a program to create structure
with person1 variable.
18 Program to demonstrate nested
structure .
19. Program to illustrating use of
pointers.
20. Program to read and print
information of student using
structure.
21. Program to write to a text file.

22. Program to read from a text file.

23. Program to read and marks of


student and store it in file.
Program to perform arithmetic operation.

#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() {

float pie = 3.14;

int radius;

printf("Enter The Radius of Cicle:");

scanf("%d",&radius);

printf("The radius of the circle is %d\n" , radius);

float area = (float)(pie* radius * radius);

printf("The area of the given circle is %f", area);

return 0;

Output: The radius of the circle is 10

The area of the given circle is 314.000000


Program to find Simple Interest

# include <conio.h>
# include <stdio.h>
# include <stdlib.h>

int main(){

//Simple interset program


int principal, rate, time, interest;

printf("Enter the principal: ");


scanf("%d", &principal);

printf("Enter the rate: ");


scanf("%d", &rate);

printf("Enter the time: ");


scanf("%d", &time);

interest = principal * rate * time / 100;


printf("The Simple interest is %d", interest);

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>

//PROGRAM OF REVERS THREE DIGIT NUMBER WITHOUT LOOP

void main()

int num, temp;

clrscr();

printf("ENTER THE THREE DIGIT NUMBER:- ");

scanf("%d",&num);

printf("AFTER REVERSE RESULT IS:- ");

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);

// true if num is perfectly divisible by 2


if(num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", 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;

// condition for real and different roots


if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("root1 = %.2lf and root2 = %.2lf", root1, root2);
}

// condition for real and equal roots


else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
printf("root1 = root2 = %.2lf;", root1);
}

// if roots are not real


else {
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imagPart, realPart, imagPart);
}

return 0;
}

Enter coefficients a, b and c: 2.3


4
5.6
root1 = -0.87+1.30i and root2 = -0.87-1.30i
Write a program for calculator using switch statement.
#include <stdio.h>

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;
}

Enter an operator (+, -, *,): *


Enter two operands: 1.5
4.5
1.5 * 4.5 = 6.8
program to find sum and average of the mark of 5 students using while loop

#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);

printf("\nEnter elements of 1st matrix:\n");


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}

printf("Enter elements of 2nd matrix:\n");


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element b%d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}

// adding two matrices


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
sum[i][j] = a[i][j] + b[i][j];
}

// printing the result


printf("\nSum of two matrices: \n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", sum[i][j]);
if (j == c - 1) {
printf("\n\n");
}
}

return 0;
}
Output:

Enter the number of rows (between 1 and 100): 2


Enter the number of columns (between 1 and 100): 3

Enter elements of 1st matrix:


Enter element a11: 2
Enter element a12: 3
Enter element a13: 4
Enter element a21: 5
Enter element a22: 2
Enter element a23: 3
Enter elements of 2nd matrix:
Enter element b11: -4
Enter element b12: 5
Enter element b13: 3
Enter element b21: 5
Enter element b22: 6
Enter element b23: 3

Sum of two matrices:


-2 8 7

10 8 6
Program to perform multiplication of matrix.

#include <stdio.h>

int main()

int m, n, p, q, c, d, k, sum = 0;

int first[10][10], second[10][10], multiply[10][10];

printf("\nEnter the number of rows and columns of first


matrix:\n");

scanf("%d%d", &m, &n);

printf("\nEnter the elements of first matrix\n");

for ( c = 0 ; c < m ; c++ )

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

scanf("%d", &first[c][d]);*/

printf("\nEnter the number of rows and columns of second


matrix:\n");

scanf("%d%d", &p, &q);

if ( n != p )

{
printf("\nMatrices with entered orders can't be
multiplied with each other.\n");

printf("\nThe column of first matrix should be equal to


row of second.\n");

else

printf("\nEnter the elements of first matrix:\n");

for ( c = 0 ; c < m ; c++ )

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

scanf("%d", &first[c][d]);

printf("\nEnter the elements of second matrix:\n");

for ( c = 0 ; c < p ; c++ )

for ( d = 0 ; d < q ; d++ )

scanf("%d", &second[c][d]);

for ( c = 0 ; c < m ; c++ )

for ( d = 0 ; d < q ; d++ )

for ( k = 0 ; k < p ; k++ )

sum = sum + first[c][k]*second[k][d];


}

multiply[c][d] = sum;

sum = 0;

//Printing the final product matrix

printf("\nThe product of entered matrices is:\n");

for ( c = 0 ; c < m ; c++ )

for ( d = 0 ; d < q ; d++ )

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);

// asssigning elements to the matrix


printf("\nEnter matrix elements:\n");
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}

// printing the matrix a[][]


printf("\nEntered matrix: \n");
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
printf("%d ", a[i][j]);
if (j == c - 1)
printf("\n");
}

// computing the transpose


for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
transpose[j][i] = a[i][j];
}

// printing the transpose


printf("\nTranspose of the matrix:\n");
for (int i = 0; i < c; ++i)
for (int j = 0; j < r; ++j) {
printf("%d ", transpose[i][j]);
if (j == r - 1)
printf("\n");
}
return 0;
}
Output:

Enter rows and columns: 2


3

Enter matrix elements:


Enter element a11: 1
Enter element a12: 4
Enter element a13: 0
Enter element a21: -5
Enter element a22: 2
Enter element a23: 7

Entered matrix:
1 4 0
-5 2 7

Transpose of the matrix:


1 -5
4 2
0 7
Program to find sum of number using function.

#include<stdio.h>

int main() {
int num1, num2, res;

printf("\nEnter the two numbers : ");


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

res = sum(num1, num2);

printf("nAddition of two number is : ");


return (0);
}

int sum(int num1, int num2) {


int num3;
num3 = num1 + num2;
return (num3);
}

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;
}

long int multiplyNumbers(int n) {


if (n>=1)
return n*multiplyNumbers(n-1);
else
return 1;
}

Output:

Enter a positive integer: 6


Factorial of 6 = 720
Write a program to create structure with person1 variable.
#include <stdio.h>
#include <string.h>

// create struct with person1 variable


struct Person {
char name[50];
int citNo;
float salary;
} person1;

int main() {

// assign value to name of person1


strcpy(person1.name, "George Orwell");

// assign values to other person1 variables


person1.citNo = 1984;
person1. salary = 2500;

// print struct variables


printf("Name: %s\n", person1.name);
printf("Citizenship No.: %d\n", person1.citNo);
printf("Salary: %.2f", person1.salary);

return 0;
}

Output:

Name: George Orwell


Citizenship No.: 1984
Salary: 2500.00
Program to demonstrate Nested Structures.
#include <stdio.h>

struct complex {
int imag;
float real;
};

struct number {
struct complex comp;
int integer;
} num1;

int main() {

// initialize complex variables


num1.comp.imag = 11;
num1.comp.real = 5.25;

// initialize number variable


num1.integer = 6;

// print struct variables


printf("Imaginary Part: %d\n", num1.comp.imag);
printf("Real Part: %.2f\n", num1.comp.real);
printf("Integer: %d", num1.integer);

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 pointer pc: 2686784


Content of pointer pc: 22

Address of pointer pc: 2686784


Content of pointer pc: 11

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:

For roll number1,


Enter name: Tom
Enter marks: 98

For roll number2,


Enter name: Jerry
Enter marks: 89
.
.
.
Displaying Information:

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;

// use appropriate location if you are using MacOS or Linux


fptr = fopen("C:\\program.txt","w");

if(fptr == NULL)
{
printf("Error!");
exit(1);
}

printf("Enter num: ");


scanf("%d",&num);

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;

if ((fptr = fopen("C:\\program.txt","r")) == NULL){


printf("Error! opening file");

// Program exits if the file pointer returns NULL.


exit(1);
}

fscanf(fptr,"%d", &num);

printf("Value of n=%d", num);


fclose(fptr);

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;

printf("Enter number of students: ");

scanf("%d",&n);

FILE *fptr;

fptr=(fopen("C:\\student.txt","w"));

if(fptr==NULL) {

printf("Error!");

exit(1);

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

printf("For student%d\nEnter name: ",i+1);

scanf("%s",name);

printf("Enter marks: ");

scanf("%d",&marks);

fprintf(fptr,"\nName: %s \nMarks=%d \n",name,marks);

fclose(fptr);

return 0;

You might also like