ST.
XAVIER’S COLLEGE
Maitighar, Kathmandu
(Affiliated to National Examinations Board)
C programming
Lab Report
Submitted by:
Bikrant Lekhak
023NEB023
Grade 11 “G”
Submitted to:
Mr. Jaya Sundar Shilpakar
(Lecturer)
Department of Computer Science
Table of Contents
To print hello world ...................................................................................................................................... 1
Output: ....................................................................................................................................................... 1
To perform arithmetic operation ................................................................................................................ 1
Output: ....................................................................................................................................................... 2
Perform increment and decrement operation ......................................................................................... 2
Output: ....................................................................................................................................................... 3
Code to display the size of int, char, float and double................................................................................... 3
Output: ....................................................................................................................................................... 3
Code to take marks of 5 subjects and calculate average ..................................................................... 4
Output: ....................................................................................................................................................... 4
Code to convert cm to m taking user input ............................................................................................. 4
Output: ....................................................................................................................................................... 5
Code to calculate the volume of cuboid .................................................................................................. 5
Output: ....................................................................................................................................................... 5
Code to take user input money in dollar and convert it in Nepali rupees .......................................... 6
Output: ....................................................................................................................................................... 6
Code to display smallest among 2 numbers .......................................................................................... 6
Output: ....................................................................................................................................................... 7
To print hello world
//program to display hello word #include<stdio.h>//header
file
int main()// main function
{
printf("Hello world");//print function
return 0;//returning value to the function
}
Output:
To perform arithmetic operation
#include<stdio.h>//header file int
main()// main function
{
// declare integer data type variable
int a, b; int sum, sub,
mult, mod;
float div;
//Input two numbers from user
printf("Enter any two numbers: ");
scanf("%d%d", &a, &b);
//Perform all arithmetic operations
sum = a + b; sub = a - b; mult =
a * b; div = (float)a/b;
mod = a % b;
//Print result of all arithmetic operations
printf("SUM = %d\n", sum); printf("DIFFERENCE
= %d\n", sub); printf("PRODUCT = %d\n", mult);
printf("QUOTIENT = %f\n", div);
printf("MODULUS = %d", mod);
return 0; //returning value to the function
}
1
Output:
Perform increment and decrement operation
//Perform increment and decrement operation
#include <stdio.h>
int main() {
int x = 5; int
result;
// Post-increment (x++): Use the current value of x, then increment.
result = x++;
printf("Post-increment (x++): x = %d, result = %d\n", x, result);
x = 5; // Reset x to its initial value
// Pre-increment (++x): Increment first, then use the updated value.
result = ++x;
printf("Pre-increment (++x): x = %d, result = %d\n", x, result);
x = 5; // Reset x to its initial value
// Post-decrement (x--): Use the current value of x, then decrement.
result = x--;
printf("Post-decrement (x--): x = %d, result = %d\n", x, result);
x = 5; // Reset x to its initial value
// Pre-decrement (--x): Decrement first, then use the updated value.
result = --x;
printf("Pre-decrement (--x): x = %d, result = %d\n", x, result);
return 0;
}
2
Output:
Code to display the size of int, char, float and double.
//code to display the size of int, char, float and double.
#include<stdio.h> int
main() { int
intType; float
floatType; double
doubleType;
char charType;
// size of evaluates the size of a variable printf("Size of
int: %zu bytes\n", sizeof(intType)); printf("Size of float:
%zu bytes\n", sizeof(floatType)); printf("Size of double:
%zu bytes\n", sizeof(doubleType)); printf("Size of char:
%zu byte\n", sizeof(charType));
return 0; }
Output:
3
Code to take marks of 5 subjects and calculate average.
//code to take marks of 5 subjects and calculate average
#include<stdio.h>
int main() {
float eng, phy, chem, math, comp;
float total, average;
/* Input marks of all five subjects */
printf("Enter marks of five subjects: \n");
scanf("%f%f%f%f%f", &eng, &phy, &chem, &math, &comp);
// Calculate total and average// total =
eng + phy + chem + math + comp;
average = total / 5.0;
/* Print all results */ printf("Total marks
= %.2f\n", total); printf("Average marks =
%.2f\n", average);
return 0;
}
Output:
Code to convert cm to m taking user input.
//code to convert cm to m taking user input.
#include<stdio.h> int
main() {
float c, m;
// c = centimeter, m = meter
printf("Enter the length in centimeter::\n");
scanf("%f", &c);
4
// It will convert centimeters into metres
m = (float)(c / 100);
// It will print the final output
printf("\nLength in Meter = %f meter \n", m);
return 0; }
Output:
Code to calculate the volume of cuboid
//to calculate volume of a cuboid
#include<stdio.h> int
main(){
float l,b,h,V;
printf("Enter length, breadth and height\n"); //input length, breadth and height from user
scanf("%f%f%f",&l,&b,&h);
//calculating volume
V=l*b*h;
printf("Volume of cuboid is %f\n",V); //printing the volume
return 0;
}
Output:
5
Code to take user input money in dollar and convert it in Nepali rupees
//code to take user input money in dollar and convert it in Nepali rupees
#include<stdio.h> int
main(){
int c,d;
printf("Enter the dollar money\n");//taking input from the user//
scanf("%d",&d);
c=132*d; //multiplying given input with standard value of $1 in
Rs// printf("The required Nepali rupees is %d\n",c); return 0;
}
Output:
Code to display smallest among 2 numbers.
//Code to display smallest among 2 numbers
#include<stdio.h> int
main(){
int a,b;
printf("Enter two numbers\n");
scanf("%d%d",&a,&b);
if(a<b)
printf("Smallest number is %d\n",a);
else
printf("Smallest number is %d\n",b);
return 0;
}
6
Output: