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

Assignment A

The document contains lab assignments for a CS 101 course focused on C programming. It includes user-interactive programs for reading inputs, calculating geometric figures' perimeters and areas, checking even or odd numbers, and performing basic arithmetic operations. Additionally, it covers concepts like loops, ternary operators, and swapping variables.

Uploaded by

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

Assignment A

The document contains lab assignments for a CS 101 course focused on C programming. It includes user-interactive programs for reading inputs, calculating geometric figures' perimeters and areas, checking even or odd numbers, and performing basic arithmetic operations. Additionally, it covers concepts like loops, ternary operators, and swapping variables.

Uploaded by

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

CS 101 – Fundamentals of Computer Programming

C Programming
Lab Assignment 1

Write user-interactive C programs to perform the following:


1. Write a program to read the input and print the output as mentioned below.
a. Input: 25
Output: 25

#include<stdio.h>
int main(){

int a;

printf("Enter your value :\n");


scanf("%d",&a);
printf("User entered value : %d \n",a);

return 0;
}

b. Input: 5.3267
Output: 5.3267

#include<stdio.h>
int main(){

float b;

printf("Enter float value :\n");


scanf("%f",&b);
printf("User entered value : %.4f \n",b);

return 0;
}

c. Input: 5.3267
Output: 5.33

#include<stdio.h>
int main(){
float c;

printf("Enter float value :\n");


scanf("%f",&c);
printf("User entered value : %.2f \n",c);

return 0;
}

d. Input: 5664.57567456
Output: 5664.58

#include<stdio.h>
int main(){

float d;

printf("Enter float value :\n");


scanf("%f",&d);
printf("User entered value : %.2f \n",d);

return 0;
}

e. Input: 35345364
Output: 35345364 * 35345364

#include<stdio.h>
int main(){

int e;

printf("Enter your value :\n");


scanf("%d",&e);
printf("User entered value : %d*%d \n",e,e);

return 0;
}
2. Print your name, roll number, branch, college, and home address (Read Data during
execution of the program).
Ans:
#include<stdio.h>
int main(){

char n[10],r[10],b[10],c[10],h[50];

printf("Enter your name :\n");


scanf("%s",n);
printf("User name : %s \n",n);

printf("Enter your roll number :\n");


scanf("%s",r);
printf("User roll number : %s \n",r);

printf("Enter your branch :\n");


scanf("%s",b);
printf("User branch : %s \n",b);

printf("Enter your college name :\n");


scanf("%s",c);
printf("User college : %s \n",c);

printf("Enter your address :\n");


scanf("%s",h);
printf("User address : %s \n",h);

return 0;
}
3. Evaluate the following expressions for variables with suitable datatypes:
Note: Reading values to variables during the execution of the program.
Ans:
a)
#include<stdio.h>
#include<math.h>
int main(){

int d,t;
float X,x,y,z,a,b,c;
a=7;
b=2;
c=3;
d=5;
x=5;
y=6;
z=2;

t=d%2;
X=pow(a+pow(b,3)+pow(c/x,4)*pow(y/z,2)*t,2);

printf("%f\n",X);

return 0;
}

Output: 261.352509
b)
#include<stdio.h>
#include<math.h>
int main(){

int Y,a,b,c,d,t;
a=1;
b=2;
c=5;
d=3;
t=c%d;
Y=pow(a-b*t,2);

printf("%d\n",Y);

return 0;
}

Output: 9

c)
#include<stdio.h>
#include<math.h>
int main(){

float Z1,Z2,a,b,c;
a=1;
b=3;
c=2;

Z1=(-b+pow(pow(b,2)-4*a*c,0.5))/2*a;
Z2=(-b-pow(pow(b,2)-4*a*c,0.5))/2*a;

printf("Value of Z1 is %f\n",Z1);
printf("Value of Z2 is %f\n",Z2);

return 0;
}

Output: Value of Z1 is -1.000000


