Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

Faculty of Engineering & Technology

303105104 - Computational Thinking for Structured Design-1

Practical 7

Write a C program to find the largest integer in a list of integers.


#include <stdio.h>
#include <conio.h>
void main()
{
int a[25], i, large, small, n;
clrscr();
printf("Enter the size of array(max 25)\n");
scanf("%d", &n);
printf("Enter any %d integer array elements\n",n);
for(i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
large = a[0];
small = a[0];
for(i = 1; i < n ; i++)
{
if(a[i] > large)
{
large = a[i];
}
if(a[i] < small)
{
small = a[i];
}
}
printf("The largest element from the given array is %d \nThe smallest element from the
given array
is %d", large, small);
getch();
Enrollment Number : 23UG032890 Page No: 27
Faculty of Engineering & Technology
303105104 - Computational Thinking for Structured Design-1

Enrollment Number : 23UG032890 Page No: 28


Faculty of Engineering & Technology
303105104 - Computational Thinking for Structured Design-1

2.Write a C program that uses functions to perform the following:


1.Addition of Two Matrices
#include <stdio.h>
#include <conio.h>
void main()
{
int a[3][3], b[3][3], c[3][3], i, j;
clrscr();
printf("Enter the elements of 3*3 matrix a \n")
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
scanf("%d", &a[i][j])
}
}
printf("Enter the elements of 3*3 matrix b \n");
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
scanf("%d", &b[i][j]);
}
}
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
c[i][j] = a[i][j] + b[i][j];
}

Enrollment Number : 23UG032890 Page No: 29


Faculty of Engineering & Technology
303105104 - Computational Thinking for Structured Design-1

}
printf("The resultant 3*3 matrix c is

\n");
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
printf("%d\t", c[i][j]);
}
printf("\n");
}
getch();
}

Enrollment Number : 23UG032890 Page No: 30


Faculty of Engineering & Technology
303105104 - Computational Thinking for Structured Design-1

2. Multiplication of Two Matrices


#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3], b[3][3], c[3][3], i, j, k;
clrscr();
printf("Enter the elements of 3*3 matrix a \n");
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("Enter the elements of 3*3 matrix b \n");
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
scanf("%d", &b[i][j]);
}
}
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
c[i][j] = 0
for(k = 0; k < 3; k++)
{
c[i][j] = c[i][j] + (a[i][k] * b[k][j])

Enrollment Number : 23UG032890 Page No: 31


Faculty of Engineering & Technology
303105104 - Computational Thinking for Structured Design-1

}
}
}
printf("The resultant 3*3 matrix c is

\n");
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
printf("%d\t", c[i][j]);
}
printf("\n");
}
getch();
}

Enrollment Number : 23UG032890 Page No: 32


Faculty of Engineering & Technology
303105104 - Computational Thinking for Structured Design-1

Practical 8
1. Write a C program that uses functions to perform the following operation;
1. To insert a sub-string into a given main string from a given position.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[20], str2[20];
int l1, l2, n, i;
clrscr();
puts("Enter the string 1\n");
gets(str1);
l1 = strlen(str1);
puts("Enter the string 2\n");
gets(str2);
l2 = strlen(str2);
printf("Enter the position where the string is to be inserted\n");
scanf("%d", &n);
for(i = n; i < l1; i++)
{
str1[i + l2] = str1[i];
}
for(i = 0; i < l2; i++)
{
str1[n + i] = str2[i];
} str2[l2 + 1] = '\0';
printf("After inserting the string is %s", str1);
getch();
}

Enrollment Number : 23UG032890 Page No: 33


Faculty of Engineering & Technology
303105104 - Computational Thinking for Structured Design-1

Enrollment Number : 23UG032890 Page No: 34


Faculty of Engineering & Technology
303105104 - Computational Thinking for Structured Design-1

2.Write a C program to determine if the given string is a palindrome or not.


#include <stdio.h>
#include <string.h>
int checkpalindrome(char *s)
{
int i,c=0,n;
n=strlen(s);

for(i=0;i<n/2;i++)

{
if(s[i]==s[n-i-1])
c++;
}
if(c==i)
return 1;
else
return 0;
}
int main()
{
char s[1000];
printf("Enter the string: ")
gets(s)

Enrollment Number : 23UG032890 Page No: 35


Faculty of Engineering & Technology
303105104 - Computational Thinking for Structured Design-1

if(checkpalindrome(s))
printf("string is palindrome");
else
printf("string is not

palindrome");
}

Enrollment Number : 23UG032890 Page No: 36


Faculty of Engineering & Technology
303105104 - Computational Thinking for Structured Design-1

Practical 9

1. Write a C program that displays the position or index in the string S where the string
T begins, or -1 if S doesn’t contain T
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char s[30], t[20];
char *found;
clrscr();
puts("Enter the first string: ");
gets(s);
puts("Enter the string to be searched: ");
gets(t);
found = strstr(s, t);
if(found)
{
printf("Second String is found in the First String at %d position.\n", found - s);
}
else
{
printf("-1");
}
getch();
}

