New Text Document
New Text Document
----
float a,b;
scanf("%f",&a);
scanf("%f",&b);
-----
double a,b;
scanf("%lf",&a);
scanf("%lf",&b);
--2. input n element into arr integer
// input n
int n;
scanf("%d",&n);
// declear array
int arr[n];
scanf("%d", arr[i]);
}
--2.1 if else condition
if(){
printf("");
}else{
printf("");
}
--2.2 for if else
for(int i = 0; i < n;i++){
if(){
printf("");
}else{
printf("");
}
}
--2.3 convert base number from base 2
// Function to convert binary to decimal
long long binaryToDecimal(long long binary) {
long long decimal = 0;
long long base = 1;
if (decimal == 0) {
printf("Binary representation: 0\n");
return;
}
if (decimal == 0) {
printf("Octal representation: 0\n");
return;
}
if (decimal == 0) {
printf("Hexadecimal representation: 0\n");
return;
}
}
}
}
}
--3.2 function to count even number or old number in array
void countEvenAndOdd(int arr[], int size, int *evenCount, int *oddCount) {
*evenCount = 0;
*oddCount = 0;
return product;
}
--3.4 sum of digits
// 123 -> 6
int sumOfDigits(int number) {
int sum = 0;
while (number != 0) {
int digit = number % 10; // Extract the last digit
sum += digit;
number /= 10; // Remove the last digit from the number
}
return sum;
}
--3.5 Size of int array
size_t n = sizeof(a) / sizeof(int);
}else{
printf(" ");
}
}
printf("\n");
}
--5.1
// Function to print right triangle pattern
void printRightTriangle(int rows) {
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
}
--5.2
// Function to print left triangle pattern
void printLeftTriangle(int rows) {
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= rows - i; j++) {
printf(" ");
}
for (int j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
}
--5.3 inverted triangle
void printInvertedRightTriangle(int n) {
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= n - i; j++) {
printf(" ");
}
for (int j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
}
--5.4 Function to print a rectangular shape filled with character '*'
// Print stars
for (int j = 1; j <= starCount; j++) {
printf("*");
}
printf("\n");
}
// Print stars
for (int j = 1; j <= starCount; j++) {
printf("*");
}
printf("\n");
}
}
--5.6 Function to print a half pyramid pattern with height 'n'
// Function to print a half pyramid pattern with height 'n'
void printHalfPyramid(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
}
--5.7 Function to print a half pyramid pattern with height 'n' with palindrome
// Function to print a half pyramid pattern with height 'n'
void shapingWords(int n) {
char ch = 'A';
int i,j;
if(n > 0) {
for(i=1;i<=n;i++) {
for(j=1;j<=i-1;j++) printf("%c",ch+j-1);
printf("%c",ch+i-1);
for(j=i-1;j>=1;j--)printf("%c",ch+j-1);
if(i != n) printf("\n");
}
}
}
--6. remove ele with position p in array
void removeElement(int arr[], int *size, int position) {
if (position < 0 || position >= *size) {
printf("Invalid position. Cannot remove element.\n");
return;
}
// Shift elements to the right to make space for the new element
for (int i = *size; i > position; i--) {
arr[i] = arr[i - 1];
}
// Find and print each pair (frequency, divisibility) for the elements in the
array
printf("Element : Frequency : Divisibility (by %d)\n", k);
for (int i = 0; i < size; i++) {
if (hashTable[arr[i]] > 0) {
printf("%d : %d : %s\n", arr[i], hashTable[arr[i]], (arr[i] % k == 0) ?
"Yes" : "No");
hashTable[arr[i]] = 0; // Mark the element as visited
}
}
}
--8. swap the places of the first largest and smallest even number with each other
in array
[1,2,3,5,1,8] -> [1,8,3,5,1,2]
void swap(int *x, int *y) {
int temp = *x;
*x = *y;
*y = temp;
}
void swapFirstLargestAndSmallestEven(int arr[], int size) {
int largestEvenIndex = -1;
int smallestEvenIndex = -1;
// Find the indices of the first largest and smallest even numbers
for (int i = 0; i < size; i++) {
if (arr[i] % 2 == 0) {
if (largestEvenIndex == -1 || arr[i] > arr[largestEvenIndex]) {
largestEvenIndex = i;
}
if (smallestEvenIndex == -1 || arr[i] < arr[smallestEvenIndex]) {
smallestEvenIndex = i;
}
}
}
return sum;
}
--8.2 Function to find the two-digit number(s) that appear the most in the array
return count;
}
--9.4 count Words Starting With Prefix
int countWordsStartingWithPrefix(char *inputString, char *prefix) {
int count = 0;
int inputLen = strlen(inputString);
int prefixLen = strlen(prefix);
return count;
}
--9.5 Function to convert uppercase vowels and lowercase consonants in a string
#include <ctype.h>
strcpy(reversedP, p);
reverseString(reversedP);
free(reversedP);
}
--9.8 count occurrences of 'p' with its original version in 'o'
int count = 0;
for (int i = 0; i <= oLen - pLen; i++) {
if (strncmp(o + i, p, pLen) == 0) {
count++;
}
}
return count;
}
--9.9 // Function to display the binary representation of a string 's' of length
'n'
void printBinaryRepresentation(const char* s, int n) {
for (int i = 0; i < n; i++) {
unsigned char ch = s[i];
for (int j = 7; j >= 0; j--) {
int bit = (ch >> j) & 1;
printf("%d", bit);
}
printf(" ");
}
printf("\n");
}
--9.10
// Function to display the hexadecimal representation and integer value of a
character
void printHexAndInteger(char c) {
printf("%02X %d\n", c, c);
}
--9.11
// Function to find and display the first two pairs of frequency_of_character and
the_character
void findFirstTwoPairs(const char* str) {
int frequency[256] = {0}; // Assuming ASCII character set
// Count the frequency of each character in the string
for (int i = 0; str[i] != '\0'; i++) {
frequency[(unsigned char)str[i]]++;
}
if (count == 2) {
break; // Stop after finding the first two pairs
}
}
}
--9.12
// Function to find and replace the first occurrence of 's2' with 's3' in 's1'
void findAndReplace(char* s1, const char* s2, const char* s3) {
char* found = strstr(s1, s2);
if (found != NULL) {
// Calculate the length of the parts before and after the first occurrence
of 's2'
int beforeLen = found - s1;
int afterLen = strlen(s1) - (beforeLen + strlen(s2));
// Construct the resultant string with 's3' replacing the first occurrence
of 's2'
char result[100];
strncpy(result, s1, beforeLen);
result[beforeLen] = '\0';
strcat(result, s3);
strcat(result, found + strlen(s2));
#include <ctype.h>
while (*s) {
if (isalpha(*s)) {
if (capitalizeNext) {
*s = toupper(*s); // Capitalize the first letter of the word
capitalizeNext = 0; // Set the flag to lowercase the rest of the
word
} else {
*s = tolower(*s); // Lowercase the rest of the word
}
} else {
// If the current character is not an alphabetic character, set the
flag to capitalize the next character
capitalizeNext = 1;
}
s++;
}
}
--9.14 sort string theo alphabe
#include<stdlib.h>
#include<string.h>
#include<ctype.h> // uppercase lowsercase
int cmp(const void* a,const void* b){
return *(char*)a - *(char*)b;
}
void sortAscending(char arr[], int size){
qsort(arr,size,sizeof(char),cmp);
}
--9.15 check digit
isdigit('3'); // ctype.h
--9.16 char to string
char c = '1';
char str1[2] = {c , '\0'};
char str2[5] = "";
strcpy(str2,str1);
int power = 1;
*exponent = 0;
if (power == a) {
return 1; // If a is a power of n, return true.
}
return 0; // If a is not a power of n, return false.
}
--13. check number is perfect
int isPerfectNumber(int number) {
if (number <= 0) {
return 0; // Perfect numbers are positive integers.
}
int sum = 0;
int i, j;
for (i = 0, j = 0; str[i]; i++) {
if (isalpha(str[i])) {
str[j] = str[i]; // Move the alphabet character to its proper position.
j++;
}
}
str[j] = '\0'; // Null-terminate the string after the last alphabet character.
}
--15.