What will be the output of the following program? | Set-2

Share this Content

1. Write a program to allocate memory to represent a 2d array, where the size of the array will be provided by the user and we’ve to put some data and display those data in a matrix form

Here we are basically creating code for showing a Matrix

Input 1:


#include <stdio.h>
#include <stdlib.h>

int main()
{

int **p;
int i, j, r, c;

// Get the number of elements for the array
printf("Enter the number of rows: ");
scanf("%d", &r);
printf("Enter the number of column: ");
scanf("%d", &c);

// Dynamically allocate memory using malloc()
p = (int **)malloc(sizeof(int *) * r);
for (i = 0; i < r; i++)
{
*(p + i) = (int *)malloc(sizeof(int) * c);
}
printf("Enter elements of the array:n");
for (i = 0; i < r; ++i)
{
for (j = 0; j < c; ++j)
{
scanf("%d", &p[i][j]);
p[i][j] = *(*(p + i) + j);
}
}

// printing the result
printf("The answer is:n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
{
printf("%d ", p[i][j]);
if (j == c - 1)
{
printf("n");
}
}

return 0;
}

Output 1:

write a program in C to insert 10 elements in array and display it

Subscribe to Tech Break

Input 2:


#include <stdio.h>
int main()
{
int n, arr[20];
printf("Enter the size ");
scanf("%d", &n);

for (int i = 0; i < n; i++)
{
printf("Enter the element ");
scanf("%d", &arr[i]);
}

for (int j = 0; j < n; j++)
{
printf("The element of %d position is: %d n ",j, arr[j]);
}

return 0;
}

Output 2:

3. Write a program to exchange character from small to capital and vice versa.

Input


#include <stdio.h>
int main()
{
char a;
printf("Enter the character here-");
scanf("%c",&a);
if (a>=97 && a<=122)
a=a-32;
else if(a>=65 && a<=90)
a=a+32;
printf("The complementary alphabet is= %c",a);
return 0;
}

Output

Write a code to concatinate two strings.

Input


#include <stdio.h>
int main()
{

char str1[100], str2[100];
char str3[100];
int i = 0, j = 0;
printf("nEnter first string: ");
scanf("%s", str1);
printf("nEnter second string: ");
scanf("%s", str2);
while (str1[i] != '')
{

str3[j] = str1[i];
i++;
j++;
}

i = 0;
while (str2[i] != '')

{
str3[j] = str2[i];
i++;
j++;
}

str3[j] = '';
printf("nConcatenated string: %s", str3);
return 0;
}

Output

Share this Content
Snehasish Konger
Snehasish Konger

Snehasish Konger is the founder of Scientyfic World. Besides that, he is doing blogging for the past 4 years and has written 400+ blogs on several platforms. He is also a front-end developer and a sketch artist.

Articles: 192

Newsletter Updates

Join our email-newsletter to get more insights

Leave a Reply

Your email address will not be published. Required fields are marked *