Enrollment Number : 23UG032890 Page No: 37


Faculty of Engineering & Technology
303105104 - Computational Thinking for Structured Design-1

2.Write a C program to count the lines, words and characters in a given


text.
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char str[100];
int i = 0, l = 0, f = 1;
clrscr();
puts("Enter any string\n");
gets(str);
for(i = 0; str[i] !='\0'; i++)
{
l = l + 1;
}
printf("The number of characters in the string are %d\n", l);
for(i = 0; i <= l-1; i++)
{
if(str[i] == ' ')
{
f = f + 1;
}
}
printf("The number of words in the string are %d", f);
getch();
}

Enrollment Number : 23UG032890 Page No: 38


Faculty of Engineering & Technology
303105104 - Computational Thinking for Structured Design-1

Practical 10
1.Write a C program to generate Pascal's triangle.

#include <stdio.h>
void printPascal(int n)
{
for (int line = 1; line <= n; line++)
{
for (int space = 1; space <= n - line; space++)
printf(" ");
int coef = 1;
for (int i = 1; i <= line; i++)
{
printf("%4d", coef);
coef = coef * (line - i) / i;
}
printf("\n");
}
}
int main()
{
int n = 5;
printPascal(n);
return 0;
}

Enrollment Number : 23UG032890 Page No: 39


Faculty of Engineering & Technology
303105104 - Computational Thinking for Structured Design-1

2.Write a C program to construct a pyramid of numbers.

#include <stdio.h>

