0% found this document useful (0 votes)
15 views

Lab-report-SwoyamCS-11

This document is a lab report on C programming submitted by Mr. Swoyam Govinda to Hari Chandra Thapa, detailing various programming exercises. It includes source code and outputs for multiple C programs, covering topics such as arithmetic operations, area calculations, temperature conversion, and matrix operations. Each lab section presents a specific programming task along with its implementation and expected output.

Uploaded by

tikshanluitel
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)
15 views

Lab-report-SwoyamCS-11

This document is a lab report on C programming submitted by Mr. Swoyam Govinda to Hari Chandra Thapa, detailing various programming exercises. It includes source code and outputs for multiple C programs, covering topics such as arithmetic operations, area calculations, temperature conversion, and matrix operations. Each lab section presents a specific programming task along with its implementation and expected output.

Uploaded by

tikshanluitel
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/ 22

Mahalaxmisthan, Lalitpur

“A Lab Report on C- Programming”

Submitted by : Submitted to :
Name: Mr. Swoyam Govinda Hari Chandra Thapa
Amatya

Grade: 11 Department of CS
Roll no: 29 …………………
Symbol no: 002550029 Date: 2080/ /

1
Lab 1: Write a C program to input 3 numbers and find sum and average.
Source Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,sum;
float avg;
printf("\nEnter first number ");
scanf("%d",&a);
printf("\nEnter Second number ");
scanf("%d",&b);
printf("\nEnter Third number ");
scanf("%d",&c);
sum=a+b+c;
avg=sum/3;
printf("\nThe sum is %d",sum);
printf("\nThe average is %.2f",avg);
}
Output

2
Lab 2: Write a C program to find greatest number among 3 number.
Source Code:
#include<stdio.h>
void main()
{
int a,b,c;
printf("\nEnter first number ");
scanf("%d",&a);
printf("\nEnter first number ");
scanf("%d",&b);
printf("\nEnter first number ");
scanf("%d",&c);
if((a>b)&(a>c))
{ printf("\n%d is the greatest number",a); }
else if((b>a)&(b>c))
{ printf("\n%d is the greatest number",b); }
else
{ printf("\n%d is the greatest number",c); }
}
}
Output

3
Lab 3: Write a C program to calculate area and circumference of a circle.
Source Code:
#include<stdio.h>
void main()
{
float radius, area, cf;
printf("Enter Radius of Circle\n");
scanf("%f",&radius);
//value of pi is 3.14
area=3.14*radius*radius;
printf("The area of Circle is %.2f",area);
cf=2*3.14*radius;
printf("\nThe Circumference of Circle is %.2f",cf);
}
Output

4
Lab 4: Write a C program to find square root and cube root of a number.
Use sqrt() and cbrt().
Source Code:
#include <stdio.h>
#include <math.h>
void main()
{
double number; printf("Enter a number: ");
scanf("%lf", &number); double squareRoot = sqrt(number);
printf("Square root of %.2lf = %.2lf\n", number, squareRoot);
double cubeRoot = cbrt(number);
printf("Cube root of %.2lf = %.2lf\n", number, cubeRoot);
}
Output

5
Lab 5 :Write a C program to calculate and print simple interest (SI) and
net amount(A), given that SI=PTR/100 and A=SI+P.
Source Code:
#include<stdio.h>
void main()
{
int P,T,R;
float SI,A;
printf("Enter Principal :");
scanf("%d",&P);
printf("Enter Tine:");
scanf("%d",&T);
printf("Enter Rate :");
scanf("%d",&R);
SI=(P*T*R)/100;
A=SI+P;
printf("The Simple intrest = %.2f \n",SI);
printf("The Net Amount = %.2f ",A);
}
Output:

6
Lab 6: Write a C program to convert temperature from centigrade(c)
into Fahrenheit(f). Use f=1.8*c+32
Source Code:
#include <stdio.h>
void main()
{
float celsius, fahrenheit;
printf("Enter temperature in Celsius: ");
scanf("%f", &celsius);
fahrenheit = 1.8 * celsius + 32;
printf("%.2f Celsius is equal to %.2f Fahrenheit\n", celsius, fahrenheit);
}
Output

7
Lab 7: Write a C program to calculate value of s using s=ut+1/2at.
Source Code:
#include <stdio.h>
void main()
{
float u, t, a, s;
printf("Enter initial velocity (u): ");
scanf("%f", &u);
printf("Enter time (t): ");
scanf("%f", &t);
printf("Enter acceleration (a): ");
scanf("%f", &a);
s = u * t + 0.5 * a * t * t;
printf("The value of s is: %.2f\n", s);
}
Output

8
Lab 8: Write a C program C to find total surface area of cuboid using
(TSA =2(lb+bh+hl)).
Source Code:
#include <stdio.h>
void main()
{
float l,b,h,tsa;
printf("Lenth: ");
scanf("%f",&l);
printf("Breath: ");
scanf("%f",&b);
printf("Height: ");
scanf("%f",&h);
tsa=2*(l*b+b*h+h*l);
printf("The TSA is %.2f",tsa);
}
Output

