Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

C Lab Assignment

Uploaded by

chinmaykhare87
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

C Lab Assignment

Uploaded by

chinmaykhare87
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Programming in C

Lab Assignment

Bachelor of Technology

Submitted by

Name:- Chinmay khare Roll No.590017049

Submitted to
Dr. Mahendra Kumar Shrivas

UPES
Bidholi, Via Prem Nagar, Dehradun, Uttarakhand
July-Dec – 2024
Lab Assignment 1

Question 1: Write a C program to print “Hello world”.

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;

printf("Enter age: ");


scanf("%d",&age);
printf("Enter name: ");
scanf("%d",&name);
return 0;
}

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);
}

Question 1: Write a C programe to add anumbers, take number from user

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;
}

Question 2 WAP a C program to convert tempersture from Celsius to


Fahrenheit usimg the formula: Fn = (c*9/5)+32

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)

printf("Triangle is right angled\n");}


else
{
printf("Triangle not valid\n");}}

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;

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 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;

days= (leap*366) + (nonleap*365) + 1;


//conditions if-else
if(days%7==0){
printf("1st January of %d is:Sunday",year);
}
else if(days%7==1){
printf("1st January of %d is:Monday",year);
}
else if(days%7==2){
printf("1st January of %d is:Tuesday",year);
}
else if(days%7==3){
printf("1st January of %d is:Wednesday",year);
}
else if(days%7==4){
printf("1st January of %d is:Thursday",year);
}
else if(days%7==5){
printf("1st January of %d is:Friday",year);
}
else{
printf("1st January of %d is:Saturday",year);
}
}

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

Question 2:-WAP to print the multiplication table of the number entered by


the user. It should be in the correct formatting. Num * 1 = Num
Solution:-
Coding Output

#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

Question 3: WAP to generate the following set of output

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

Question 4: The population of a town is 100000. The population has increased


steadily at the rate of 10% per year for the last 10 years. Write a program to
determine the population at the end of each year in the last decade.

Solution:
Coding Output

#include int main()


{
//initialising integer
and float variables
int year; float
pop=100000;
//for-loop for 10 years
printf(“Population at
the end of each year in
the last decade is:\n”);
for(year=1;year<=10;y
ear++) {
//10 % of population
pop = pop - pop*0.1;
//10 % of population
printf("%d year: %.2f\
n",year,pop); }
}

Lab Assignment 5

Question 5: Ramanujan Number is the smallest number that can be expressed as


the sum of two cubes in two different ways. WAP to print all such numbers up to a
reasonable limit.

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);
}}}

Experiment 4: Variable and Scope of Variable


Lab Assignment 1
Question 1: Declare a global variable outside all functions and use it inside various
functions to understand its accessibility.
Solution:
Coding Output
#include <stdio.h>

// Global variable
declaration
int globalVar = 10;

// Function to display the


global variable
void displayGlobal() {
printf("Global variable:
%d\n", globalVar);
}

// Function to modify the


global variable
void modifyGlobal() {
globalVar += 5;
printf("Global variable
modified: %d\n",
globalVar);
}

// Another function that


accesses the global variable
void anotherFunction() {
printf("Global variable in
anotherFunction: %d\n",
globalVar);
}

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

*Local Variable can only accessible within the function where


it is declared. Trying to access it outside the function results
in an error, whereas Global Variable can be accessible from
any function in the program, allowing its value to be modified
and accessed globally.
Lab Assignment 3
Question 3: Declare variables within different code blocks (enclosed by curly
braces) and test their accessibility within and outside those blocks.
Solution:
Coding
#include <stdio.h>
int main() {
// Outer block
{
int outerVar = 5; // Variable declared in the outer block
printf("Outer block: outerVar = %d\n", outerVar);
// Inner block
{
int innerVar = 10; // Variable declared in the inner block
printf("Inner block: innerVar = %d\n", innerVar);
}
// Trying to access innerVar here
printf("Outer block: innerVar = %d\n", innerVar);
}

// Trying to access outerVar here


printf("Main function: outerVar = %d\n", outerVar);

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>

void cumulativeSum(int num) {


static int sum = 0; // Static local
variable to store the cumulative
sum
sum += num;
printf("Cumulative sum is: %d\
n", sum);
}

int main() {
cumulativeSum(10);
cumulativeSum(20);
cumulativeSum(30);

return 0;
}

You might also like