Value of Z2 is -2.000000
d)
#include<stdio.h>
#include<math.h>
int main()
{

int a,b,c,P;
a=2;
b=1;
c=1;

P=pow(pow(a+b+c,2)*pow(a-b,2),0.33);

printf("P=pow(pow(a+b+c,2)*pow(a-b,2),1/3)\n");
printf("Value of P = %d",P);

return 0;
}

Output:
P=pow(pow(a+b+c,2)*pow(a-b,2),0.33)
Value of P = 2

CS 101 – Fundamentals of Computer Programming


C Programming
Lab Assignment 2
Concepts Covered:
Reading Values from the keyboard
Convert Mathematical Expressions to C Expressions
Write user-interactive C programs to perform the following:
1. Compute the perimeter of geometric figures by seeking the specific
parameters from the user: Square, Circle, Rectangle, Triangle.
Ans:
#include <stdio.h>
#include <math.h>

int main() {
int choice;
float side, radius, length, width, a, b, c, perimeter;
printf("Choose a geometric figure to calculate its perimeter:\n");
printf("1. Square\n");
printf("2. Circle\n");
printf("3. Rectangle\n");
printf("4. Triangle\n");
printf("Enter your choice : ");
scanf("%d", &choice);

switch (choice) {
case 1:printf("Enter the length of the square: ");
scanf("%f", &side);
perimeter = 4 * side;
printf("The perimeter of the square is: %.2f\n", perimeter);
break;

case 2:
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
perimeter = 2 * 3.14 * radius;
printf("The perimeter of the circle is: %.2f\n", perimeter);
break;

case 3:
printf("Enter the length of the rectangle: ");
scanf("%f", &length);
printf("Enter the width of the rectangle: ");
scanf("%f", &width);
perimeter = 2 * (length + width);
printf("The perimeter of the rectangle is: %.2f\n", perimeter);
break;

case 4:
printf("Enter the lengths of the three sides of the triangle:\n");
printf("Side a: ");
scanf("%f", &a);
printf("Side b: ");
scanf("%f", &b);
printf("Side c: ");
scanf("%f", &c);
perimeter = a + b + c;
printf("The perimeter of the triangle is: %.2f\n", perimeter);
break;

default:
printf("Invalid choice.\n");
}
return 0;
}

2. Compute the area of geometric figures by seeking the specific parameters from
the user: Square, Circle, Rectangle, Triangle.
Insightful Question
Ans:
#include <stdio.h>
#include <math.h>

int main() {
int choice;
float side, radius, length, width, a, b, c, s, area;
printf("Choose a geometric figure to calculate its area:\n");
printf("1. Square\n");
printf("2. Circle\n");
printf("3. Rectangle\n");
printf("4. Triangle\n");
printf("Enter your choice : ");
scanf("%d", &choice);

switch (choice) {
case 1:printf("Enter the length of the square: ");
scanf("%f", &side);
area = side * side;
printf("The area of the square is: %.2f\n", area);
break;

case 2:
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
area = 3.14 * radius * radius;
printf("The area of the circle is: %.2f\n", area);
break;
case 3:
printf("Enter the length of the rectangle: ");
scanf("%f", &length);
printf("Enter the width of the rectangle: ");
scanf("%f", &width);
area = length * width;
printf("The area of the rectangle is: %.2f\n", area);
break;

case 4:
printf("Enter the lengths of the three sides of the triangle:\n");
printf("Side a: ");
scanf("%f", &a);
printf("Side b: ");
scanf("%f", &b);
printf("Side c: ");
scanf("%f", &c);
s=(a+b+c)/2;
area = pow(s * (s-a) * (s-b) * (s-c),0.5);
printf("The area of the triangle is: %.2f\n", area);
break;

default:
printf("Invalid choice.\n");
}
return 0;
}

3. Program to check whether a number is even or odd using ternary operator.


Ans:
#include <stdio.h>
int main() {
int n;

printf("Enter a number \n");


scanf("%d",&n);

(n%2==0)?printf("%d is Even\n",n):printf("%d is Odd\n",n);

return 0;
}

4. Program to perform the addition of two numbers without using + operator.


Ans:
#include <stdio.h>

