AllCodes
AllCodes
i)
#include <stdio.h>
int main() {
int num, sum = 0;
printf("Enter a number: ");
scanf("%d", &num);
while (num != 0) {
sum += num % 10;
num = num / 10;
}
printf("Sum of digits: %d\n", sum);
return 0;
}
Input:
Enter a number: 1234
Output:
Sum of digits: 10
1. ii)
#include <stdio.h>
#include <math.h>
int main() {
int num, i;
int Prime = 1;
return 0;
}
Input 1:
Enter a number: 29
Output 1:
Prime
Input 2:
Enter a number: 0
Output 2:
Not Prime
1. iii)
#include <stdio.h>
int main() {
int n, i, num;
int sum = 0;
float average;
printf("Enter the number of elements: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
printf("Enter number %d: ", i);
scanf("%d", &num);
sum += num; // Add the number to sum
}
average = (float)sum / n;
printf("Sum = %d\n", sum);
printf("Average = %.2f\n", average);
return 0;
}
Input:
Enter the number of elements: 4
Enter number 1: 10
Enter number 2: 20
Enter number 3: 30
Enter number 4: 40
Output:
Sum = 100
Average = 25.00
1. iv)
#include <stdio.h>
#include <math.h>
int main() {
int num, temp, digit, n = 0;
int sum = 0;
printf("Enter a number: ");
scanf("%d", &num);
temp = num;
while (temp > 0) {
n++;
temp /= 10;
}
temp = num;
while (temp > 0) {
digit = temp % 10;
sum += pow(digit, n);
temp /= 10;
}
return 0;
}
Input 1:
Enter a number: 9474
Output 1:
9474 is an Armstrong Number.
Input 2:
Enter a number: 123
Output 2:
123 is not an Armstrong Number.
2.i)
#include <stdio.h>
int main() {
int n, i;
int a = 0, b = 1, c;
printf("Enter the number of Fibonacci terms: ");
scanf("%d", &n);
if (n >= 1) {
printf("%d ", a);
}
if (n >= 2) {
printf("%d ", b);
}
2. ii)
#include <stdio.h>
#include<math.h>
int main() {
int n, i, a[100];
float avg, sd, s=0;
printf("How many numbers\n");
scanf("%d", &n);
printf("Enter the numbers\n");
for(i=0; i<n; i++){
scanf("%d", &a[i]);
s=s+a[i];
}
avg = s/n;
printf("The sum is: \n");
printf("The average is \n");
s=0;
for(i=0; i<n; i++){
s=s+(a[i]-avg)*(a[i]-avg);
}
sd = sqrt(s/n);
printf("Standard deviation is: \n");
return 0;
}
Input:
Enter the number of elements: 4
Enter number 1: 5
Enter number 2: 10
Enter number 3: 15
Enter number 4: 20
Output:
Sum = 50.00
Average = 12.50
Standard Deviation = 5.59
2. iii)
#include <stdio.h>
int main() {
int n, i, a[100], key;
printf("Enter the number of characters\n");
scanf("%d",&n);
printf("Enter the sorted elements\n")
for(i=0; i<n; i++){
scanf("%d", &a[i]);
}
printf("Enter the key to be found\n");
scanf("%d", &key);
l=0; u=n-1; flag=1;
while(l<=u){
mid=(l+n)/2;
if(a[mid]==key){
printf("No. found at %d", mid);
flag=0;
break;
}
if(a[mid]>key){
u=mid-1;
}
if(a[mid]<key){
l=mid+1;
}
}
if(flag==1){
printf("Not found");
}
return 0;
}
Input:
Enter the number of characters
5
Enter the sorted elements
1
3
5
7
9
Enter the key to be found
7
Output:
No. found at 3
2. iv)
#include <stdio.h>
int main() {
int n, i, a[100], key;
printf("Enter the number of elements\n");
scanf("%d",&n);
printf("Enter the elements\n")
for(i=0; i<n; i++){
scanf("%d", &a[i]);
}
printf("Enter the key to be found\n");
scanf("%d", &key);
flag=1;
for(i=0; i<n; i++){
if(a[i] == key){
printf("Found at the %d location", i);
flag=0;
break;
}
if(flag == 1){
printf("Not found")
}
return 0;
}
Input:
Enter the number of elements
5
Enter the elements:
10
20
30
40
50
Enter the key to be found
30
Output:
Found at 2 location
3. i)
#include <stdio.h>
int main() {
int i, j, minIndex, temp, n;
scanf("%d", &n);
for (i = 0; i < n-1; i++) {
minIndex = i;
for (j = i+1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
printf("Sorted array: \n");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Input:
5
64 25 12 22 11
Output:
Sorted array:
11 12 22 25 64
3. ii)
#include <stdio.h>
int main() {
int n, i, j, key;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements: ");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
printf("Sorted array: \n");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Input:
Enter the number of elements: 5
Enter the elements: 64 25 12 22 11
Output:
Sorted array:
11 12 22 25 64
3. iii)
#include <stdio.h>
int main() {
int n, i, j, temp;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements: ");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
// Swap arr[j] and arr[j+1]
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
printf("Sorted array: \n");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Input:
Enter the number of elements: 5
Enter the elements: 6 5 1 2 8
Output:
Sorted array:
1 2 5 6 8
4. i)
#include <stdio.h>
#include <string.h>
int main() {
char word[100];
int start, end, isPalindrome;
printf("Enter a word: ");
scanf("%s", word);
start = 0;
end = strlen(word) - 1;
isPalindrome = 1;
while (start < end) {
if (word[start] != word[end]) {
isPalindrome = 0;
break;
}
start++;
end--;
}
if (isPalindrome) {
printf("The word '%s' is a palindrome.\n", word);
} else {
printf("The word '%s' is not a palindrome.\n", word);
}
return 0;
}
Input 1:
Enter a word: radar
Output 1:
The word 'radar' is a palindrome.
Input 2:
Enter a word: hello
Output 2:
The word 'hello' is not a palindrome.
4. ii)
#include <stdio.h>
int main() {
char str[100];
int length = 0;
printf("Enter a string: ");
scanf("%s", str);
while (str[length] != '\0') {
length++;
}
printf("The length of the string is: %d\n", length);
return 0;
}
Input:
Enter a string: hello
Output:
The length of the string is: 5
4. iii)
#include <stdio.h>
int main() {
char source[100], destination[100];
int i = 0;
printf("Enter the source string: ");
scanf("%s", source);
while (source[i] != '\0') {
destination[i] = source[i];
i++;
}
destination[i] = '\0';
printf("The copied string is: %s\n", destination);
return 0;
}
Input:
Enter the source string: hello
Output;
The copied string is: hello
4.iv)
#include <stdio.h>
int main() {
char str1[20], str2[20];
int i = 0, j = 0;
printf("Enter the first string: ");
scanf("%s", str1);
printf("Enter the second string: ");
scanf("%s", str2);
while (str1[i] != '\0') {
i++;
}
while (str2[j] != '\0') {
str1[i] = str2[j];
i++;
j++;
}
str1[i] = '\0';
printf("The combined string is: %s\n", str1);
return 0;
}
Input:
Enter the first string: hello
Enter the second string: world
Output:
The combined string is: helloworld
4.v)
#include <stdio.h>
int main() {
char str1[20], str2[20];
int i = 0;
printf("Enter the first string: ");
scanf("%s", str1);
printf("Enter the second string: ");
scanf("%s", str2);
while (str1[i] != '\0' && str2[i] != '\0') {
if (str1[i] != str2[i]) {
break;
}
i++;
}
if (str1[i] > str2[i]) {
printf("The first string is greater than the second string.\n");
} else if (str1[i] < str2[i]) {
printf("The first string is less than the second string.\n");
} else {
printf("The two strings are equal.\n");
}
return 0;
}
Input 1:
Enter the first string: apple
Enter the second string: banana
Output 1:
The first string is less than the second string.
Input 2:
Enter the first string: hello
Enter the second string: hello
Output 2:
The two strings are equal.
4.vi)
#include <stdio.h>
int main() {
char str[20];
int i = 0;
printf("Enter a string: ");
scanf("%s", str);
while (str[i] != '\0') {
if (str[i] >= 'a' && str[i] <= 'z') {
str[i] = str[i] - 32;
}
i++;
}
printf("The string in uppercase is: %s\n", str);
return 0;
}
Input:
Enter a string: hello
Output:
The string in uppercase is: HELLO
4. vii)
#include <stdio.h>
int main() {
char str[100];
int i = 0, wordCount = 0;
int inWord = 0;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
while (str[i] != '\0') {
if (str[i] != ' ' && str[i] != '\n' && inWord == 0) {
inWord = 1;
wordCount++;
} else if (str[i] == ' ' || str[i] == '\n') {
inWord = 0;
}
i++;
}
printf("The number of words in the string is: %d\n", wordCount);
return 0;
}
Input:
Enter a string: This is a test string
Output:
The number of words in the string is: 5
4. viii)
#include <stdio.h>
int main() {
char str[100], substr[100];
int i = 0, j = 0;
int found = 0;
printf("Enter the main string: ");
fgets(str, sizeof(str), stdin);
printf("Enter the substring: ");
fgets(substr, sizeof(substr), stdin);
while (str[i] != '\0') {
if (str[i] == '\n') {
str[i] = '\0';
}
i++;
}
i = 0;
while (substr[j] != '\0') {
if (substr[j] == '\n') {
substr[j] = '\0';
}
j++;
}
i = 0;
while (str[i] != '\0') {
if (str[i] == substr[0]) {
j = 0;
while (substr[j] != '\0' && str[i + j] == substr[j]) {
j++;
}
if (substr[j] == '\0') {
found = 1;
break;
}
}
i++;
}
if (found) {
printf("The substring is found in the string.\n");
} else {
printf("The substring is not found in the string.\n");
}
return 0;
}
4. ix)
#include <stdio.h>
int main() {
char str[20];
int start = 0, end = 0;
char temp;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
while (str[end] != '\0' && str[end] != '\n') {
end++;
}
end--;
while (start < end) {
temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
printf("The reversed string is: %s\n", str);
return 0;
}
Input:
Enter a string: Hello World
Output:
The reversed string is: dlroW olleH