Triton SS & College
Seti Opi Marga Koteshwor-32, Kathmandu
Lab Report Of Computer Science on C Programming
Lab Report No: 01
Submitted By: Submitted to:
Name: Shivam Kumar Shah Computer Science Department
Roll: 36 Triton SS and College
Sec: 503
Lab 1: Basic C Programs
Theoretical Background
C programming is a foundational language in computer science, known for its efficiency and
control over system resources. It is widely used for system programming, developing operating
systems, and embedded systems. In this lab, we will explore basic C programs that perform
simple arithmetic operations, input/output operations, and mathematical calculations.
Objectives
1. To understand the basic structure of a C program.
2. To write and execute simple C programs.
3. To perform basic arithmetic operations and mathematical calculations.
4. To handle user input and display output.
Programs
1. Program to Add Two Numbers
#include <stdio.h>
int main() {
int num1, num2, sum;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
sum = num1 + num2;
printf("Sum: %d\n", sum);
return 0;
}
Output:
2. Program to Find Sum and Average of Two Numbers
#include <stdio.h>
int main() {
int num1, num2, sum;
float average;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
sum = num1 + num2;
average = (float)sum / 2;
printf("Sum: %d\n", sum);
printf("Average: %.2f\n", average);
return 0;
}
Output:
3. Program to Find the Area of a Triangle
include <stdio.h> int
main() {
float base, height, area;
printf("Enter base and height of the triangle: ");
scanf("%f %f", &base, &height);
area = 0.5 * base * height;
printf("Area of the triangle: %.2f\n", area);
return 0;
}
Output :
4. Program to Convert Kilometer to Meter
#include <stdio.h>
int main() {
float kilometer, meter;
printf("Enter distance in kilometers: ");
scanf("%f", &kilometer);
meter = kilometer * 1000;
printf("Distance in meters: %.2f\n", meter);
return 0;
}
Output :
5. Program to Find Volume of a Sphere
#include <stdio.h>
#include <math.h>
int main() {
float radius, volume;
printf("Enter radius of the sphere: ");
scanf("%f", &radius);
volume = (4.0 / 3.0) * 3.14 * pow(radius, 3);
printf("Volume of the sphere: %.2f\n", volume);
return 0;
}
Output :
6. Program to Find Square Root and Cube Root of a Number.
#include <stdio.h>
#include <math.h>
int main() {
float number, sqrtResult, cubeResult;
printf("Enter the number : ");
scanf("%f", &number);
sqrtResult=sqrt(number);
cubeResult=cbrt(number);
printf(" square root of number is %.2f",sqrtResult);
printf("cube root of number is %.3f",cubeResult);
return 0; }
Output :
7. Program to find the Value of Square Root of (a + b)
#include<stdio.h>
#include<math.h>
int main() {
float a,b,result;
printf("enter two numbers : ");
scanf("%f%f",&a,&b);
result=sqrt(a+b);
printf("square root of numbers (a+b) is %.2f:",result);
}
Output :
8. Program to Find the Value of v in Equation v2 =u2+2as
#include <stdio.h>
#include <math.h>
int main() {
float u, a, s, v;
printf("Enter initial velocity (u), acceleration (a), and distance (s): ");
scanf("%f %f %f", &u, &a, &s);
v = sqrt(pow(u, 2) + 2 * a * s);
printf("Final velocity (v): %.2f\n", v);
return 0;
}
Output:
9.Program to input Name,
#include <stdio.h> int main()
{
char name[50], address[100];
char grade;
printf("Enter name: ");
scanf(" %s", name);
printf("Enter address: ");
scanf("%s ", address);
printf("Enter grade: ");
scanf(" %c", &grade);
printf("\nStudent Details:\n");
printf("Name: %s\n", name);
printf("Address: %s\n", address);
printf("Grade: %c\n", grade);
return 0;
}
Output:
Conclusion
In this lab, we successfully wrote and executed several basic C programs. These programs helped
us understand the fundamental concepts of C programming, including input/output operations,
arithmetic calculations, and the use of mathematical functions. By completing these exercises,
we gained practical experience in writing, compiling, and running C programs, which is essential
for further studies in computer science and programming.
Triton SS & College
Seti Opi Marga, Koteshwor-32, Kathmandu
Lab Report Of Computer Science on C Programming
Lab Report No: 02
Submitted By: Submitted to:
Name: Shivam Kumar Shah Computer Science Department
Roll: 36 Triton SS and College
Class: 11
Sec: 503
Lab 2 : C Programming - Control Structures
Introduction
Control structures in C programming are used to control the flow of execution based on certain conditions
or loops. This lab focuses on various control structures such as if, if-else, else-if ladder, nested if-else,
and switch-case. We will solve several problems using these structures to understand their practical
applications.
Theory
1. If Statement
The if statement is used to execute a block of code only if a specified condition is true.
if (condition) {
// Code to execute if condition is true
}
2. If-Else Statement
The if-else statement allows you to execute one block of code if the condition is true and another block if
the condition is false.
if (condition) {
// Code to execute if condition is true
} else {
// Code to execute if condition is false
}
3. Else-If Ladder
The else-if ladder is used when multiple conditions need to be checked.
if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition2 is true
} else {
// Code to execute if all conditions are false
}
4. Nested If-Else
Nested if-else statements are used when a condition needs to be checked within another condition.
if (condition1) {
if (condition2) {
// Code to execute if both conditions are true
} else {
// Code to execute if condition1 is true but condition2 is false
}
} else {
// Code to execute if condition1 is false
}
5. Switch Case
The switch-case statement is used to execute one block of code among many based on the value of a
variable.
switch (expression) {
case constant1:
// Code to execute if expression equals constant1
break;
case constant2:
// Code to execute if expression equals constant2
break;
default:
// Code to execute if expression doesn't match any case
}
Programs
1. Check if a Number is Even or Odd
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0) {
printf("%d is even.\n", num);
} else {
printf("%d is odd.\n", num);
}
return 0;
}
Output:
2. Check if Two Numbers are Same
#include <stdio.h>
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
if (num1 == num2) {
printf("The numbers are same.\n");
} else {
printf("The numbers are different.\n");
}
return 0;
}
Output:
3. Print Name of Days Using Switch Case
#include <stdio.h>
int main() {
int day;
printf("Enter a number (1-7): ");
scanf("%d", &day);
switch (day) {
case 1: printf("Monday\n"); break;
case 2: printf("Tuesday\n"); break;
case 3: printf("Wednesday\n"); break;
case 4: printf("Thursday\n"); break;
case 5: printf("Friday\n"); break;
case 6: printf("Saturday\n"); break;
case 7: printf("Sunday\n"); break;
default: printf("Invalid day\n");
} return 0;
}
Output:
4. Check if a Number is Divisible by 4 and 8
#include<stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 4 == 0 && num % 8 == 0) {
printf("%d is divisible by both 4 and 8.\n", num);
} else {
printf("%d is not divisible by both 4 and 8.\n", num);
}
return 0;
}
Output :
5. Check Voting Eligibility
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age >= 18) {
printf("You are eligible to vote.\n");
} else {
printf("You are not eligible to vote.\n");
}
return 0;
}
Output :
6. Calculate Profit or Loss
#include <stdio.h>
int main() {
float costPrice, sellingPrice;
printf("Enter cost price and selling price: ");
scanf("%f %f", &costPrice, &sellingPrice);
if (sellingPrice > costPrice) {
printf("Profit: %.2f\n", sellingPrice - costPrice);
} else if (sellingPrice < costPrice) {
printf("Loss: %.2f\n", costPrice - sellingPrice);
} else {
printf("No profit, no loss.\n");
}
return 0;
}
Output :
7. Find Greatest Among Three Numbers
#include <stdio.h>
int main() {
int num1, num2, num3;
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);
if (num1 >= num2 && num1 >= num3) {
printf("%d is the greatest.\n", num1);
} else if (num2 >= num1 && num2 >= num3) {
printf("%d is the greatest.\n", num2);
} else {
printf("%d is the greatest.\n", num3);
}
return 0;
}
Output:
8. Find Smallest Among Three Numbers
#include <stdio.h>
int main() {
int num1, num2, num3;
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);
if (num1 <= num2 && num1 <= num3) {
printf("%d is the smallest.\n", num1);
} else if (num2 <= num1 && num2 <= num3) {
printf("%d is the smallest.\n", num2);
} else {
printf("%d is the smallest.\n", num3);
}
return 0;
}
Output:
9. Calculate Employee Salary
#include <stdio.h>
int main() {
int hours;
float salary;
printf("Enter working hours: ");
scanf("%d", &hours);
if (hours <= 8) {
salary = hours * 100;
} else {
salary = 8 * 100 + (hours - 8) * 120;
}
printf("Total salary: %.2f\n", salary);
return 0;
}
Output:
10. Classify Age
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age < 13) {
printf("Child\n");
} else if (age >= 13 && age < 20) {
printf("Teenager\n");
} else {
printf("Adult\n");
}
return 0;
}
Output:
11. Calculate Commission
#include <stdio.h>
int main() {
float amount, commission;
printf("Enter the amount sold: ");
scanf("%f", &amount);
if (amount <= 20000) {
commission = amount * 0.05;
} else if (amount > 20000 && amount <= 50000) {
commission = amount * 0.15;
} else {
commission = amount * 0.25;
}
printf("Commission: %.2f\n",
commission); return 0;
}
Output:
Conclusion
This lab session helped in understanding the practical implementation of various control structures in C
programming. By solving different problems, we learned how to use if, if-else, else-if ladder, nested if-
else, and switch-case statements effectively. These structures are fundamental in controlling the flow of
programs based on conditions and are widely used in real-world applications.
Triton SS & College
Seti Opi Marga Koteshwor-32, Kathmandu
Lab Report Of Computer Science on C Programming
Lab Report No: 03
Submitted By: Submitted to:
Name: Shivam Kumar Shah Computer Science Department
Roll: 36 Triton SS and College
Class: 11
Sec: 503
Lab 03: C Programming - Loops
Introduction: - Loops in C Programming
Loops are used in programming to repeat a block of code until a specific condition is met. C
programming provides three types of loops:
Theoretical Background
1. For Loop:
Syntax:
for (initialization; condition; increment/decrement) {
// Code to be executed
}
The for loop is used when the number of iterations is known beforehand.
2. While Loop:
Syntax:
while (condition) {
// Code to be executed
}
The while loop is used when the number of
iterations is not known and the loop continues as
long as the condition is true.
3. Do-While Loop:
Syntax:
do {
// Code to be executed
} while (condition);
The do-while loop is similar to the while loop,
but it guarantees that the block of code is executed at least once.
4. Nested Loops:
A loop inside another loop is called a nested loop. It can be any combination of for, while, or do-
while loops.
Lab Exercises
1. WAP to print 0, 2, 4, 6, ..., 100.
#include <stdio.h>
int main( ) {
for (int i = 0; i <= 100; i += 2) {
printf("%d ", i);
}
return 0;
}
Output:
2. WAP to print multiplication table of a number using the concept of loop.
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
for (int i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", num, i, num * i);
}
return 0;
}
Output :
3. WAP to find factorial of two numbers.
#include <stdio.h>
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
int fact1 = 1, fact2 = 1;
for (int i = 1; i <= num1; i++) {
fact1 *= i;
}
for (int i = 1; i <= num2; i++) {
fact2 *= i;
}
printf("Factorial of %d is %d\n", num1, fact1);
printf("Factorial of %d is %d\n", num2, fact2);
return 0;
}
Output :
4. WAP to print factors of a number.
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Factors of %d are: ", num);
for (int i = 1; i <= num; i++) {
if (num % i == 0) {
printf("%d ", i);
}
}
return 0;
}
Output :
5. WAP to check whether the entered number is palindrome or not.
#include <stdio.h>
int main() {
int num, reversed = 0, original;
printf("Enter a number: ");
scanf("%d", &num);
original = num;
while (num != 0) {
reversed = reversed * 10 + num % 10;
num /= 10;
}
if (original == reversed) {
printf("%d is a palindrome.\n", original);
} else {
printf("%d is not a palindrome.\n", original);
}
return 0;
}
Output :
6. WAP to find the sum of first 100 numbers.
#include <stdio.h>
int main() {
int sum = 0;
for (int i = 1; i <= 100; i++) {
sum += i;
}
printf("Sum of first 100 numbers is %d\n", sum);
return 0;
}
Output:
7. WAP to print Armstrong number.
#include <stdio.h>
#include <math.h>
int main() {
int num, original, remainder, n = 0;
float result = 0.0;
printf("Enter an integer: ");
scanf("%d", &num);
original = num;
while (original != 0) {
original /= 10;
++n;
}
original = num;
while (original != 0) {
remainder = original % 10;
result += pow(remainder, n);
original /= 10;
}
if ((int)result == num)
printf("%d is an Armstrong number.\n", num);
else
printf("%d is not an Armstrong number.\n", num);
return 0;
}
Output :
8. WAP to print and count total prime numbers between 1 and 100.
#include <stdio.h>
int main() {
int i, j, isPrime, count = 0;
for (i = 2; i <= 100; i++)
{
isPrime = 1;
for (j = 2; j <= i / 2; j++)
{
if (i % j == 0)
{
isPrime = 0;
break;
}
}
if (isPrime == 1)
{ printf("%d ", i); count++;}
}
printf("\nTotal prime numbers between 1 and 100: %d\n", count);
return 0;
}
Output:
9. WAP to print the series 1, 1/2, 1/3, 1/4, 1/5, ..., up to nth term.
#include <stdio.h>
int main() {
int n;
printf("Enter the number of terms: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
printf("1/%d ", i);
}
return 0;
}
Output :
Conclusion
In this lab, we explored various types of loops in C programming and applied them to solve
different problems. We used for, while, and do-while loops to perform tasks such as printing
series, calculating factorials, checking for palindromes, and identifying prime numbers.
Understanding and effectively using loops is fundamental in programming as they allow for
efficient and repetitive execution of code blocks.