Assignment A
Assignment A
C Programming
Lab Assignment 1
#include<stdio.h>
int main(){
int a;
return 0;
}
b. Input: 5.3267
Output: 5.3267
#include<stdio.h>
int main(){
float b;
return 0;
}
c. Input: 5.3267
Output: 5.33
#include<stdio.h>
int main(){
float c;
return 0;
}
d. Input: 5664.57567456
Output: 5664.58
#include<stdio.h>
int main(){
float d;
return 0;
}
e. Input: 35345364
Output: 35345364 * 35345364
#include<stdio.h>
int main(){
int 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];
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;
}
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
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;
}
return 0;
}
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;
return 0;
}
II)
#include <stdio.h>
int main() {
int a, b, s;
s = a + b;
a = s - a;
b = s - b;
#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);
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);