Day 2 of Programming in C
Write a program to swap two values
#include <stdio.h>
void main() {
int a, b;
printf("Enter the value of a : ");
scanf("%d", &a);
printf("Enter the value of b : ");
scanf("%d", &b);
a = a + b;
b = a - b;
a = a - b;
printf("After Swapping the values\na = %d\nb = %d", a, b);
}
Step 1: Declare two integer variables `a, b`.
Step 2: Take user input for both values.
Step 3: Swap values using arithmetic operations without a third variable:
• a = a + b (store sum in a)
• b = a - b (get original a value in b)
• a = a - b (get original b value in a)
Step 4: Print the swapped values.
Step 2: Take user input for both values.
Step 3: Swap values using arithmetic operations without a third variable:
• a = a + b (store sum in a)
• b = a - b (get original a value in b)
• a = a - b (get original b value in a)
Step 4: Print the swapped values.
Write a program to take a 5 digit number input and print its reverse
#include <stdio.h>
void main() {
int n, rev = 0;
printf("Enter a 5 digit number : ");
scanf("%d", &n);
rev = (rev * 10) + (n % 10);
n = n / 10;
rev = (rev * 10) + (n % 10);
n = n / 10;
rev = (rev * 10) + (n % 10);
n = n / 10;
rev = (rev * 10) + (n % 10);
n = n / 10;
rev = (rev * 10) + (n % 10);
n = n / 10;
printf("The reversed number is %d", rev);
}
Step 1: Declare variables `n` for input and `rev` for storing reversed number.
Step 2: Take user input for the 5-digit number.
Step 3: Extract each digit using modulo (%) and division (/) operations.
Step 4: Build reversed number by multiplying by 10 and adding each digit.
Step 5: Print the reversed number.
Step 2: Take user input for the 5-digit number.
Step 3: Extract each digit using modulo (%) and division (/) operations.
Step 4: Build reversed number by multiplying by 10 and adding each digit.
Step 5: Print the reversed number.
Write a program to input the 3 sides of triangle and print its area
#include <stdio.h>
#include <math.h>
void main()
{
float a, b, c, s;
printf("Enter the first side of the triangle : ");
scanf("%f", &a);
printf("Enter the second side of the triangle : ");
scanf("%f", &b);
printf("Enter the third side of the triangle : ");
scanf("%f", &c);
s = (a + b + c) / 2;
printf("Area of the triangle is %f", sqrt(s * (s - a) * (s - b) * (s - c)));
}
Step 1: Declare float variables `a, b, c` for sides and `s` for semi-perimeter.
Step 2: Take user input for all three sides.
Step 3: Calculate semi-perimeter using formula `s = (a + b + c) / 2`.
Step 4: Calculate and print area using Heron's formula: `√(s(s-a)(s-b)(s-c))`.
Step 2: Take user input for all three sides.
Step 3: Calculate semi-perimeter using formula `s = (a + b + c) / 2`.
Step 4: Calculate and print area using Heron's formula: `√(s(s-a)(s-b)(s-c))`.
Write a program to take input cartesian coordinate and print its corresponding polar coordinate
#include <stdio.h>
#include <math.h>
void main()
{
int x, y;
printf("Enter the x-coordinate : ");
scanf("%d", &x);
printf("Enter the y-coordinate : ");
scanf("%d", &y);
printf("The polar form of (%d,%d) is (%f,%f)", x, y, sqrt((x * x) + (y * y)), atan(y / x));
}
Step 1: Declare integer variables `x, y` for cartesian coordinates.
Step 2: Take user input for x and y coordinates.
Step 3: Calculate radius `r = √(x² + y²)`.
Step 4: Calculate angle `θ = arctan(y/x)` and print both values.
Step 2: Take user input for x and y coordinates.
Step 3: Calculate radius `r = √(x² + y²)`.
Step 4: Calculate angle `θ = arctan(y/x)` and print both values.
Write a program to take a character input and print its ASCII value
#include <stdio.h>
void main()
{
char c;
printf("Enter a character : ");
c = getchar();
printf("The ASCII Value for %c is %d", c, c);
}
Step 1: Declare a character variable `c`.
Step 2: Take character input using `getchar()`.
Step 3: Print both the character and its ASCII value using `%c` and `%d` format specifiers.
Step 2: Take character input using `getchar()`.
Step 3: Print both the character and its ASCII value using `%c` and `%d` format specifiers.
Write a program to take a 5 digit number input and print the sum of its first and last digit
#include <stdio.h>
void main()
{
int n;
printf("Enter a 5 digit number : ");
scanf("%d", &n);
printf("The first digit of the number is %d\nThe last digit of the number is %d", n / 10000, n % 10);
}
Step 1: Declare an integer variable `n`.
Step 2: Take user input for 5-digit number.
Step 3: Get first digit using `n/10000` and last digit using `n%10`.
Step 4: Print both digits.
Step 2: Take user input for 5-digit number.
Step 3: Get first digit using `n/10000` and last digit using `n%10`.
Step 4: Print both digits.
Write a program to take 10 floating inputs and print their mean, variance and standard deviation
#include <stdio.h>
#include <math.h>
void main()
{
float v1 = 1.1, v2 = 2.2, v3 = 3.3, v4 = 4.4, v5 = 5.5, v6 = 6.6, v7 = 7.7, v8 = 8.8, v9 = 9.9, v10 = 10.10;
float mean, var, std_dev;
mean = (v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9 + v10) / 10.0;
var = ((v1 - mean) * (v1 - mean) +
(v2 - mean) * (v2 - mean) +
(v3 - mean) * (v3 - mean) +
(v4 - mean) * (v4 - mean) +
(v5 - mean) * (v5 - mean) +
(v6 - mean) * (v6 - mean) +
(v7 - mean) * (v7 - mean) +
(v8 - mean) * (v8 - mean) +
(v9 - mean) * (v9 - mean) +
(v10 - mean) * (v10 - mean)) /
10;
std_dev = sqrt(var);
printf("Mean = %f\nVariance = %f\nStandard Deviation = %f", mean, var, std_dev);
}
Step 1: Declare variables for 10 values and result storage.
Step 2: Calculate mean by summing all values and dividing by 10.
Step 3: Calculate variance using the formula: Σ(x-μ)²/n.
Step 4: Calculate standard deviation as square root of variance.
Step 5: Print all three statistical measures.
Step 2: Calculate mean by summing all values and dividing by 10.
Step 3: Calculate variance using the formula: Σ(x-μ)²/n.
Step 4: Calculate standard deviation as square root of variance.
Step 5: Print all three statistical measures.