Professional Documents
Culture Documents
Pranathi CTSD
Pranathi CTSD
Practical 7
}
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();
}
}
}
}
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();
}
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();
}
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)
if(checkpalindrome(s))
printf("string is palindrome");
else
printf("string is not
palindrome");
}
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();
}
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;
}
#include <stdio.h>
int main() {
int rows;
scanf("%d", &rows);
printf(" ");
printf("\n");
return 0;
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();
}
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')
{
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();
}
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 n = 3;
first = arr[0];
for(j = 0; j < length-1; j++){
arr[j] = arr[j+1];
}
arr[j] = first;
}
printf("\n");
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()
{
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("%s\n", ss);
return 0;
}
#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;
}
}
int main()
{
char s[] = "i like this program very much";
char* temp = s;
reverseWords(s);
printf("%s", s);
return 0;
}