C Programming Lab 1 by Bhanu
C Programming Lab 1 by Bhanu
Balkumari-7, Lalitpur
Lab Report
Of
C Programming
In partial fulfillment of C Programming, BCA
Faculty of Humanities and social Sciences, Tribhuvan University
Date: Signature:
Objective(s):
• To be familiar with syntax and structure of C-programming.
• To learn problem solving techniques using C.
Code:
//Following code is written and debugged is DEV C++
#include <stdio.h>
int main(){
//Start of the program
int h=10,w=12,d=8,vol;
// variable declaration and assign value to variable
vol=h*w*d;
//Calculation using mathematical formula
printf("%d is the required volume of CUBE.",vol);
//displaying the output
return 0;
//ends the main program
}
Output (Compilation, Debugging and Testing)
960 is the required volume of CUBE.
Lab exercise 1:
Output:
Lab exercise 2:
//write a program to add two numbers 5&9 and display it sum.//
#include<stdio.h>
int main ()
{
int a,b,c;
a=5;
b=9;
c=a+b;
printf("sum is %d",c);
Output:
Lab exercise 3:
#include<stdio.h>
int main ()
{
int p,q,r;
p= 8;
q= 5;
r= p*q;
printf("The product is %d",r);
}
Output:
Lab Exercise 4:
Output:
Lab exercise 5:
// write a program to calculate area of an ellipse having its axes
(minor=5cm, major=8cm)//
#include<stdio.h>
int main()
{
//const pi= 3.14;
float m1,m2,a;
m1= 8; //major
m2= 5; //minor
a= pi*m1*m2;
printf("The area of ellipse is %.2f",a);
return 0;
}
Output:
Lab exercise 6:
// write a program to calculate simple interest for a given P=2000, T=4, R=6.(SI=P*T*R/100)//
#include<stdio.h>
int main()
{
int p,t,r,si;
p=2000;
t=4;
r=6;
si=p*t*r/100;
printf("The simple interest is %d",si);
Output:
Lab Exercise: 7
// write a program to add, subtract, multiply and divided two whole numbers. //
#include<stdio.h>
int main() {
int a, b, c;
float d;
printf("Enter two whole numbers: \n");
scanf("%d%d", &a, &b);
c = a + b;
printf("\nSum = %d", c);
c = a - b;
printf("\n Difference = %d", c);
c = a * b;
printf("\n Multiplication = %d", c);
d = a / b;
printf("\n Division = %f", d);
return 0;
}
Output:
Lab Exercise: 8
//WAP to compute an area of the circle.//
#include<stdio.h>
int main() {
int PI = 3.1416;
float radius, area;
printf("\n enter the radius of a circle : ");
scanf("%f",&radius);
area = PI * radius * radius;
printf("\n The area of the circle with radius %f and Area \t is: %f", radius, area);
return 0;
}
Output:
Lab Exercise: 9
// WAP to convert number of days into days and month. //
Code:
#include<stdio.h>
int main() {
int months, days;
printf("Enter the days\n");
scanf("%d", &days);
months = days/30;
days = days%30;
printf("Months=%d Days=%d", months, days);
return 0;
}
Output:
Lab Exercise: 10
// Draw a flow chart and print even or odd number and display it. //
Flow chart:
Start
Yes
Is rem = 0 Display n is even number
No
End
Code:
#include<stdio.h>
int main() {
int n;
int rem= n%2;
printf("Enter a number: ");
scanf("%d", &n);
if(rem % 2) {
printf("\n The number %d is even.", n);
}
else {
printf("\n The number %d is odd.", n);
}
return 0;
}
Output:
Lab Exercise: 11
// WAP to find the largest among two integers using conditional operator.//
Code:
#include<stdio.h>
int main() {
int n1, n2, larger;
printf("Enter two numbers: \n");
scanf("%d %d", &n1, &n2);
larger = n1 > n2 ? n1 : n2;
printf("The larger number is: %d", larger);
return 0;
}
Output:
Lab Exercise: 12
// write a program to finds the smallest among three numbers using nested if else statement.//
Code:
#include<stdio.h>
int main () {
int n1, n2, n3;
printf("\n Enter 3 numbers: ");
scanf("%d %d %d", &n1, &n2, &n3);
if(n1<n2)
{
if(n1<n3)
{
printf("\n %d is the smallest number. ", n1);
}
else
{
printf("\n %d is the smallest number. ", n3);
}
}
else
{
if(n2<n3)
{
printf("\n %d is the smallest number. ", n2);
}
else
{
printf("\n %d is the smallest number. ", n3);
}
}
return 0;
}
Output:
Lab Exercise: 13
// write a program to find the largest among three numbers using else if statement. //
Code:
#include<stdio.h>
int main () {
int n1, n2, n3;
printf("\n Enter 3 numbers: \t ");
scanf("%d %d %d", &n1, &n2, &n3);
if(n1>n2 && n1>n3)
{
printf("\n %d is the greatest number. ", n1);
}
else if(n2>n1 && n2>n3)
{
printf("\n %d is the greatest number. ", n2);
}
else
{
printf("\n %d is the greatest number. ", n3);
}
return 0;
}
Output:
Lab Exercise: 14
// write a program to define switch statement and print and display it. //
Code:
#include<stdio.h>
int main() {
int choice;
printf("\n which of the following girls will date you ? ");
printf("\n Select 1 for Puja, 2 for Shreeya, 3 for Prakriti and 4 for Pratima.");
printf("\n 1 => Puja \n 2 => Shreeya \n 3 => Prakriti \n 4 => Pratima\n");
printf("\n Enter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("\n you are with Puja.");
break;
case 2:
printf("\n you are with Shreeya.");
break;
case 3:
printf("\n your are with Prakriti.");
break;
case 4:
printf("\n your are with Pratima.");
break;
default:
printf("\n Oops! you are entered wrong number. ");
}
return 0;
}
Output:
Lab Exercise: 15
// write a program to sum all integers from 1 to 500 using while loop. //
Code:
#include<stdio.h>
int main() {
int sum = 0;
int i = 1;
while (i <= 500 ) {
sum += i;
i++;
}
printf(" \t Sum is %d \n ", sum);
return 0;
}
Output:
Lab Exercise: 16
// write a program to find the sum and average of the mark of four subjects using while loop. //
Code:
#include<stdio.h>
int main() {
int marks, total, i;
float average;
total = 0;
i = 1;
while (i <=4 )
{
printf("\n Enter marks of %d subject: ", i);
scanf("%d", &marks);
total = total + marks;
i++;
}
average = (float)total / 4;
printf("\n The sum = %d\t and average marks of four subjects is:\t %f", total, average);
return 0;
}
Output:
Lab Exercise: 17
// write a program to print out all numbers from 1 to 15 using do-while loop. //
Code:
#include<stdio.h>
int main() {
int x = 1;
do {
printf(" %d\t ", x);
x++;
} while( x <=15 );
return 0;
}
Output:
Lab Exercise: 18
// write a program to print out all numbers from 1 to 100 using for loop. //
Code:
#include<stdio.h>
int main() {
int x;
for ( x = 1; x <= 100; x++)
printf("%d\t", x);
return 0;
}
Output:
Lab Exercise: 19
// write a program to calculate sum of first 20 even numbers. //
Code:
#include<stdio.h>
int main() {
int n, sum = 0;
for( n = 0; n <= 20; n = n + 2)
sum = sum + n;
printf("Sum of first 20 even number is %d ", sum);
getch();
}
Output: