0% found this document useful (0 votes)
11 views10 pages

C Programming Lab Assignments Overview

Uploaded by

Gaurav Bhaisare
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views10 pages

C Programming Lab Assignments Overview

Uploaded by

Gaurav Bhaisare
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

CP Lab Assignment

Name: GAURAV BHAISARE


Roll no: 23G098
Sch. No: 2311601248
Date: 12/02/2024

Q1. #include <stdio.h>


#include <math.h>
int main()
{
int principal;
int SI;
float A1;
float A2;
float A3;
float A4;
float A5;

printf("enter your principal: ");


scanf("%d" ,&principal);

SI= principal*3.5*0.1;
printf("simple interest is :%d\n", SI);
A1=(principal*(pow((1+3.5/100), 10))-principal);

printf("compound interest annually is: %f\n", A1);


A2=(principal*(pow((1+3.5/200), 20))-principal);
printf("compound interest semi annually is: %f\n", A2);
A3=(principal*(pow((1+3.5/400), 40))-principal);
printf("compound interest quarterly is: %f\n", A3);
A4=(principal*(pow((1+3.5/1200), 120))-principal);
printf("compound interest monthly is: %f\n", A4);
A5=(principal*(pow((1+3.5/36500), 3650))-principal);
printf("compound interest daily is: %f\n", A5);
return 0;
}
OUTPUT:

Q2.
#include <stdio.h>

int main(){

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

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

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

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

return 0;
}

OUTPUT:

Q3. #include<stdio.h>
int main()
{
int n,sum;
printf("Enter a 3 digit number:");
scanf("%d",&n);
sum=0;
for(;n>0;n/=10)
{
sum+=n%10;
}

printf("Sum=%d",sum);
return 0;
}

Output:

Q4.
#include<stdio.h>

int main()
{
int num;
printf("Enter a 4 digit number:");
scanf("%d",&num);

int first = num/1000;


int last= num%10;
int sum= first + last;
printf("the sum of the first and last digit is %d\n",sum);
return 0;
}

OUTPUT:
Q5.
#include<stdio.h>
int main()
{
float l,b,r;
printf("Enter Length:");
scanf("%f",&l);

printf("Enter Breadth:");
scanf("%f",&b);

printf("Enter Radius:");
scanf("%f",&r);

printf("Perimeter of Rectangle= %f \n",2*(l+b));


printf("Area of Rectangle= %f \n",(l*b));

printf("Circumference of Circle= %f \n",2*22*r/7);


printf("Area of circle= %f \n",22*r*r/7);

return 0;
} OUTPUT:

Q6.

Q7. #include<stdio.h>
int main()
{
int a,b;
printf("Enter a number a and b:");
scanf("%d %d",&a,&b);

if (a-b)
{
printf("not equal");
}
else
{
printf("equal");
}

return 0;
}

OUTPUT:
Q8. include<stdio.h>
#include<math.h>

int main() {
float a,b,r,d,x,y;
printf("enter the coordinates of center:\n");
scanf("%f",&a);
scanf("%f",&b);

printf("enter the radius of circle:\n");


scanf("%f",&r);

printf("enter the coordinates of point:\n");


scanf("%f",&x);
scanf("%f",&y);

d = sqrt(pow((x-a),2)+pow((y-b),2));

if (d==r){printf("point on the circle");}

if(d<r){printf("point lies inside the circle");}

if(d>r){printf("point lies outside the circle");}


return 0;
}
OUTPUT:

Common questions

Powered by AI

Replacing magic numbers with macro definitions (e.g., #define PI 3.14159) improves maintainability as any change in value needs only be updated in a single place, reducing errors across the codebase. It enhances portability since the definitions can be centralized and adjusted per platform requirements if needed, maintaining consistent behavior without manually altering multiple instances. Macros make code self-documenting, thereby improving readability and reducing the likelihood of bugs .

The program uses a temporary variable to store one of the numbers, allowing them to be swapped: temp = a; a = b; b = temp;. This method is straightforward but requires additional memory for the temporary variable. A more efficient method can use arithmetic operations or bitwise XOR to swap without a temporary variable: a = a + b; b = a - b; a = a - b; or using XOR: a = a ^ b; b = a ^ b; a = a ^ b;. These methods perform the swap in constant space .

The program uses '22/7' as an approximation for pi. Using M_PI from the math library provides a more accurate value of pi. Moreover, it directly implements formulas: Area = M_PI * r * r; Circumference = 2 * M_PI * r;. This increases accuracy and clarity as these formulas directly correlate to the mathematical definition, and using standard library constants ensures precision .

The program calculates simple interest using a fixed formula: SI = principal * rate * time. In this case, the simple interest formula is adjusted for a one-year period with a rate of 3.5% per annum. For compound interest, the formula A = principal * (1 + rate/n)^(n*t) is used, where 'n' is the compounding frequency per year. The program calculates compound interest for different frequencies: annually, semi-annually, quarterly, monthly, and daily. Each different 'n' results in different amounts of compound interest earned, with more frequent compounding yielding higher returns due to interest being calculated on previously earned interest more frequently .

The program extracts the first digit by performing integer division ('num/1000'), which removes the last three digits. The last digit is extracted using modulo operation ('num%10'). These values are then summed to find the result, which is the sum of the first and last digits of the four-digit number .

The program uses a while loop that iterates as long as n > 0. In each iteration, it calculates the right-most digit of 'n' using n % 10 and adds it to 'sum'. Then, it removes the right-most digit from 'n' using integer division by 10. This process continues until 'n' becomes 0, effectively summing all the digits of the original three-digit number .

Improper input handling can lead to runtime errors, such as invalid inputs causing unexpected behavior or incorrect calculations. For instance, non-numeric input in scanf may lead to infinite loops or incorrect data being assigned. Using input validation and error-checking functions, like checking the return value of scanf or using the 'fgets()' method followed by 'sscanf()', can help ensure only valid formatted input is processed .

The program checks the condition (a-b), which evaluates to true if the difference is non-zero, otherwise false. This implicitly checks if 'a' and 'b' are equal but doesn't convey the intent clearly. Moreover, for floating-point numbers, direct comparison may fail due to precision issues, highlighting the limitation since this logic isn't type-safe for non-integers. A clearer method involves using relational operators: if (a != b), which explicitly states the condition .

The program calculates the distance 'd' between the point (x, y) and the circle’s center (a, b) using the Euclidean distance formula: d = sqrt((x-a)^2 + (y-b)^2). It then compares 'd' with the circle's radius 'r': if d == r, the point is on the circle; if d < r, the point is inside; if d > r, outside. This relies on the geometric principle that a point's position relative to a circle can be determined by comparing the radius with the distance from the center to the point .

Using identifiers like 'l' (lowercase 'L') and 'I' can lead to confusion due to their visual similarity to numbers '1' and '0', especially in some fonts, reducing code readability. Clear and descriptive identifiers help prevent this issue. For example, 'length' instead of 'l' or 'index' instead of 'I' increases clarity and reduces potential errors or misreading, making the code more maintainable .

You might also like