[go: up one dir, main page]

0% found this document useful (0 votes)
5 views5 pages

Tut2 Sol

The document contains a series of exercises for writing C programs, each with specific tasks such as printing messages, variable declaration, reversing digits, converting speeds, calculating triangle properties, computing a mathematical function, generating random numbers, and swapping integers. Each exercise includes a brief description followed by the corresponding C code solution. The exercises range in complexity and involve basic programming concepts and functions.

Uploaded by

Munesh Meena
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)
5 views5 pages

Tut2 Sol

The document contains a series of exercises for writing C programs, each with specific tasks such as printing messages, variable declaration, reversing digits, converting speeds, calculating triangle properties, computing a mathematical function, generating random numbers, and swapping integers. Each exercise includes a brief description followed by the corresponding C code solution. The exercises range in complexity and involve basic programming concepts and functions.

Uploaded by

Munesh Meena
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
You are on page 1/ 5

Exercise 1(*): Write a C program to print the following lines as shown below (on 2 lines):

Welcome!
I have written my first C exercise.

ANS-

#include <stdio.h>
int main() {
printf("Welcome!\n");
printf("I have written my first C exercise.\n");

return 0;
}

Exercise 2(*): Write a C program to declare one integer, one float and one character variables then
initialise them to 99, 12.34, and any letter of your choosing.
It then prints these values on the screen.
ANS-
#include <stdio.h>
int main() {
int number = 99;
float decimal = 12.34;
char letter = 'A';
printf("Integer value: %d\n", number);
printf("Float value: %.2f\n", decimal);
printf("Character value: %c\n", letter);

return 0;
}

Exercise 3(***): Write a program that asks the user to enter a two digit number andreturns as output the
number with its digits reversed. Think about how to use the right operators to do this. Display the
output as shown below.
+++++++++++++++++

Please enter number: 49

Number reversed is: 94

*******************

ANS -

#include <stdio.h>
int main() {
int num, tens, ones, reversed;

printf("+++++++++++++++++\n");

printf("Please enter number: ");


scanf("%d", &num);

if (num < 10 || num > 99) {


printf("Invalid input! Please enter a two-digit number.\n");
return 1; // Exit with error code
}

tens = num / 10;


ones = num % 10;
reversed = (ones * 10) + tens;

printf("Number reversed is: %d\n", reversed);

printf("*******************\n");

return 0;
}

Exercise 4(*): Write a program that asks the user to enter a number in miles per hour and display it as
kilometres per hour.
(use or improve your programme design from last week)
ANS-
#include <stdio.h>
int main() {
float mph, kmh;

printf("Enter speed in miles per hour (mph): ");


scanf("%f", &mph);

kmh = mph * 1.60934;

printf("The speed in kilometers per hour (km/h) is: %.2f\n", kmh);

return 0;
}

Exercise 5(*): Write a program that computes the perimeter and area of a right angled triangle. Program
prompts the user to enter the height and length (not the hypotenuse).

ANS-

#include <stdio.h>

#include <math.h>

int main() {

double height, base, hypotenuse, perimeter, area;


printf("Enter the height of the right-angled triangle: ");

scanf("%lf", &height);

printf("Enter the base of the right-angled triangle: ");

scanf("%lf", &base);

hypotenuse = sqrt((height * height) + (base * base));

perimeter = height + base + hypotenuse;

area = 0.5 * height * base;

printf("\nResults:\n");

printf("Hypotenuse: %.2f\n", hypotenuse);

printf("Perimeter: %.2f\n", perimeter);

printf("Area: %.2f\n", area);

return 0;

Exercise 6(***): Consider the equation f(x) = 3*sin(pi*x) + 2*cos(pi*x)


Write a program that computes and displays the values of f(x) in a table (see below)
The user should choose the, the start value, and the interval)
(Hint: use math.h library and be aware if you are using degrees or radians)
User’s input in table below as an example: start value: 0.25, interval: 0.5
/*************************************************/

Value of xValue of f(x)

0.253.5355

0.75 calculated value

1.25 calculated value

1.75 calculated value

/**************************************************/

ANS-
#include <stdio.h>
#include <math.h> // Include math library for sin() and cos()

#define PI 3.14159265358979323846 // Define π

int main() {
double x, interval, fx;
int i, num_values = 5; // Number of values to compute (modify if
needed)

// Ask the user for input values


printf("Enter the start value of x: ");
scanf("%lf", &x);

printf("Enter the interval: ");


scanf("%lf", &interval);

// Print table header


printf("\n*****************************************\n");
printf(" Value of x Value of f(x) \n");
printf("*****************************************\n");

// Compute f(x) for multiple values


for (i = 0; i < num_values; i++) {
// Calculate function value
fx = 3 * sin(PI * x) + 2 * cos(PI * x);

// Print result
printf(" %.2f %.4f\n", x, fx);

// Update x for the next iteration


x += interval;
}

printf("*****************************************\n");

return 0; // Indicate successful execution


}

Exercise 7(*): Write a program that prints a random number between 0 and X inclusive.The program
must prompt the user to enter the value of X.
Hint: use the rand() function.
ANS-

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
int x, random_number;

srand(time(NULL));

printf("Enter the value of X: ");


scanf("%d", &x);

if (x < 0) {
printf("Error: Please enter a non-negative number.\n");
return 1;
}

random_number = rand() % (x + 1);

printf("Random number between 0 and %d: %d\n", x, random_number);

return 0;
}

Exercise 8(***): Write a program that takes as input two integer numbers and perform a swap
operation and displays the swapped numbers. Hint, use a third variable.

/***************************************************************************/ Input: a=1; b=2

Output: a=2; b=1

/******************************

ANS-

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

printf("Enter second number (b): ");


scanf("%d", &b);
printf("\nBefore swapping:\n");
printf("a = %d, b = %d\n", a, b);

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

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

return 0;
}

You might also like