int main() {

int rows;

printf("Number of rows: ");

scanf("%d", &rows);

for (int i = 1; i <= rows; i++) {

for (int j = 1; j <= 2 * (rows - i); j++) {

printf(" ");

for (int k = 1; k < 2 * i; k++) {

printf("%d ", k);

printf("\n");

return 0;

Enrollment Number : 23UG032890 Page No: 40


Faculty of Engineering & Technology
303105104 - Computational Thinking for Structured Design-1

Practical 11
1. Write a C program to read in two numbers, x and n, and then compute the sum of this
geometric progression:
1+x+x^2+x^3+... x^n.
For example: if n is 3 and x is 5, then the program computes 1+5+25+125.Print x, n,
the sum.
Perform error checking. For example, the formula does not make sense for negative
exponents 􀍶􀍶if n is less than 0. Have
your program print an error - message if n<0, then go back and read in the next pair
of numbers without computing the sum.
Are any values of x also illegal? If so, test for them too.
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
int n, x, i, sum = 0;
clrscr();
printf("Enter the limit\n");
scanf("%d", &n);
printf("Enter the value of x\n");
scanf("%d", &x);
if(x < 0 || n < 0)
{
printf("illegal value");
}
else
{
for(i = 0; i <= n; i++)
sum=sum + pow(x, i);
}
printf("sum=%d", sum);
getch();
}

Enrollment Number : 23UG032890 Page No: 41


Faculty of Engineering & Technology
303105104 - Computational Thinking for Structured Design-1

Practical 12
2's complement of a number is obtained by scanning it from right to left and complementing
all the bits after the first appearance of a 1. Thus 2’s complement of 11100 is 00100. Write a
C program to find the 2's complement of a binary number.
#include <stdio.h>
#include <string.h>
#include <conio.h>
void main()
{
char a[20];
int i, carry, l;
clrscr();
printf("Enter the binary number \n");
scanf("%s", &a);
l = strlen(a);
for(i = 0; i < l; i++)
{
if(a[i] == '0')
{
a[i] = '1';
}
else
{
a[i] = '0';
}
}
printf("The 1's compliment of the binary number is %s \n", a);
i = strlen(a) - 1;
while(i >= 0)
{
if(a[i] == '0')
{

Enrollment Number : 23UG032890 Page No: 42


Faculty of Engineering & Technology
303105104 - Computational Thinking for Structured Design-1

a[i] = '1';
carry = 0;
break;
}
else
{
a[i] = '0';
carry = 1;
i = i - 1;
}
}
printf("The 2's compliment of the binary number is ");
if(carry == 1)
{
printf("1");
}
printf("%s", a);
getch();
}

Enrollment Number : 23UG032890 Page No: 43


Faculty of Engineering & Technology
303105104 - Computational Thinking for Structured Design-1

Write a C program to convert a Roman numeral to its decimal Equivalent.


#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char rom[30];
int a[30], l, i, k, dec;
clrscr();
printf("Enter the roman number\n");
scanf("%s", &rom);
l =strlen(rom);
for(i = 0; i < l; i++)
{
switch (rom[i])
{
case 'I': a[i] = 1;
break;
case 'V': a[i] = 5;
break;
case 'X': a[i] = 10;
break;
case 'L': a[i] = 50;
break;
case 'C': a[i] = 100;
break;
case 'D': dec = dec + 500;
break;
case 'M': a[i] = 1000;
break;

Enrollment Number : 23UG032890 Page No: 44


Faculty of Engineering & Technology
303105104 - Computational Thinking for Structured Design-1

default : printf("Invalid choice");


break;
}
}
k = a[l - 1];
for(i = l - 1; i > 0; i--)
{
if(a[i] > a[i - 1])
{
k = k - a[i - 1];
}
if(a[i] <= a[i - 1])
{
k = k + a[i - 1];
}
}
printf("decimal equivalent is %d", k);
getch();
}

Enrollment Number : 23UG032890 Page No: 45


Faculty of Engineering & Technology
303105104 - Computational Thinking for Structured Design-1

Practical 13

Write a c program on Given an unsorted array arr[] of size N. Rotate the array to the left
(counter-clockwise direction) by D steps, where D is a positive integer.
#include <stdio.h>
int main()
{

int arr[] = {1, 2, 3, 4, 5};

int length = sizeof(arr)/sizeof(arr[0]);

int n = 3;

printf("Original array: \n");


for (int i = 0; i < length; i++) {
printf("%d ", arr[i]);
}

for(int i = 0; i < n; i++){


int j, first;

first = arr[0];
for(j = 0; j < length-1; j++){

arr[j] = arr[j+1];
}

arr[j] = first;
}

Enrollment Number : 23UG032890 Page No: 46


Faculty of Engineering & Technology
303105104 - Computational Thinking for Structured Design-1

printf("\n");

printf("Array after left rotation: \n");


for(int i = 0; i < length; i++){
printf("%d ", arr[i]);
}
return 0;
}

Enrollment Number : 23UG032890 Page No: 47


Faculty of Engineering & Technology
303105104 - Computational Thinking for Structured Design-1

Write a c Program on given two sorted arrays arr1 and arr2 of size N and M respectively
and an element K. The task is to
find the element that would be at the k’th position of the final sorted array.Explanation:
Input :
Array 1 - 1 4 2 3 5
Array 2 - 7 8 6
k=5
Output : 5
Because The final sorted array would be -1, 2, 3, 4, 5, 6, 7, 8, The 5th element of this array

is
6.
#include<stdio.h>
int kth(int arr1[], int arr2[], int m, int n, int k)
{
int sorted1[m + n];
int i = 0, j = 0, d = 0;
while (i < m && j < n)
{
if (arr1[i] < arr2[j])
sorted1[d++] = arr1[i++];
else

sorted1[d++] = arr2[j++];
}
while (i < m)
sorted1[d++] = arr1[i++];

while (j < n)
sorted1[d++] = arr2[j++];
return sorted1[k - 1];
}

int main()
{

Enrollment Number : 23UG032890 Page No: 48


Faculty of Engineering & Technology
303105104 - Computational Thinking for Structured Design-1

int arr1[5] = {2, 3, 6, 7, 9};


int arr2[4] = {1, 4, 8, 10};
int k = 5;
cout << kth(arr1, arr2, 5, 4, k);
return 0;
}

Enrollment Number : 23UG032890 Page No: 49


Faculty of Engineering & Technology
303105104 - Computational Thinking for Structured Design-1

Practical 14

1. Write a c program to take multiline string input and print individual string length .
#include <stdio.h>
#include <string.h>

int main()
{

printf("Enter the String: ");

char ss[] = "geeks";

printf("%s\n", ss);

printf("Length of Str is %ld", sizeof(ss) - 1);

return 0;
}

Enrollment Number : 23UG032890 Page No: 50


Faculty of Engineering & Technology
303105104 - Computational Thinking for Structured Design-1

Write a c program to reverse the individual word of a given string .


Explanation:input :Welcome To Bytexl output: emocleW oT lxetyB.

#include <stdio.h>
void reverse(char* begin, char* end)
{
char temp;
while (begin < end) {
temp = *begin;
*begin++ = *end;
*end-- = temp;
}
}

void reverseWords(char* s)
{
char* word_begin = s;

char* temp = s;

while (*temp) {
temp++;
if (*temp == '\0') {
reverse(word_begin, temp - 1);
}
else if (*temp == ' ') {
reverse(word_begin, temp - 1);
word_begin = temp + 1;

Enrollment Number : 23UG032890 Page No: 51


Faculty of Engineering & Technology
303105104 – Computational Thinking for Structured Design-1

}
}

reverse(s, temp - 1);


}

int main()
{
char s[] = "i like this program very much";
char* temp = s;

reverseWords(s);
printf("%s", s);
return 0;
}

Enr ollment Number : 23UG032890 Page No: 52

You might also like