Assignment 4
Problem 1: WAP in C that uses sizeof operator to compute and display the memory size (in bytes) occupied by the following data types – int, short int, unsigned int, long int, long long int, unsigned long int, unsigned long long int, char, signed char, unsigned char, float, double, long double. Use appropriate format specifiers for data types.
sol.
#include <stdio.h>
int main()
{
printf("Size of int: %zu bytes\n", sizeof(int));
printf("Size of short int: %zu bytes\n", sizeof(short int));
printf("Size of unsigned int: %zu bytes\n", sizeof(unsigned int));
printf("Size of long int: %zu bytes\n", sizeof(long int));
printf("Size of long long int: %zu bytes\n", sizeof(long long int));
printf("Size of unsigned long int: %zu bytes\n", sizeof(unsigned long int));
printf("Size of unsigned long long int: %zu bytes\n",sizeof(unsigned long long int));
printf("Size of char: %zu bytes\n", sizeof(char));
printf("Size of signed char: %zu bytes\n", sizeof(signed char));
printf("Size of unsigned char: %zu bytes\n", sizeof(unsigned char));
printf("Size of float: %zu bytes\n", sizeof(float));
printf("Size of double: %zu bytes\n", sizeof(double));
printf("Size of long double: %zu bytes\n", sizeof(long double));
return 0;
}
Problem 3: WAP in C to swap the contents of two variables without using a third variable.
sol.
#include<stdio.h>
main() {
int a,b;
printf("Enter the values of a and b");
scanf("%d%d", &a,&b);
a+=b;
b=a-b;
a=a-b;
printf("a is %d and b is %d",a,b);
}
Problem 4: WAP in C to input number of days from user and convert it to years, weeks and days.
sol.
#include <stdio.h>
int main()
{
int days, years, months, weeks, remaining_days;
printf("Enter the number of days: ");
scanf("%d", &days);
years = days / 365; // Calculate years
months = (days % 365) / 30; // Calculate months from remaining days after considering years
weeks = ((days % 365) % 30) / 7; // Calculate weeks from remaining days after considering years and months
remaining_days = ((days % 365) % 30) % 7; // Calculate remaining days
printf("Equivalent: %d days = %d years, %d months, %d weeks, and %d days \n", days, years, months, weeks, remaining_days);
return 0;
}
Problem 5: WAP in C to find power of a given number, i.e. x to the power y.
sol.
#include<stdio.h>
#include<math.h>
int main() {
int x,y,z;
printf("x =");
scanf("%d",&x);
printf("y =");
scanf("%d",&y);
z=pow(x,y);
printf("x to the power y is equals to %d",z);
}
Problem 6: WAP in C to find the square root of a given number.
sol.
#include<stdio.h>
#include<math.h>
int main() {
int x,y;
printf("x =");
scanf("%d",&x);
y=sqrt(x);
printf("Square root of x is %d",y);
return 0;
}
Problem 7: WAP in C to input two numbers and find maximum and minimum between two numbers using conditional/ternary operator ? :
sol.
#include <stdio.h>
int main()
{
int n1, n2, max, min;
printf("Enter first number: ");
scanf("%d", &n1);
printf("Enter second number: ");
scanf("%d", &n2);
max = (n1 > n2) ? n1 : n2;
min = (n1 < n2) ? n1 : n2;
printf("Maximum number: %d\n", max);
printf("Minimum number: %d\n", min);
return 0;
}
Problem 8: WAP in C to input a character and check whether the character is alphabet or not using conditional/ternary operator ? :
sol.
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
scanf(" %c", &ch);
//condition based on ascii values.
( (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ) ? printf("%c is an alphabet.\n", ch) : printf("%c is not an alphabet.\n", ch);
return 0;
}
Problem 10: WAP in C to check whether an alphabet is vowel or consonant
sol.
#include <stdio.h>
int main()
{
char alphabet;
printf("Enter any one alphabet from a to z (small letters preferred): ");
scanf(" %c", &alphabet);
if (alphabet=='a'||alphabet== 'e'||alphabet=='i'|| alphabet=='o'||alphabet=='u')
{
printf("The given alphabet is a vowel.\n");
}
else if (alphabet >'a' && alphabet <'z') //condition based on ascii values of characters
{
printf("The given alphabet is a consonant.\n");
}
else {
printf("Invalid character entered.\n");
}
return 0;
}
Problem 11: WAP in C to input a character from user and check whether given character is alphabet, digit or special character.
sol.
#include <stdio.h>
int main()
{
char ch;
printf("Enter a character: ");
scanf(" %c", &ch);
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) //condition based on ascii values
{
printf("The entered character is an alphabet.\n");
}
else if (ch >= '0' && ch <= '9')
{
printf("The entered character is a digit.\n");
}
else
{
printf("The entered character is a special character.\n");
}
return 0;
}
Problem 13: WAP that takes two operands and one operator from the user, perform the operation, and prints the result by using switch/case statement.
sol.
#include <stdio.h>
int main()
{
float n1, n2, result;
char operator;
printf("Enter first number: ");
scanf("%f", &n1);
printf("Enter operator (+, -, *, /): ");
scanf(" %c", &operator); /*Note the space before %c to consume newline or other whitespace characters */
printf("Enter second number: ");
scanf("%f", &n2);
switch (operator)
{
case '+':
result = n1 + n2;
printf("%.2f + %.2f = %.2f\n", n1, n2, result);
break;
case '-':
result = n1 - n2;
printf("%.2f - %.2f = %.2f\n", n1, n2, result);
break;
case '*':
result = n1 * n2;
printf("%.2f * %.2f = %.2f\n", n1, n2, result);
break;
case '/':
if (n2 == 0)
{
printf("Error! Division by zero is not allowed.\n");
} else
{
result = n1 / n2;
printf("%.2f / %.2f = %.2f\n", n1, n2, result);
}
break;
default:
printf("Invalid operator entered.\n");
}
return 0;
}
Problem 14: WAP in C to find all roots of a Quadratic equation using switch/case.
sol.
#include <stdio.h>
#include <math.h>
int main( ) {
float a, b, c;
float discriminant, root1, root2, realPart, imaginaryPart;
printf("Enter coefficients a, b, and c: ");
scanf("%f %f %f", &a, &b, &c);
discriminant = b * b - 4 * a * c;
switch (discriminant > 0) {
case 1:
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("Roots are real and different.\n");
printf("Root 1 = %.2f and Root 2 = %.2f\n", root1, root2);
break;
case 0:
switch (discriminant == 0) {
case 1:
root1 = root2 = -b / (2 * a);
printf("Roots are real and same.\n");
printf("Root 1 = Root 2 = %.2f\n", root1);
break;
case 0:
realPart = -b / (2 * a);
imaginaryPart = sqrt(-discriminant) / (2 * a);
printf("Roots are complex and different.\n");
printf("Root 1 = %.2f+%.2fi and Root 2 = %.2f-%.2fi\n", realPart, imaginaryPart, realPart, imaginaryPart);
break;
}
break;
}
return 0;
}
Problem 15: WAP in C to input month number and print total number of days in month using switch/case.
sol.
#include <stdio.h>
int main()
{
int month, days;
printf("Enter the month number (1-12): ");
scanf("%d", &month);
switch (month)
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
days = 31;
break;
case 4: case 6: case 9: case 11:
days = 30;
break;
case 2:
days = 28; // Considering a non-leap year for simplicity
break;
default:
printf("Invalid month number!\n");
return 1; // Exiting the program if an invalid month number is entered
}
printf("The total number of days in month %d is: %d\n", month, days);
return 0;
}
0 comments:
Post a Comment