0% found this document useful (0 votes)
91 views

Lab Assignment

This document contains 5 programming assignments completed by Shubhi Shrivastava for their 2nd semester B.Arch lab course in 2021. The assignments include programs to: 1) Print "Hello World" and add/sum two numbers 2) Calculate simple interest and convert Fahrenheit to Celsius 3) Swap values with and without a third variable, and check for leap years 4) Calculate roots of a quadratic equation, find largest of 3 numbers, and print prime numbers from 1 to 100 5) Calculate power, cube, square root, sum and factorial of a number, Fibonacci sequence, and determine if a matrix is symmetric
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views

Lab Assignment

This document contains 5 programming assignments completed by Shubhi Shrivastava for their 2nd semester B.Arch lab course in 2021. The assignments include programs to: 1) Print "Hello World" and add/sum two numbers 2) Calculate simple interest and convert Fahrenheit to Celsius 3) Swap values with and without a third variable, and check for leap years 4) Calculate roots of a quadratic equation, find largest of 3 numbers, and print prime numbers from 1 to 100 5) Calculate power, cube, square root, sum and factorial of a number, Fibonacci sequence, and determine if a matrix is symmetric
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Shubhi Shrivastava

B.Arch. 2nd Semester


2021UAR1195

Lab Assignment 1

1. WAP to print “Hello World”.


#include <stdio.h>
int main()
{
// prints hello world
printf("Hello World");
return 0;
}

2.WAP to add two numbers and print out the result.


#include<stdio.h>
int main()
{
int X, Y, sum = 0;
//accepting the numbers from user
printf("Enter two numbers X and Y : \n");
//storing
scanf("%d%d", &X, &Y);
sum = X + Y;
// Print
printf("Sum of X and Y is: %d", sum);
return 0;
}
3.WAP to calculate the simple interest given the principal amount, rate of interest and time period.

#include <stdio.h>
int main()
{
float P, T, R, SI;
// Input principle, rate and time and they can be decimal
printf("Enter principle (amount): ");
scanf("%f", &P);
printf("Enter time: ");
scanf("%f", &T);
printf("Enter rate: ");
scanf("%f", &R);
//Calculate simple interest
SI = (P*T*R) / 100;
//Print the resultant value of SI
printf("Simple Interest = %f", SI);
return 0;
}

4. Develop a program to convert Fahrenheit Temperature to Celsius.


#include<stdio.h>
int main(){
float f, c;
//accepting the input from user
printf("Enter Fahrenheit: \n");
scanf("%f",&f);
c = ((f - 32)*5)/9;
printf("Celsius: %f \n", c);
return 0;
}

5. WAP to :
a) swap two numbers using a third variable.
#include <stdio.h>
int main()
{
int a, b, c;
scanf("%d%d", &a, &b);
c = a;
a = b;
b = c;
printf("After SwappingnFirst variable = %d& Second variable = %d", a, b);
return 0;
}
b) swap without using a third variable.
#include <stdio.h>
int main()
{
int x = 109, y =66;
// Code to swap 'x' and 'y'
x = x + y; // x now becomes 15
y = x - y; // y becomes 10
x = x - y; // x becomes 5
printf("After Swapping: x = %d, y = %d", x, y);
return 0;
}
Lab Assignment -2
1. WAP to find whether a given year is a leap year or not.
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
// leap year if perfectly divisible by 400
if (year % 400 == 0) {
printf("%d is a leap year.", year);
}
// not a leap year if divisible by 100
// but not divisible by 400
else if (year % 100 == 0) {
printf("%d is not a leap year.", year);
}
// leap year if not divisible by 100
// but divisible by 4
else if (year % 4 == 0) {
printf("%d is a leap year.", year);
}
// all other years are not leap years
else {
printf("%d is not a leap year.", year);
}
return 0;
}

2. WAP to check whether a number is even or odd.


#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);

// true if num is perfectly divisible by 2


