1.
Write a program in C to print a vertically aligned sinusoidal
curve defined by Y = A * Sin (θ). The user will provide the amplitude
'A' and the number of full cycles to be printed.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
In the given example, the curve is printed for 2 full cycles.
#include <stdio.h>
#include <math.h>
#define PI 3.14159265
int main() {
int A, cycles;
printf("Enter amplitude (A): ");
scanf("%d", &A);
printf("Enter number of full cycles: ");
scanf("%d", &cycles);
int points = 360 * cycles; // total angular points
for (int i = 0; i <= points; i += 10) { // 10-degree step for smoothness
double theta = i * PI / 180.0; // convert to radians
double y = A * sin(theta);
int spaces = (int)(y + A); // shift so all y are positive for printing
for (int j = 0; j < spaces; j++)
printf(" ");
printf("*\n");
return 0;
OUTPUT:
2. Write a Program in C to solve the following problem.
You are given a string number representing a positive integer and a character digit.
Return
the resulting string after removing exactly one occurrence of a digit
from the number, such that the value of the resulting string in decimal
form is maximised. The test cases are generated such that a digit occurs
at least once in the number.
#include <stdio.h>
#include <string.h>
// Function to remove one occurrence of 'digit' to maximize result
void removeDigit(char number[], char digit) {
int len = strlen(number);
int indexToRemove = -1;
// Find first occurrence where removing the digit increases the value
for (int i = 0; i < len - 1; i++) {
if (number[i] == digit && number[i] < number[i + 1]) {
indexToRemove = i;
break; }
// If no such position found, remove the last occurrence
if (indexToRemove == -1) {
for (int i = len - 1; i >= 0; i--) {
if (number[i] == digit) {
indexToRemove = i;
break;
// Print result after removing chosen digit
printf("Output: ");
for (int i = 0; i < len; i++) {
if (i != indexToRemove)
printf("%c", number[i]);
printf("\n");
}
int main() {
char number[100];
char digit;
printf("Enter number: ");
scanf("%s", number);
printf("Enter digit to remove: ");
scanf(" %c", &digit);
removeDigit(number, digit);
return 0;
3. Write a program in C to implement Binary Search using Recursion.
#include <stdio.h>
// Recursive binary search function
int binarySearch(int arr[], int low, int high, int key) {
if (low > high)
return -1; // Not found
int mid = (low + high) / 2;
if (arr[mid] == key)
return mid;
else if (arr[mid] > key)
return binarySearch(arr, low, mid - 1, key);
else
return binarySearch(arr, mid + 1, high, key);
int main() {
int n, key;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d sorted elements: ", n);
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
printf("Enter element to search: ");
scanf("%d", &key);
int result = binarySearch(arr, 0, n - 1, key);
if (result != -1)
printf("Element found at index %d\n", result);
else
printf("Element not found\n");
return 0;
Input:
2 4 6 8 10
Output:
Element found at index 3