C Lab Assignment
C Lab Assignment
Lab Assignment
Bachelor of Technology
Submitted by
Submitted to
Dr. Mahendra Kumar Shrivas
UPES
Bidholi, Via Prem Nagar, Dehradun, Uttarakhand
July-Dec – 2024
Lab Assignment 1
Solution:
Coding Output
#include<stdio.h>
int main()
{
printf("Hello World");
return 0; }
Question 2: Write a C program to print the address in multiple lines (new lines)
Solution:
Coding Output
#include<stdio.h>
int main()
{
printf("Ivy league
boys hostel\nNear upes
college\nDehradun
248001");
return 0; }
Question 3: Write a program that prompt the user to enter their name and age.
Solution:
Coding Output
#include<stdio.h>
int main()
{
int age,name;
Question 4: Write a C program to add two numbers, Take numbers from user.
Solution:
Coding Output
#include<stdio.h>
int main()
{
int a,b,c,y;
printf("Enter a: ");
scanf("%d",&a);
printf("Enter b: ");
scanf("%d",&b);
printf("Enter c: ");
scanf("%d",&c);
y=a+b+c;
printf("%d",y);
}
Solution:
#include<stdio.h>
int main(){
int l , b , area , perm;
printf("Enter the
length of the rectangle:
");
scanf("%d",&l);
printf("Enter the
breadth of the
rectangle: ");
scanf("%d",&b);
area = l * b;
perm = 2 * (l + b);
printf("The area of
the given rectangle is:
%d\n",area);
printf("The perimeter
of the given rectangle
is: %d",perm);
return 0;
}
Solution Solution:s
#include<stdio.h>
int main(){
int c;
printf("Enter the
tempreature in celcius:
");
scanf("%d",&c);
float h = (c* 9/5)+32;
printf("The
tempreature in
fahrenheit is: %f",h);
return 0;
}
Experiment 3.1:- Conditional Statements
Question 1:- Write a program to take check if the triangle is valid or not. If the
validity is established, do check if the triangle is isosceles, equilateral, right angle,
or scalene. Take sides of the triangle as input from a user.
Solution:
Coding Output
#include<stdio.h>
//header file for mathematical calculations
#include<math.h>
void main(){
int side1,side2,side3;
printf("Enter the first side");
scanf("%d",&side1);
printf("Enter the second side");
scanf("%d",&side2);
printf("Enter the third side");
scanf("%d",&side3);
int a1,a2,a3;
a1=side1+side2;
a2=side2+side3;
a3=side1+side3;
int b1,b2,b3;
b1=pow(side1,2);
b2=pow(side2,2);
b3=pow(side3,2);
if(a1>side3 && a2>side1 && a3>side2)
{
printf("Triangle is valid\n");
if(side1==side2 || side2==side3 ||
side1==side3)
printf("Triangle is Isoceles\n");}
else if(side1==side2==side3)
printf("Triangle is Equilateral\n");
else if(side1!=side2!=side3)
printf("Triangle is Scalence\n");
else if(b1==b2+b3 || b2==b3+b1 ||
b3==b2+b1)
printf("Triangle is right angled\n");}
else
{
printf("Triangle not valid\n");}}
coding output
#include<stdio.h>
//header file for mathematical calculations
#include<math.h>
void main(){
int side1,side2,side3;
printf("Enter the first side");
scanf("%d",&side1);
printf("Enter the second side");
scanf("%d",&side2);
printf("Enter the third side");
scanf("%d",&side3);
int a1,a2,a3;
a1=side1+side2;
a2=side2+side3;
a3=side1+side3;
int b1,b2,b3;
b1=pow(side1,2);
b2=pow(side2,2);
b3=pow(side3,2);
if(a1>side3 && a2>side1 && a3>side2) {
printf("Triangle is valid\n");
if(side1==side2 || side2==side3 ||
side1==side3)
printf("Triangle is isoceles\n");}
else if(side1==side2==side3)
printf("Triangle is equilateral\n");
else if(side1!=side2!=side3)
printf("Triangle is scalence\n");
else if(b1==b2+b3 || b2==b3+b1 ||
b3==b2+b1)
Question 3:- WAP to check if three points (x1,y1), (x2,y2) and (x3,y3) are collinear or not.
Solution:-
coding output
#include<stdio.h>
void main()
{
int x1,y1,x2,y2,x3,y3;
float m1,m2;
m1= (y2-y1)/(x2-x1);
m2= (y3-y2)/(x3-x2);
if(m1!=m2)
{
printf("The given points are non-collinear");
}
else{
printf("The given points are collinear");
}
}
Lab Assignment 3
Question 3: WAP to check if three points (x1,y1), (x2,y2) and (x3,y3) are collinear or not.
Solution:
Coding Output
#include<stdio.h>
void main()
{
int x1,y1,x2,y2,x3,y3; //declaration
float m1,m2;
printf("Enter the coordinates of A\n");
scanf("%d %d",&x1,&y1);
printf("Enter the coordinates of B\n");
scanf("%d %d",&x2,&y2);
printf("Enter the coordinates of C\n");
scanf("%d %d",&x3,&y3);
m1= (y2-y1)/(x2-x1);
m2= (y3-y2)/(x3-x2);
if(m1!=m2)
{
printf("The given points are non-collinear");
}
else{
printf("The given points are collinear");
}
}
Lab Assignment 4
Question4:-According to the gregorian calendar, it was Monday on the date
01/01/01. If Any year is input through the keyboard write a program to find out
what is the day on 1st January of this year.
Solution:-
Coding
#include<stdio.h>
int main(){
int yearGiven=2001;
int year,diff,leap,nonleap,days;
//input from user
printf("Enter the year\n");
scanf("%d",&year);
//calculating difference
diff= year-yearGiven;
leap= diff/4;
nonleap= diff-leap;
Output
Question 5: WAP using ternary operator, the user should input the length and
breadth of a rectangle, one has to find out which rectangle has the highest
perimeter. The minimum number of rectangles should be three.
Solution:
Coding Output
#include<stdio.h>
int main()
{
//declaring float variables
float
length1,length2,length3,breadt
h1,breadth2,breadth3;
float p1,p2,p3,max;
//taking input from user
printf("Enter Length and
Breadth of Rectangle(1):\n");
scanf("%f
%f",&length1,&breadth1);
printf("Enter Length and
Breadth of Rectangle(2):\n");
scanf("%f
%f",&length2,&breadth2);
printf("Enter Length and
Breadth of Rectangle(3):\n");
scanf("%f
%f",&length3,&breadth3);
//calculating perimeters
p1=2*(length1+breadth1);
p2=2*(length2+breadth2);
p3=2*(length3+breadth3);
//calculating maximum
perimeter of three using
ternary operator
max=(p1>p2)?((p1>p3)?
p1:p3):((p2>p3)?p2:p3);
//if-else conditon to display
output
if(max==p1)
printf("Rectangle(1) has
highest perimeter");
else if(max==p2)
printf("Rectangle(2) has
highest perimeter");
else
printf("Rectangle(3) has
highest perimeter");
return 0;
}
Lab Assignment 2
#include<std
io.h>
void main()
{
int
number,resu
lt;
printf("Enter
a number:\
n");
scanf("%d",
&number);
for(int i=1;
i<=10; i++)
{
printf("%d
X %d = %d\
n",number,i,
result=numb
er*i);
}
}
Lab Assignment 3
a. 1
2 3
4 5 6
Solution:
Output
Coding
#include<stdio.h>
int main()
{
int rows=3,value=1;
//first loop to print rows
for (int counter = 1;
counter <= rows;
counter++)
{
for (int space = rows;
space > counter; space--)
{
printf(" ");
}
for (int display = 1;
display <= counter;
display++)
{
printf("%d ", value);
value++;
}
//new line
printf("\n");
}
}
b. 1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Solution:
Coding Output
#include<stdio.h>
int main()
{
//initialising number of rows
int rows=5,value;
//first loop to print rows
for(int
counter=0;counter<rows;counter++)
{
//second loop to print spaces
for(int
space=rows;space>counter;space--)
{
printf(" ");
}
//third loop to calculate and print
value
for(int
display=0;display<=counter;display+
+)
{
if(counter==0 || display==0)
value=1;
else
value=(value*(counter-display+1))/dis
play;
printf("%d ",value);
}
//new line
printf("\n");
}
}
Lab Assignment 4
Solution:
Coding Output
Lab Assignment 5
Solution:
Output
Coding
#include<stdio.h>
int main()
{
int i,num,x,y,count;
num=10000;
//for loop for range (1-30000)
for(i=1;i<=num;i++)
{
count=0;
for(x=1;x*x*x<=i;x++)
{
//for-loop for finding Ramanujan
number
for(y=x; x*x*x+ y*y*y<=i;y++)
{
//list all the numbers
if(x*x*x+y*y*y==i)
{
count++;
}
}
}
if(count==2)
{
printf("%d\n",i);
}}}
// Global variable
declaration
int globalVar = 10;
int main() {
printf("Initial global
variable in main: %d\n",
globalVar);
displayGlobal(); //
Call to display the global
variable
modifyGlobal(); //
Call to modify the global
variable
anotherFunction(); //
Call to access the modified
global variable
return 0;
}
Lab Assignment 2
Question 2: Declare a local variable inside a function and try to access it outside
the function. Compare this with accessing the global variable from within the
function.
Solution:
Coding
#include<stdio.h>
void displayLocal() {
int localVar = 10;
printf("Local Variable: %d\n", localVar);
}
void modifyLocal() {
localVar +=5;
printf("Local Variable modified: %d\n",localVar);
}
int main() {
printf("Accessing Local Variable\n");
displayLocal();
modifyLocal();
return 0;
}
Output
return 0;
}
Output
Lab Assignment 4
Question 4: Declare a static local variable inside a function. Observe how its value
persists across function calls.
Solution:
Coding Output
#include <stdio.h>
int main() {
cumulativeSum(10);
cumulativeSum(20);
cumulativeSum(30);
return 0;
}