if(num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", num);

return 0;
}

3. WAP to calculate the roots of a quadratic equations.


#include <math.h>
// needed to use sqrt function
#include <stdio.h>
int main() {
double a, b, c, discriminant, root1, root2, realPart, imagPart;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf", &a, &b, &c);
discriminant = b * b - 4 * a * c;
// condition for real and different roots
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("root1 = %.2lf and root2 = %.2lf", root1, root2);
}
// condition for real and equal roots
else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
printf("root1 = root2 = %.2lf;", root1);
}
// if roots are not real
else {
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imagPart, realPart, imagPart);
}
return 0;
}

4. WAP to find the highest among three numbers and print the same.
#include <stdio.h>
int main() {
double n1, n2, n3;
printf("Enter three different numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
// if n1 is greater than both n2 and n3, n1 is the largest
if (n1 >= n2 && n1 >= n3)
printf("%.2f is the largest number.", n1);
// if n2 is greater than both n1 and n3, n2 is the largest
if (n2 >= n1 && n2 >= n3)
printf("%.2f is the largest number.", n2);
// if n3 is greater than both n1 and n2, n3 is the largest
if (n3 >= n1 && n3 >= n2)
printf("%.2f is the largest number.", n3);
return 0;
}
Lab Assignment -3
1. WAP to
a) evaluate the power of a number.
b) cube of a number.
c) square root of a number.

a)
#include <stdio.h>
int main() {
int a, exp;
long double result = 1.0;
printf("Enter a base number: ");
scanf("%d", &a);
printf("Enter an exponent: ");
scanf("%d", &exp);
while (exp != 0) {
result *= a;
--exp;
}
printf("Answer = %.0Lf", result);
return 0;
}

b)
#include<stdio.h>
int main()
{
int a, cube;
printf(" \n Please Enter any integer Value : ");
scanf("%d", &a);
cube = a * a * a;
printf("\n Cube of a given number %d is = %d", a, cube);
return 0;
}
c)
#include <math.h>
#include <stdio.h>
int main() {
double x, squareRoot;
printf("Enter a number: ");
scanf("%lf", &x);
// computing the square root
squareRoot = sqrt(x);
printf("Square root of %.2lf = %.2lf", x, squareRoot);
return 0;
}

2. WAP to print first ‘n’ natural numbers and their total sum.
#include <stdio.h>
int main() {
int n, i, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
for (i = 1; i <= n; ++i) {
printf("\n%d",i);
sum += i;
}
printf("Sum = %d", sum);
return 0;
}

3. WAP to print all the prime numbers from 1 to 100.


#include <stdio.h>
int main()
{
int i, a = 1, count;
while(a <= 100)
{
count = 0;
i = 2;
while(i <= a/2)
{
if(a%i == 0)
{
count++;
break;
}
i++;
}
if(count == 0 && a != 1 )
{
printf(" %d ", a);
}
a++;
}
return 0;
}

4. WAP to calculate the factorial of a number.


#include <stdio.h>
int main() {
int n, i;
unsigned long long fact = 1;
printf("Enter an integer: ");
scanf("%d", &n);
// to show error if the user enters a negative integer
if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");
else {
for (i = 1; i <= n; ++i) {
fact *= i;
}
printf("Factorial of %d = %llu", n, fact);
}
return 0;
}
5. WAP to print the Fibonacci sequence of the first ‘n’ numbers.
#include <stdio.h>
int main() {
int i, n;
// initializing first and second terms
int t1 = 0, t2 = 1;
// initializing the next term (3rd term)
int nextTerm = t1 + t2;
// get no. of terms accepted from user
printf("Enter the number of terms: ");
scanf("%d", &n);
// printing the first two terms t1 and t2
printf("Fibonacci Series: %d, %d, ", t1, t2);
// print 3rd to nth terms
for (i = 3; i <= n; ++i) {
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
return 0;
}
Lab Assignment - 4

Q.1 Write a program to find sum and average of given N elements in an array.

#include <stdio.h>
int main()
{
int i, num;
float total = 0.0, average;
printf ("Enter the value of N \n");
scanf("%d", &num);
int array[num];
printf("Enter %d numbers (-ve, +ve and zero) \n", num);
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}
printf("Input array elements \n");
for (i = 0; i < num; i++)
{
printf("%+3d\n", array[i]);
}
/* Summation starts */
for (i = 0; i < num; i++)
{
total+=array[i];/* this means total=total+array[i]; */
}
average = total / num;
printf("\n Sum of all numbers = %.2f\n", total);
printf("\n Average of all input numbers = %.2f\n", average);
}
Q.2 Write a program to print the given array in reverse order.

