You are on page 1of 8

Computer Programming Record Book 2016 for 1st Semester Exam

Lab 1 (a) Area of Rectangle


Formula of Area of rectangle
Area = Length * Width
#include<stdio.h>
int main () {
float l,w;
float area;
printf("\n\nEnter Size of Each Sides of the rectangle:");
scanf("%f%f", &l, &w);
area = l*w;
printf("\n\nArea of rectangle is : %.3f", area);
return 0;}

Lab 1 (b) Area and circumference of circle with defining value of PI


C Program to calculate area of a circle
#include <stdio.h>
#include<conio.h>
#define PI 3.141
int main (){
float radius, area;
printf("\nEnter Radius of Circle:");
scanf("%f", &radius);
/*Area of Circle=PI x Radius x Radius */
area = PI*radius*radius;
printf("Area of Circle: %0.4f\n\n",area);
getch();
return 0; }
Lab 2 (a) Swap 2 Variables without Using Temporary Variable.
The idea is to get sum in one of two given numbers. The numbers can then be swapped
Using the sum and subtraction from sum.
#include<stdio.h>
int main(){
int x = 10, y = 5;
// Code to swap 'x' and 'y'
x = x + y; // x now becomes 15
y = x - y; // y now becomes 10
x = x - y; // x now becomes 5
printf("After Swapping: x = %d, y = %d" , x ,y);
return 0;}

Lab 2 (b) C Program to Find Profit and Loss


C program to calculate profit or loss
#include<stdio.h>
int main() {
int cp,sp,amt;
/*Reads cost price and selling price of a product*/
printf ("\nEnter Cost Price:");
scanf ("%d",&cp);
printf("\nEnter Selling Price:");
scanf("%d",&sp);
if(sp>cp)//Profit
{
amt=sp-cp;
printf("\nProfit =%d", amt);
}
else if (cp>sp)// Loss
{
amt=cp-sp;
printf("\nLoss=%d", amt);
}
else // No Profit No Loss
{
printf("\nNo profit No Loss.");
}
return 0;
Lab 2 (c) C Program to Find Area of Triangle based on the sides of triangle
Formula of area of any triangle:
Area=(s*(s-a)*(s-b)*(s-c))
Where s = (a+b+c)/2
#include<stdio.h>
#include<math.h>
int main(){
float a,b,c;
float s,area;
printf("\nEnter Size of Each Sides of Triangle:");
scanf("%f%f%f", &a, &b, &c);
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("Area of triangle is: %.3f",area);
return 0; }

Lab 3 (a) C Program to check whether the number is positive or negative

#include<stdio.h>
int main() {
int num;
/* Read number from user*/
printf("\nEnter any number :");
scanf("%d",&num);
if(num>0)
{
printf("\nNumber is Positive:");
}
else if(num<0)
{
printf("\nNumber is Negative:");
}
else
{
printf("\nNumber is Zero:");
}
return 0; }

Lab 3 (b) C Program to check whether the number is odd or even.


#include<stdio.h>
int main (){
int n;
printf("\nEnter an integer:");
scanf("%d", &n);
if(n%2==0)
printf("Even");
else
printf("Odd");
return 0;}
Lab 4 (a) C Program to check equilateral, scalene or isosceles triangle.
C program to check whether a triangle is Equilateral, Isosceles or Scalene.

#include <stdio.h>
int main()
{
int a, b, c; //a, b, c are three sides of a triangle
/* Reads all sides of a triangle */
printf("Enter three sides of triangle: ");
scanf("%d%d%d", &a, &b, &c);
if(a==b && b==c)
{
//If all sides are equal
printf("Equilateral triangle.\n");
}
else if(a==b || a==c || b==c)
{
//If two sides are equal
printf("Isosceles triangle.\n");
}
else
{
//If none sides are equal
printf("Scalene triangle.\n");
}
return 0;
}
Lab 4 (b) C Program to check whether the given year is leap year Definition of
leap year:
#include<stdio.h>
int main() {
int year;
printf("\nEnter any year:");
scanf("%d",&year);
if((( year%4==0)&&(year%100!=0))||(year%400==0) )
printf("%d is a leap year", year);
else
printf("%d is not a leap year", year);
return 0;}

Lab 5 (a) Write a c program to find the biggest among three numbers
# include<stdio.h>
int main(){
int a,b,c;
int big;
printf("\nEnter any there numbers:");
scanf("%d%d%d",&a,&b,&c);
if(a>b && a>c)
big = a;
else if(b>c)
big = b;
else
big = c;
printf("Largest number is: %d",big);
return 0;}
Lab 5 (b) Write a c program to add n numbers
#include<stdio.h>
int main (){
int n, sum = 0, i, value;
printf("\nEnter The Number of Integers You Want to Add\n");
scanf("%d", &n);
printf("\nEnter %d Integers\n",n);
for (i = 1; i<=n; i++)
{
scanf("%d", & value);
sum = sum + value;
}
printf("Sum of Entered Integers = %d\n", sum);
return 0;}

Lab 5 (c) Write a c program to find the sum of digits of a given number
#include<stdio.h>
int main() {
int n, t, sum = 0, remainder;
printf("\nEnter an integer\n");
scanf("%d", &n);
t = n;
while (t != 0)
{
remainder = t % 10;
sum = sum + remainder;
t = t / 10;
}
printf("Sum of digits of %d = %d\n", n, sum);
return 0;}
Lab 6 (b) Write a c programs to add to mxn matrices
#include <stdio.h>
int main() {
int m,n,i,j, first[10] [10], second [10][10], sum [10] [10];
printf("\nEnter the number of rows and columns of matrix\n");
scanf("%d%d",&m,&n);
printf("\nEnter the elements of first matrix\n");
for (i = 0; i<m; i++)
for(j = 0; j<n; j++)
scanf("%d", &first[i][j]);
printf("\nEnter the elements of second matrix\n");
for(i = 0; i<m; i++)
for(j = 0; j<n; j++)
scanf("%d", &second[i][j]);
printf("Sum of entered matrices:-\n");
for(i = 0; i<m; i++){
for(j = 0; j<n; j++){
sum[i][j]=first[i][j]+second[i][j];
printf("%d\t", sum[i][j]);
}
scanf("\n");
}
return 0;}

Lab 6(c) Write a c program to find factorial of a number.


#include<stdio.h>
int main (){
int i, f=1,num;
printf("Enter a number:");
scanf("%d",&num);
for(i=1; i<=num; i++)
f=f*i;
printf("Factorial of %d is: %d",num, f);
return 0;}

You might also like