0% found this document useful (0 votes)
7 views19 pages

elephant

The document contains various C source code examples demonstrating different programming concepts. These include summation of series, array manipulation, finding pairs in arrays, matrix operations, word counting in strings, text transformation, base conversion, string reversal, and expression evaluation using stacks. Each section provides a brief description of the functionality along with the corresponding code.

Uploaded by

2403717662221016
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views19 pages

elephant

The document contains various C source code examples demonstrating different programming concepts. These include summation of series, array manipulation, finding pairs in arrays, matrix operations, word counting in strings, text transformation, base conversion, string reversal, and expression evaluation using stacks. Each section provides a brief description of the functionality along with the corresponding code.

Uploaded by

2403717662221016
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

SUMMATION OF SERIES

SOURCE CODE:
#include <stdio.h>
int main() {
int sum = 0;
for(int i=1;i<=100;i++){
if(i%3==0 || i%5==0 || i%7==0){
sum += i;
}
else{
continue;
}
}
printf("Sum: %d",sum);
return 0;
}

OUTPUT:
EXCHANGE OF VALUES BETWEEN ARRAYS

SOURCE CODE:
#include <stdio.h>
int main() {
int n,i, arr2[10], arr3[10];
int odd=0, even=0;
printf("Enter the array size: ");
scanf("%d",&n);
int arr1[n];
printf("Enter the Array Elements: \n");
for(i=0; i<n ;i++){
scanf("%d",&arr1[i]);
if(arr1[i]%2==0){
arr3[even++] = arr1[i];
}
else{
arr2[odd++] = arr1[i];
}
}
printf("\nOdd Array: ");
for(i=0;i<odd;i++){
printf("%d ",arr3[i]);
}
printf("\nEven Array: ");
for(i=0;i<even;i++){
printf("%d ",arr2[i]);
}
return 0;
}
OUTPUT:
FINDING PAIRS FOR A SPECIFIC
CONDITION IN AN ARRAY

SOURCE CODE:
#include <stdio.h>
void main() {
int n;
printf("Enter the array size: ");
scanf("%d",&n);
int arr[n];
printf("Enter the array elements:\n");
for(int i; i<n; i++){
scanf("%d",&arr[i]);
}
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(arr[i] < arr[j])
printf("(%d,%d)",arr[i],arr[j]);
}
printf("\n");
}
}
OUTPUT:
CONVERTING VALUES TO LOWER AND
UPPER TRIANGULAR IN A MATRIX

SOURCE CODE:
#include <stdio.h>
void row_swap(int arr[5][5], int row1, int row2)
{
for(int i=0;i<5;i++){
if(arr[row1][i] > arr[row2][i]){
int temp = arr[row1][i];
arr[row1][i] = arr[row2][i];
arr[row2][i] = temp;
}
}
}
void column_swap(int arr[5][5], int col1, int col2)
{
for(int i=0;i<5;i++){
if(arr[i][col1] > arr[i][col2]){
int temp = arr[i][col1];
arr[i][col1] = arr[i][col2];
arr[i][col2] = temp;
}
}
}
void display(int arr[5][5])
{
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
printf("%d\t",arr[i][j]);
}
printf("\n");
}
}
int main()
{
int arr[5][5];
printf("Enter the 5*5 matrix:\n");
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
scanf("%d",&arr[i][j]);
}
}
row_swap(arr,0,4);
row_swap(arr,1,3);
printf("\n After Row Swapping:\n");
display(arr);
column_swap(arr,0,4);
column_swap(arr,1,3);
printf("\n After Column Swapping:\n");
display(arr);
return 0;
}
OUTPUT:
COUNTING FOR THE SAME LENGTH
WORDS IN A GIVEN STRING

SOURCE CODE:
#include <stdio.h>
int main() {
char str[]="Jana-gana-mana-adhinayaka, jaya he Bharata-bhagya-vidhata Panjaba-Sindha
Gujrata-Maharata-Dravida-Utkala-Vanga Vindhya-Himachala-Yamuna-Ganga Uchhala-
Jaladhi-taranga Tava shubha name jage Tava shubha ashisha mage Gave tava jaya-gatha Jana-
gana-mangala-dayaka jaya he Bharata-bhagya vidhata.Jaya he! Jaya he! Jaya he! Jaya jaya
jaya, jaya he!";
int one=0, two = 0, three=0, four=0, five=0;
int len = 0, six=0, seven=0, eight=0, nine=0, ten=0;;
for(int i=0;str[i]!='\0';i++)
{
if(str[i]!=' '&& str[i]!=',' && str[i]!='-' && str[i]!='!' && str[i]!='.')
len++;
else
{
switch(len)
{
case 1:
one ++;
break;
case 2:
two ++;
break;
case 3:
three ++;
break;
case 4:
four ++;
break;
case 5:
five ++;
break;
case 6:
six ++;
break;
case 7:
seven ++;
break;
case 8:
eight ++;
break;
case 9:
nine ++;
break;
case 10:
ten ++;
break;
}
len =0;
}
}
printf("\nOne Letter word: %d",one);
printf("\nTwo Letter word: %d",two);
printf("\nThree Letter word: %d",three);
printf("\nFour Letter word: %d",four);
printf("\nFive Letter word: %d",five);
printf("\nSix Letter word: %d",six);
printf("\nSeven Letter word: %d",seven);
printf("\nEight Letter word: %d",eight);
printf("\nNine Letter word: %d",nine);
printf("\nTen Letter word: %d",ten);
return 0;
}