9
Lab 9: Write a C program to input a number and check whether it is odd
or even.
Source Code:
#include <stdio.h>
void main()
{
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if(num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", num);
}
Output

10
Lab 10: Write a C program to input a number and check whether it is
exactly divisible by 5 but not by 7.
Source Code:
#include<stdio.h>
void main()
{
int a;
printf("\n Enter Your number:");
scanf("%d",&a);
if (a%5==0 && a%7==0)
{
printf("\n %d is divisible by 5 and 7 \n ",a);
}
else
{
printf("\n %d is not divisible by 5 and 7 \n ",a);
}
}
Output

11
Lab 11: Write a C program to input a number and print multiplication
table of given number.
Source Code:
#include <stdio.h>
void main()
{
int n;
printf("Enter an integer: ");
scanf("%d", &n);
for (int i = 1; i <= 10; ++i)
{
printf("%d * %d = %d \n", n, i, n * i);
}
}
Output

12
Lab 12: Write a C program to generate Fibonacci series. [ 2, 3, 5, 8
……10th term].
Source Code:
#include <stdio.h>
void main()
{
int n = 10;
int fib[n];
int i;
fib[0] = 2;
fib[1] = 3;
for (i = 2; i < n; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
}
printf("The Fibonacci series up to the 10th term:\n");
for (i = 0; i < n; i++) {
printf("%d ", fib[i]);
}
printf("\n");
}
Output

13
Lab 13: Write a C program to calculate sum of n-natural number.
Source Code:
#include <stdio.h>
void main()
{
int n, sum = 0;
printf("Enter the value of n: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
sum += i;
}
printf("The sum of first %d natural numbers is: %d\n", n, sum);
}
Output

14
Lab 14: Write a C program to calculate factorial of a given number.
Source Code:
#include <stdio.h>
void main()
{
int n, i;
unsigned long long fact = 1;
printf("Enter an integer: ");
scanf("%d", &n);
if (n < 0)
{ printf("Error! Factorial of a negative number doesn't exist.");}
else {
for (i = 1; i <= n; ++i)
{
fact *= i;
}
printf("Factorial of %d = %llu", n, fact);
}
}
Output

15
Lab 15: Write a C program to reverse a given number.
Source Code:
#include <stdio.h>
void main()
{
int number, reversedNumber = 0, remainder;
printf("Enter a number: ");
scanf("%d", &number);
while (number != 0)
{
remainder = number % 10;
reversedNumber = reversedNumber * 10 + remainder;
number /= 10;
}
printf("Reversed number: %d\n", reversedNumber);
}
Output

16
Lab 16: Write a C program to check whether given string is palindrome
or not.
Source Code:
#include <stdio.h>
#include <string.h>
void main()
{
char str[100];
int i, len, flag = 1;
printf("Enter a string: ");
scanf("%s", str);
len = strlen(str);
for (i = 0; i < len / 2; i++) {
if (str[i] != str[len - i - 1]) {
flag = 0;
break;
}
}
if (flag)
printf("%s is a palindrome.\n", str);
else
printf("%s is not a palindrome.\n", str);
}
Output

17
Lab 17: Write a C program to arrange 10/n numbers in ascending order.
Source Code:
#include <stdio.h>
void main()
{ int n = 10,i,j,temp;
int arr[n];
printf("Enter %d numbers:\n", n);
for (i = 0; i < n; i++)
{ printf("Number %d: ", i + 1);
scanf("%d", &arr[i]); }
for (i = 0; i < n - 1; i++)
{ for (j = 0; j < n - i - 1; j++)
{ if (arr[j] > arr[j + 1])
{ temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp; } } }
printf("\nNumbers in ascending order:\n");
for (i = 0; i < n; i++)
{ printf("%d ", arr[i]); }
printf("\n");
}
Output

18
Lab 18: Write a C program to count the age between 15 and 25 of 20
employees.
Source Code:
#include <stdio.h>
void main()
{ int age, count = 0;
for (int i = 1; i <= 20; ++i) {
printf("Enter age of employee %d: ", i);
scanf("%d", &age);
if (age >= 15 && age <= 25)
{count++;}}
printf("Number of employees aged between 15 and 25: %d\n", count);
}
Output

19
Lab 19: Write a C program to read two 3x3 matrices and perform matrix
addition and multiplication.
Source Code:
#include <stdio.h>
void readMatrix(int matrix[3][3]) {
printf("Enter the elements of the matrix:\n");
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
printf("Enter element at position [%d][%d]: ", i + 1, j + 1);
scanf("%d", &matrix[i][j]);
}
}
}
void printMatrix(int matrix[3][3]) {
printf("The matrix is:\n");
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
void addMatrices(int matrix1[3][3], int matrix2[3][3], int result[3][3]) {
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
result[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
}
20
void multiplyMatrices(int matrix1[3][3], int matrix2[3][3], int result[3][3])
{
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
result[i][j] = 0;
for (int k = 0; k < 3; ++k) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
}
void main() {
int matrix1[3][3], matrix2[3][3], sum[3][3], product[3][3];
printf("Enter the first matrix:\n");
readMatrix(matrix1);
printf("Enter the second matrix:\n");
readMatrix(matrix2);
addMatrices(matrix1, matrix2, sum);
printf("\nMatrix Addition:\n");
printMatrix(sum);
multiplyMatrices(matrix1, matrix2, product);
printf("\nMatrix Multiplication:\n");
printMatrix(product);
}

21
Output

22

You might also like