int main() {

int a, b;
printf("Enter the value of first number :");
scanf("%d",&a);
printf("Enter the value of second number :");
scanf("%d",&b);
int sum= a-~b-1;
printf("Sum is %d",sum);
return 0;
}

5. Program to swap the values of two variables (i) Using another temporary
variable (ii) Without using a temporary variable.
Ans:
I)
#include <stdio.h>

int main() {
int a, b, temp;
printf("Enter value of a: ");
scanf("%d", &a);
printf("Enter value of b: ");
scanf("%d", &b);

temp = a;
a = b;
b = temp;

printf("a = %d, b = %d\n", a, b);

return 0;
}

II)
#include <stdio.h>

int main() {
int a, b, s;

printf("Enter value of a: ");


scanf("%d", &a);
printf("Enter value of b: ");
scanf("%d", &b);

s = a + b;
a = s - a;
b = s - b;

printf("a = %d, b = %d\n", a, b);


return 0;
}
Lab Assignment 3.
Concepts Covered: use of do- while loop and star pattern

1. Write a menu-driven program using switch-case in C : i. Addition, ii. Subtraction


iii.Multiplication iv. Division v. odd or even.
Ans:

#include<stdio.h>

int main()
{
int choice,a,b;
printf("Enter number for a=");
scanf("%d",&a);
printf("Enter number for b=");
scanf("%d",&b);
printf("1=Addition 2=Subtraction 3=multiplication 4=division 5=oddeven");
scanf("%d",&choice);

switch(choice)
{
case 1:printf("Addition is %d\n",a+b);
break;
case 2:printf("Subtraction is %d\n",a-b);
break;
case 3:printf("Multiplication is %d\n",a*b);
break;
case 4:printf("Division is %d\n",a/b);
break;
case 5:(a%2==0)?printf("a is even\n"):printf("a is odd\n");
break;
defalt :printf("Enter correct choice\n");
return 0;
}
}
2. WAP in C to print the following pattern:

Ans:

1)
#include<stdio.h>

int main()
{

int n,i,j;
printf("Enter no of rows \n");
scanf("%d",&n);

for(i=1;i<=n;i++)
{
for(j=n;j>=i;j--)
{
printf("*");
}
printf("\n");
}
}
2)
#include<stdio.h>

int main()
{

int n,i,j;
printf("Enter no of rows \n");
scanf("%d",&n);

for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
}
}
3)
#include<stdio.h>

int main(){

int i,j,k,n;
printf("Enter no of rows\n");
scanf("%d",&n);

for(i=1;i<=n;i++)
{
for(j=1;j<i;j++)
{
printf(" ");
}
for(k=n;k>=i;k--)
{
printf("*");
}
printf("\n");
}
return 0;
}
4)
#include<stdio.h>

int main(){

int i,j,k,n;
printf("Enter no of rows\n");
scanf("%d",&n);

for(i=1;i<=n;i++)
{
for(j=n;j>i;j--)
{
printf(" ");
}
for(k=1;k<=i;k++)
{
printf("* ");
}
printf("\n");
}
return 0;
}
3. Write a C program that calculates the sum of even and odd numbers from 1 to 50 using
do-while loops.
Ans:
#include<stdio.h>

int main(){
int i=1,oddsum=0,evensum=0;

do{
if(i%2==0)
{
evensum=evensum+i;
}
else
{
oddsum=oddsum+i;
}
i++;
}while(i<50);

printf("Sum of all Even integer from 1 to 50 is %d\n",evensum);


printf("Sum of all Odd integer from 1 to 50 is %d\n",oddsum);

return 0;
}
4. Write a C program that prompts the user to enter a positive integer and then calculates
and
prints the sum of the squares of each digit in that number using a do-while loop.
Ans:
#include<stdio.h>

int main()
{
int i,n,sum=0;
while (1) {
printf("Please enter a positive integer: ");
scanf("%d", &n);

if (n > 0) {
printf("You entered: %d\n", n);
break;
}
else
{
printf("Invalid input.\n");
}
}

do{
i=n%10;
i=i*i;
sum=sum+i;
n=n/10;
}while(n>0);

printf("Sum of square of each digit in number is %d",sum);


return 0;
}

You might also like