OUTPUT:
CONVERTING PLAIN TEXT
TO CIPHER TEXT

SOURCE CODE:
#include <stdio.h>
#include <ctype.h>
int main() {
char str[50] ;
printf("Enter a String: ");
scanf("%[^\n]s", str);
int i = 0;
printf("\nAfter Transformation: ");
while (str[i] != '\0') {
char c = str[i];
if(c==' '){
i++;
printf(" ");
}
else{
char lc = tolower(c);
if (lc == 'a')
c = 'e';
else if (lc == 'e')
c = 'i';
else if (lc == 'i')
c = 'o';
else if (lc == 'o')
c = 'u';
else if (lc == 'u')
c = 'a';
else if (isalpha(c))
c = c + 2;
}
printf("%c", c);
i++;
}
return 0;
}

OUTPUT:
BASE CONVERSION FROM BINARY
TO DECIMAL, HEXADECIMAL

SOURCE CODE:
#include <stdio.h>
#include <string.h>
void binary(int num, char *str, int *index)
{
if(num>1)
binary(num/2, str, index);
str[(*index)++] = (num%2)+'0';
str[*index] = '\0';
}
void hexadecimal(char *str, int len, int idx)
{
if(idx >= len)
return;
int decimalvalue = 0;
for(int i=0; i<4 && (idx+i)<len; i++){
decimalvalue = decimalvalue * 2+(str[idx+i]-'0');
}
if(decimalvalue < 10)
printf("%d",decimalvalue);
else
printf("%c",decimalvalue-10+'A');
hexadecimal(str,len,idx+4);
}
int main() {
int num;
char str[33];
int index=0;
printf("Enter an integer: ");
scanf("%d",&num);
binary(num,str,&index);
printf("Binary: %s\n",str);
int len = strlen(str);
int remainder = len%4;
if(remainder != 0){
for(int i=0;i<(4-remainder);i++)
printf("0");
}
printf("Hexadeciamal: ");
hexadecimal(str,len,0);
printf("\n");
return 0;
}

OUTPUT:
STRING REVERSAL USING STACK

SOURCE CODE:
#include <stdio.h>
#include <string.h>
#define MAX 100
int main() {
char str[MAX], stack[MAX];
int top = -1, i;
printf("Enter a string: ");
scanf("%s", str);
for (i = 0; i < strlen(str); i++) {
stack[++top] = str[i];
}
printf("Reversed string: ");
while (top >= 0) {
printf("%c", stack[top--]);
}
return 0;
}

OUTPUT:
EVALUATION OF EXPRESSION USING STACK

SOURCE CODE:
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#define MAX 100
int stack[MAX];
int top = -1;
void push (int value)
{
if (top == MAX - 1) {
printf("Stack overflow\n");
exit(1);
}
stack[++top] = value;
}
int pop()
{
if (top == -1) {
printf("Stack underflow\n");
exit(1);
}
return stack[top--];
}
int evaluatePostfix (const char* expression)
{
for (int i = 0; expression[i] != '\0'; i++)
{
if (isdigit(expression[i]))
push (expression[i] - '0');
else {
int val2 = pop();
int val1 = pop();
switch (expression[i])
{
case '+':
push(val1+ val2);
break;
case '-':
push (val1 - val2);
break;
case '*':
push (val1 * val2);
break;
case '/':
if(val2 == 0){
printf("Error: Division by zero\n");
exit(1);
}
push (val1 / val2);
break;
default:
printf("Error: Unknown operator c\n", expression[i]);
exit(1);
}
}
}
return pop();
}
int main()
{
char expression [MAX];
printf("Enter a postfix expression: ");
scanf("%s", expression);
int result = evaluatePostfix(expression);
printf("The result is: %d\n", result);
return 0;
}

OUTPUT:

You might also like