#include<stdio.h>
int main()
{
int a[100], i, j, Size, Temp;
printf("\nPlease Enter the size : ");
scanf("%d",&Size);
//Inserting elements in to it
for (i = 0; i < Size; i++)
{
scanf("%d", &a[i]);
}
j = i - 1; // Assigning j to Last element
i = 0; // Assigning i to first element
while (i < j)
{
Temp = a[i];
a[i] = a[j];
a[j] = Temp;
i++;
j--;
}
printf("\nResult is: ");
for (i = 0; i < Size; i++)
{
printf("%d \t", a[i]);
}
return 0;
}
Q.3 Write a program to print the elements of array by eliminating the duplicate numbers.
#include<stdio.h>
#include<stdlib.h>
int main(){
int a[50],i,j,k, count = 0, dup[50], number;
printf("Enter size of the array\n");
scanf("%d",&number);
printf("Enter Elements of the array:\n");
for(i=0;i<number;i++){
scanf("%d",&a[i]);
dup[i] = -1;
}
printf("Entered element are: \n");
for(i=0;i<number;i++){
printf("%d ",a[i]);
}
for(i=0;i<number;i++){
for(j = i+1; j < number; j++){
if(a[i] == a[j]){
for(k = j; k <number; k++){
a[k] = a[k+1];
}
j--;
number--;
}
}
}
printf("\nAfter deleting the duplicate element the Array is:\n");
for(i=0;i<number;i++){
printf("%d ",a[i]);
}
}
Q.4 Write a program to test whether a given matrix is symmetric or not.
#include<stdio.h>
int main()
{
int m, n, c, d, matrix[10][10], transpose[10][10];

printf("Enter the number of rows and columns of matrix\n");


scanf("%d%d", &m, &n);
printf("Enter elements of the matrix\n");

for (c = 0; c < m; c++)


for (d = 0; d < n; d++)
scanf("%d", &matrix[c][d]);

for (c = 0; c < m; c++)


for (d = 0; d < n; d++)
transpose[d][c] = matrix[c][d];

if (m == n) /* check if order is same */


{
for (c = 0; c < m; c++)
{
for (d = 0; d < m; d++)
{
if (matrix[c][d] != transpose[c][d])
break;
}
if (d != m)
break;
}
if (c == m)
printf("The matrix is symmetric.\n");
else
printf("The matrix isn't symmetric.\n");
}
else
printf("The matrix isn't symmetric.\n");

return 0;
}

Q.5 Write a program to find the sum of diagonal elements of a given matrix.
#include<stdio.h>
int main()
{
int i, j, rows, columns, a[10][10], Sum = 0;
printf("\n Please Enter Number of rows and columns : ");
scanf("%d %d", &i, &j);
printf("\n Please Enter the Matrix Elements \n");
for(rows = 0; rows < i; rows++)
{
for(columns = 0;columns < j;columns++)
{
scanf("%d", &a[rows][columns]);
}
}
for(rows = 0; rows < i; rows++)
{
Sum = Sum + a[rows][rows];
}
printf("\n The Sum of Diagonal Elements of a Matrix = %d", Sum );
return 0;
}

You might also like