Programming
Programming
Programming
A)Getting Started:
1)Temperature of a city in Fahrenheit degrees is input through the keyboard. Write a program to
convert this temperature into Centigrade degrees.
Code:
#include <stdio.h>
int main() {
float fahrenheit, celsius;
printf("Enter temperature in Fahrenheit: ");
scanf("%f", &fahrenheit);
celsius = (fahrenheit - 32) * 5 / 9;
printf("Temperature in Celsius: %.2f\n", celsius);
return 0;
}
2)The length and breadth of a rectangle and radius of a circle are input through the keyboard. Write a
program to calculate the area and perimeter of the rectangle, and the area and circumference of the
circle.
Code:
#include <stdio.h>
#define PI 3.14159
int main() {
float length, breadth, radius;
float area_rectangle, perimeter_rectangle, area_circle, circumference_circle;
3)Paper of size A0 has dimensions 1189 mm x 841 mm. Each subsequent size A(n) is defined as A(n-1)
cut in half, parallel to its shorter sides. Thus, paper of size A1 would have dimensions 841 mm x 594
mm. Write a program to calculate and print paper sizes
A0, A1, A2, … A8.
Code:
#include <stdio.h>
int main() {
int width = 1189, height = 841;
int temp;
return 0;
}
B)C instruction:
Code:#include <stdio.h>
int main() {
int num, sum = 0, digit;
return 0;
}
Code:
#include <stdio.h>
int main() {
int n, fact = 1;
printf("Enter a number: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
fact *= i;
}
printf("Factorial of %d is %d\n", n, fact);
return 0;
}
Code:
#include <stdio.h>
#include <math.h>
int main() {
double lat1, lon1, lat2, lon2, radians1, radians2, distance;
return 0;
}
Code:
#include <stdio.h>
#include <math.h>
int main() {
double temperature, wind_speed, wind_chill;
return 0;
}
Code:
#include <stdio.h>
#include <math.h>
#define PI 3.14159265358979323846
int main() {
double angle, radians;
return 0;
}
Code:
#include <stdio.h>
int main() {
int c, d, temp;
printf("After swapping:\n");
printf("C = %d\n", c);
printf("D = %d\n", d);
return 0;
}
(a) A five-digit number is entered through the keyboard. Write a program to obtain the reversed
number and to determine whether the original and reversed numbers are equal or not.
Code:
#include <stdio.h>
int main() {
int num, reversed_num = 0, remainder;
while (num != 0) {
remainder = num % 10;
reversed_num = reversed_num * 10 + remainder;
num /= 10;
}
if (reversed_num == num) {
printf("The original and reversed numbers are equal.\n");
} else {
printf("The original and reversed numbers are not equal.\n");
}
return 0;
}
(b) If ages of Ram, Shyam and Ajay are input through the keyboard, write a program to determine the
youngest of the three.
Code:
#include <stdio.h>
int main() {
int ram_age, shyam_age, ajay_age;
return 0;
}
(c) Write a program to check whether a triangle is valid or not, if three angles of the triangle are
entered through the keyboard. A triangle is valid if the sum of all the three angles is equal to 180
degrees.
Code:
#include <stdio.h>
int main() {
int angle1, angle2, angle3;
return 0;
}
(d) Write a program to find the absolute value of a number entered through the keyboard.
Code:
#include <stdio.h>
#include <stdlib.h>
int main() {
int num;
return 0;
}
(e) Given the length and breadth of a rectangle, write a program to find whether the area of the
rectangle is greater than its perimeter. For example, the area of the rectangle with length = 5 and
breadth = 4 is greater than its perimeter.
Code:
#include <stdio.h>
int main() {
int length, breadth, area, perimeter;
return 0;
}
(f) Given three points (x1, y1), (x2, y2) and (x3, y3), write a program to check if the three points fall on
one straight line.
Code:
#include <stdio.h>
int main() {
float x1, y1, x2, y2, x3, y3;
float slope1, slope2;
if (slope1 == slope2) {
printf("The three points fall on one straight line.\n");
} else {
printf("The three points do not fall on one straight line.\n");
}
return 0;
}
(g) Given the coordinates (x, y) of center of a circle and its radius, write a program that will determine
whether a point lies inside the circle, on the circle or outside the circle. (Hint: Use sqrt( ) and pow( )
functions)
Code:
#include <stdio.h>
#include <math.h>
int main() {
float center_x, center_y, radius, point_x, point_y, distance;
printf("Enter the coordinates of the center of the circle (x, y): ");
scanf("%f %f", ¢er_x, ¢er_y);
return 0;
}
(h) Given a point (x, y), write a program to find out if it lies on X-axis, Yaxis or origin.
Code:
#include <stdio.h>
int main() {
float x, y;
if (x == 0 && y == 0) {
printf("The point lies at the origin.\n");
} else if (x == 0) {
printf("The point lies on the Y-axis.\n");
} else if (y == 0) {
printf("The point lies on the X-axis.\n");
} else {
printf("The point does not lie on the X-axis, Y-axis, or origin.\n");
}
return 0;
}
(i) According to Gregorian calendar, it was Monday on the date 01/01/01. If any year is input through
the keyboard write a program to find out what is the day on 1st January of this year.
Code:
#include <stdio.h>
int main() {
int year, day;
switch(day) {
case 0:
printf("Sunday\n");
break;
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
case 6:
printf("Saturday\n");
break;
}
return 0;
}
Code:
#include <stdio.h>
int main() {
int side1, side2, side3;
// Check for invalid triangle (sum of any two sides must be greater than the third)
if (side1 + side2 <= side3 || side2 + side3 <= side1 || side1 + side3 <= side2) {
printf("Invalid triangle (sides must form a valid triangle)\n");
return 1;
}
return 0;
}
Code:
#include <stdio.h>
int main() {
int red, green, blue;
float white, cyan, magenta, yellow, black;
printf("CMYK: Cyan = %.2f, Magenta = %.2f, Yellow = %.2f, Black = %.2f\n", cyan, magenta, yellow,
black);
return 0;
}
Code:
#include <stdio.h>
int main() {
int red, green, blue;
float white, cyan, magenta, yellow, black;
printf("Enter RGB values (0-255): ");
scanf("%d %d %d", &red, &green, &blue);
printf("CMYK: Cyan = %.2f, Magenta = %.2f, Yellow = %.2f, Black = %.2f\n", cyan, magenta, yellow,
black);
return 0;
}
Code:
#include <stdio.h>
int main() {
float hardness, carbon_content, tensile_strength;
int grade;
return 0;
}
(a) Write a program to print all the ASCII values and their equivalent characters using a while loop.
The ASCII values vary from 0 to 255.
Code:
#include <stdio.h>
int main() {
int i = 0;
return 0;
}
(b) Write a program to print out all Armstrong numbers between 1 and 500. If sum of cubes of each
digit of the number is equal to the number itself, then the number is called an Armstrong number. For
example, 153 = ( 1 * 1 * 1 ) + ( 5 * 5 * 5 ) + ( 3 * 3 * 3 ).
Code:
#include <stdio.h>
int main() {
int num, originalNum, remainder, result, i;
originalNum = num;
while (originalNum != 0) {
remainder = originalNum % 10;
result += remainder * remainder * remainder;
originalNum /= 10;
}
if (result == num) {
printf("%d\n", num);
}
}
return 0;
}
(c) Write a program for a matchstick game being played between the
computer and a user. Your program should ensure that the
computer always wins. Rules for the game are as follows:
_x0010_ There are 21 matchsticks.
_x0010_ The computer asks the player to pick 1, 2, 3, or 4 matchsticks.
_x0010_ After the person picks, the computer does its picking.
_x0010_ Whoever is forced to pick up the last matchstick loses the game.
Code:
#include <stdio.h>
int main() {
int matchsticks = 21, user_pick, comp_pick;
while (1) {
printf("Remaining matchsticks: %d\n", matchsticks);
printf("Pick 1, 2, 3, or 4 matchsticks: ");
scanf("%d", &user_pick);
comp_pick = 5 - user_pick;
printf("Computer picks: %d\n", comp_pick);
if (matchsticks <= 0) {
printf("Computer wins!\n");
break;
}
}
return 0;
}
(d) Write a program to enter numbers till the user wants. At the end it should display the count of
positive, negative and zeros entered.
Code:
#include <stdio.h>
int main() {
int num, positive_count = 0, negative_count = 0, zero_count = 0;
char choice;
do {
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0) {
positive_count++;
} else if (num < 0) {
negative_count++;
} else {
zero_count++;
}
return 0;
}
(e) Write a program to receive an integer and find its octal equivalent.
(Hint: To obtain octal equivalent of an integer, divide it continuously by 8 till dividend doesn’t become
zero, then write the remainders obtained in reverse direction.)
Code:
#include <stdio.h>
int main() {
int num, octal_num = 0, remainder, place = 1;
while (num != 0) {
remainder = num % 8;
octal_num += remainder * place;
num /= 8;
place *= 10;
}
return 0;
}
(f) Write a program to find the range of a set of numbers entered through the keyboard. Range is the
difference between the smallest and biggest number in the list.
Code:
#include <stdio.h>
int main() {
int num, smallest, largest;
while (1) {
printf("Enter the next number (or 0 to finish): ");
scanf("%d", &num);
if (num == 0) {
break;
}
return 0;
}
Code:
#include <stdio.h>
int main() {
int num, i;
(b) According to a study, the approximate level of intelligence of a person can be calculated using the
following formula:
i = 2 + ( y + 0.5 x )
Write a program that will produce a table of values of i, y and x, where y varies from 1 to 6, and, for
each value of y, x varies from 5.5 to 12.5 in steps of 0.5.
Code:
#include <stdio.h>
int main() {
float x, y, i;
printf("y\tx\ti\n");
printf("--\t--\t--\n");
return 0;
}
(c) When interest compounds q times per year at an annual rate of r % for n years, the principal p
compounds to an amount a as per the following formula a=p(l+r/q)ng Write a program to read 10 sets
of p, r, n & q and calculate the corresponding as.
Code:
#include <stdio.h>
#include <math.h>
int main() {
float p, r, a;
int n, q, i;
a = p * pow(1 + r / q, n * q);
return 0;
}
Code:
#include <stdio.h>
#include <math.h>
int main() {
float x, sum = 0;
int i;
return 0;
}
(e) Write a program to generate all Pythagorean Triplets with side length less than or equal to 30.
Code:
#include <stdio.h>
int main() {
int a, b, c;
return 0;
}
F)Population of a town today is 100000. The population has increased steadily at the rate of 10% per
year for last 10 years. Write a program to determine the population at the end of each year in the last
decade.
Code:
#include <stdio.h>
#include <math.h>
int main() {
int initial_population = 100000;
float growth_rate = 0.10;
int num_years = 10;
float population;
printf("Year\tPopulation\n");
printf("----\t----------\n");
return 0;
}
G)Ramanujan number (1729) is the smallest number that can be expressed as sum of two cubes in
two different ways-1729 can be expressed as 13 + 123 and 93 + 103. Write a program to print all such
numbers up to a reasonable limit.
Code:
#include <stdio.h>
#include <math.h>
int main() {
int limit = 1000000;
int i, j, k;
return 0;
}
(h) Write a program to print 24 hours of day with suitable suffixes like AM, PM, Noon and Midnight.
Code:
#include <stdio.h>
int main() {
int hour;
return 0;
}
Code:
#include <stdio.h>
int main() {
int i, j = 1;
return 0;
}
Code:
# include <stdio.h>
# include <stdlib.h>
int main( )
{
int choice, num, i, fact ;
while ( 1 )
{
printf ( "\n1. Factorial\n" ) ;
printf ( "2. Prime\n" ) ;
printf ( "3. Odd / Even\n" ) ;
printf ( "4. Exit\n" ) ;
printf ( "Your choice? " ) ;
scanf ( "%d", &choice ) ;
switch ( choice )
{
case 1 :
printf ( "\nEnter number: " ) ;
scanf ( "%d", &num ) ;
fact = 1 ;
for ( i = 1 ; i <= num ; i++ )
fact = fact * i ;
printf ( "Factorial value = %d\n", fact ) ;
break ;
case 2 :
printf ( "\nEnter number: " ) ;
scanf ( "%d", &num ) ;
for ( i = 2 ; i < num ; i++ )
{
if ( num % i == 0 )
{
printf ( "Not a prime number\n" ) ;
break ;
}
if ( i == num )
printf ( "Prime number\n" ) ;
break ;
case 3 :
printf ( "\nEnter number: " ) ;
scanf ( "%d", &num ) ;
if ( num % 2 == 0 )
printf ( "Even number\n" ) ;
else
printf ( "Odd number\n" ) ;
break ;
case 4 :
exit ( 0 ) ; /* Terminates program execution */
default :
printf ( "Wrong choice!\a\n" ) ;
}
}
return 0 ;
}
8.Function:
(a)Any year is entered through the keyboard. Write a function to determine whether the year is a leap
year or not.
Code:
#include <stdio.h>
int main() {
int year;
if (isLeapYear(year)) {
printf("%d is a leap year.\n", year);
} else {
printf("%d is not a leap year.\n", year);
}
return 0;
}
(b)A positive integer is entered through the keyboard. Write a function to obtain the prime factors of
this number. For example, prime factors of 24 are 2, 2, 2 and 3, whereas prime factors of 35 are 5 and
7.
Code:
#include <stdio.h>
void primeFactors(int n) {
int i;
printf("\n");
}
int main() {
int num;
primeFactors(num);
return 0;
}
9.Pointers
(a) Given three variables x, y, z, write a function to circularly shift their values to right. In other words,
if x = 5, y = 8, z = 10, after circular shift y = 5, z = 8, x =10. Call the function with variables a, b, c to
circularly shift values.
Code:
#include <stdio.h>
int main() {
int a = 5, b = 8, c = 10;
return 0;
}
(b) Define a function that receives weight of a commodity in Kilograms and returns the equivalent
weight in Grams, Tons and Pounds. Call this function from main( ) and print the results in main( ).
Code:
#include <stdio.h>
int main() {
float weight_kg, weight_grams, weight_tons, weight_pounds;
return 0;
}
(c) Define a function to compute the distance between two points and use it to develop another
function that will compute the area of the triangle whose vertices are A(x1, y1), B(x2, y2), and C(x3,
y3). Use these functions to develop a function which returns a value 1 if the point (x, y) lines inside the
triangle ABC, otherwise returns a value 0. Would you get any advantage if you develop these
functions to work on call be reference principle?
Code:
#include <stdio.h>
#include <math.h>
float area(float x1, float y1, float x2, float y2, float x3, float y3) {
float a = distance(x1, y1, x2, y2);
float b = distance(x2, y2, x3, y3);
float c = distance(x3, y3, x1, y1);
float s = (a + b + c) / 2;
return sqrt(s * (s - a) * (s - b) * (s - c));
}
int isInside(float x1, float y1, float x2, float y2, float x3, float y3, float x, float y) {
float A = area(x1, y1, x2, y2, x3, y3);
float A1 = area(x, y, x2, y2, x3, y3);
float A2 = area(x1, y1, x, y, x3, y3);
float A3 = area(x1, y1, x2, y2, x, y);
return (A == A1 + A2 + A3);
}
int main() {
float x1, y1, x2, y2, x3, y3, x, y;
return 0;
}
10. Recursion
(a) A positive integer is entered through the keyboard, write a function
to find the binary equivalent of this number:
(1) Without using recursion
(2) Using recursion
Code:
Without recursion:
#include <stdio.h>
int main() {
int num;
binary(num);
return 0;
}
Using recursion:
#include <stdio.h>
binary(num / 2);
printf("%d", num % 2);
}
int main() {
int num;
return 0;
}
#include <stdio.h>
int sumOfFirstN(int n) {
if (n == 0) {
return 0;
} else {
return n + sumOfFirstN(n - 1);
}
}
int main() {
int n = 25;
int sum = sumOfFirstN(n);
return 0;
}
(c) There are three pegs labeled A, B and C. Four disks are placed on peg A. The bottom-most disk is
largest, and disks go on decreasing in size with the topmost disk being smallest. The objective of the
game is to move the disks from peg A to peg C, using peg B as an auxiliary peg. The rules of the game
are as follows:
(1) Only one disk may be moved at a time, and it must be the top
disk on one of the pegs.
(2) A larger disk should never be placed on the top of a smaller
disk.
Write a program to print out the sequence in which the disks should
be moved such that all disks on peg A are finally transferred to peg
C.
Code:
#include <stdio.h>
int main() {
int n = 4;
towerOfHanoi(n, 'A', 'C', 'B');
return 0;
}
12. C preprocessor
(a) If a macro is not getting expanded as per your expectation, how will you find out how is it being
expanded by the preprocessor?
#define TOLOWER(c) (((c) >= 'A' && (c) <= 'Z') ? ((c) + 'a' - 'A') : (c))
#define MAX(a, b, c) (((a) > (b)) ? (((a) > (c)) ? (a) : (c)) : (((b) > (c)) ? (b) : (c)))
(c) Write macro definitions with arguments for calculation of Simple Interest and Amount. Store these
macro definitions in a file “interest.h”. Include this file in your program, and use the macro
definitions for calculating Simple Interest and Amount.
Code:
// interest.h
#include <stdio.h>
#include "interest.h"
int main() {
float principal = 1000.0;
float rate = 5.0;
float time = 2.0;
return 0;
}
13.Array:
(a) Twenty-five numbers are entered from the keyboard into an array. Write a program to find out
how many of them are positive, how many are negative, how many are even and how many odd.
Code:
#include <stdio.h>
int main() {
int numbers[25];
int positive = 0, negative = 0, even = 0, odd = 0;
printf("Enter 25 numbers:\n");
if (numbers[i] > 0) {
positive++;
} else if (numbers[i] < 0) {
negative++;
}
if (numbers[i] % 2 == 0) {
even++;
} else {
odd++;
}
}
return 0;
}
(b) If an array arr contains n elements, then write a program to check if arr[ 0 ] = arr[ n - 1 ], arr[ 1 ] =
arr[ n - 2 ] and so on.
Code:
#include <stdio.h>
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
int symmetric = 1;
if (symmetric) {
printf("The array is symmetric.\n");
} else {
printf("The array is not symmetric.\n");
}
return 0;
}
(c) Write a program using pointers to find the smallest number in an array of 25 integers.
Code:
#include <stdio.h>
int main() {
int arr[25];
int *ptr;
int smallest;
printf("Enter 25 integers:\n");
ptr = arr;
smallest = *ptr;
return 0;
}
Code:
#include <stdio.h>
int main() {
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array: \n");
printArray(arr, n);
insertionSort(arr, n);
return 0;
}
Code:
#include <stdio.h>
int main() {
int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
return 0;
}
Code:
#include <stdio.h>
#include <math.h>
#define NUM_ELEMENTS 15
int main() {
int data[NUM_ELEMENTS] = {-6, -12, 8, 13, 11, 6, 7, 2, -6, -9, 10, 11, 10, 9, 2};
float sum = 0.0, mean, variance = 0.0, standard_deviation;
return 0;
}
Code:
#include <stdio.h>
#include <math.h>
#define PI 3.14159265358979323846
int main() {
int num_plots = 6;
// Data for each plot (can be stored in a structure for better organization)
double plots[num_plots][4] = {
{137.4, 80.9, 0.78}, // Plot 1
{155.2, 92.62, 0.89}, // Plot 2
{149.3, 160.0, 1.35}, // Plot 3
{100.25, 9.00, 1.25}, // Plot 4
{155.6, 68.95, 1.25}, // Plot 5
{149.7, 120.0, 1.75}, // Plot 6
};
return 0;
}
Code:
#include <stdio.h>
#include <math.h>
#define NUM_DATA_POINTS 12
int main() {
double x[NUM_DATA_POINTS] = {
34.22, 39.87, 41.85, 43.23, 40.06, 53.29, 53.29, 54.14, 49.12, 40.71, 55.15, 94.65
};
double y[NUM_DATA_POINTS] = {
102.43, 100.93, 97.43, 97.81, 98.32, 98.32, 100.07, 97.08, 91.59, 94.85, 94.65, 94.65
};
double sum_x = 0.0, sum_y = 0.0, sum_xy = 0.0, sum_x2 = 0.0, sum_y2 = 0.0;
return 0;
}
(i)The X and Y coordinates of 10 different points are entered through the keyboard. Write a program
to find the distance of last point from the first point (sum of distances between consecutive points).
Code:
#include <stdio.h>
#include <math.h>
int main() {
float x[10], y[10];
float distance = 0.0;
printf("The distance of the last point from the first point is: %.2f\n", distance);
return 0;
}
(j) A dequeue is an ordered set of elements in which elements may be inserted or retrieved from
either end. Using an array simulate a dequeue of characters and the operations retrieve left, retrieve
right, insert left, insert right. Exceptional conditions such as dequeue full or empty should be
reported. Use two pointers left and right for this simulation.
Code:
#include <stdio.h>
#define MAX_SIZE 10
char deque[MAX_SIZE];
int left = -1, right = -1;
void retrieveLeft() {
if (left == -1) {
printf("Deque is empty. Cannot retrieve from left.\n");
} else {
printf("Retrieved %c from the left end.\n", deque[left]);
if (left == right) {
left = -1;
right = -1;
} else {
if (left == MAX_SIZE - 1) {
left = 0;
} else {
left = left + 1;
}
}
}
}
void retrieveRight() {
if (left == -1) {
printf("Deque is empty. Cannot retrieve from right.\n");
} else {
printf("Retrieved %c from the right end.\n", deque[right]);
if (left == right) {
left = -1;
right = -1;
} else {
if (right == 0) {
right = MAX_SIZE - 1;
} else {
right = right - 1;
}
}
}
}
int main() {
insertLeft('A');
insertRight('B');
insertLeft('C');
insertRight('D');
retrieveLeft();
retrieveRight();
retrieveLeft();
retrieveRight();
retrieveLeft();
retrieveRight();
return 0;
}
14.Multidimensional Array:
(a) How will you initialize a three-dimensional array threed[ 3 ][ 2 ][ 3]? How will you refer the first
and last element in this array?
Code:
int threed[3][2][3] = {
{
{1, 2, 3},
{4, 5, 6}
},
{
{7, 8, 9},
{10, 11, 12}
},
{
{13, 14, 15},
{16, 17, 18}
}
};
(b) Match the following with reference to the program segment given
below:
int i, j, = 25 ;
int *pi, *pj = & j ;
*pj = j + 5 ;
j = *pj + 5 ;
pj = pj ;
*pi = i + j ;
Ans)
a. 30
b. F9E
c. 35
e. F9C
h. 65
i. F9E
j. F9E
Ans)a. 9
b. 13
c. 4
d. 3
f. 12
g. 14
h. 7
j. 8
k. 5
l. 10
m. 6
Ans:
k. 5
i. 20
c. 6
d. 3
e. 0
f. 16
g. 1
h. 11
j. 2
l. 4
int main() {
int matrix[MAX_SIZE][MAX_SIZE];
int size, i, j, symmetric = 1;
return 0;
}
(f)Write a program to add two 6 x 6 matrices.
Code:
#include <stdio.h>
int main() {
int matrix1[6][6], matrix2[6][6], sum[6][6];
int i, j;
return 0;
}
int main() {
int matrix1[3][3], matrix2[3][3], result[3][3];
int i, j, k;
return 0;
}
arr[size - 2] = temp1;
arr[size - 1] = temp2;
}
int main() {
int matrix[4][5] = {
{15, 30, 28, 19, 61},
{12, 45, 67, 89, 90},
{11, 22, 33, 44, 55},
{10, 20, 30, 40, 50}
};
printf("Original matrix:\n");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 5; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
printf("\nShifted matrix:\n");
for (int i = 0; i < 4; i++) {
shiftLeft(matrix[i], 5);
for (int j = 0; j < 5; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}