0% found this document useful (0 votes)
16 views22 pages

Lab Report SGA

The document is a lab report on C programming submitted by Mr. Swoyam Govinda Amatya to Hari Chandra Thapa. It includes various C programs that cover topics such as calculating sums, averages, areas, temperature conversions, and matrix operations. Each lab section presents the source code and expected output for the respective programming tasks.

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 DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views22 pages

Lab Report SGA

The document is a lab report on C programming submitted by Mr. Swoyam Govinda Amatya to Hari Chandra Thapa. It includes various C programs that cover topics such as calculating sums, averages, areas, temperature conversions, and matrix operations. Each lab section presents the source code and expected output for the respective programming tasks.

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 DOCX, 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 Amatya Hari Chandra Thapa
Grade: 11 Department of CS
Roll no: 29 …………………
Symbol no: Date: 2080/ /

1
Lab 1: 1. Write a C program to input 3 numbers and find sum and average.
Source Code:
#include<stdio.h>
void main()
{
int a,b,c,sum,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 %d",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 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

5
Lab 5: .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;
// Input
printf("Enter a number: ");
scanf("%lf", &number);
// Calculate and display square root
double squareRoot = sqrt(number);
printf("Square root of %.2lf = %.2lf\n", number, squareRoot);
// Calculate and display cube root
double cubeRoot = cbrt(number);
printf("Cube root of %.2lf = %.2lf\n", number, cubeRoot);
}
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;
// Input temperature in Celsius
printf("Enter temperature in Celsius: ");
scanf("%f", &celsius);
// Convert Celsius to Fahrenheit
fahrenheit = 1.8 * celsius + 32;
// Display the result
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;
// Input initial velocity, time, and acceleration
printf("Enter initial velocity (u): ");
scanf("%f", &u);
printf("Enter time (t): ");
scanf("%f", &t);
printf("Enter acceleration (a): ");
scanf("%f", &a);
// Calculate the value of s
s = u * t + 0.5 * a * t * t;
// Display the result
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);
// true if num is perfectly divisible by 2
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 \n",a);
}
else
{
printf("\n %d is not divisible by 5 and 7 \n \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