Question 1: Write a program to
print the following pattern:
a) 1 b)
1
1 2 2 2
1 2 3 3 3 3
1 2 3 4 4 4 4 4
1 2 3 4 5 5 5 5 5 5
Ans
a)
#include <stdio.h>
#include <conio.h>
void
main ( )
{
int i, j;
clrscr ( );
for ( i =1; i <= 5; i++ )
{
for ( j = 1; j <= i; j++ )
{
printf("%d",j);
}
printf("\n");
}
getch();
}
Ans b)
#include <stdio.h>
#include <conio.h>
void
main ( )
{
int i, j;
clrscr ( );
for ( i = 1; i <= 5; i++ )
{
for ( j = 1; j <= i; j++ )
{
printf("%d",i);
}
printf("\n");
}
getch();
}
Question 2: Declare two arrays A and B, find
(i) A intersection B and (ii) A union B.
Ans(i)
#include <stdio.h>
#include <conio.h>
void
main ( )
{
int arr[10], brr[10], inter[10], i, j, k = 0, f;
clrscr ( );
printf ("\n Enter numbers in 1st
array..:");
for ( i = 0; i < 10; i++ )
{
printf ("\n Enter a
number..:");
scanf ("%d", &arr[i]);
}
printf ("\n Enter numbers in 2nd
array..:");
for ( i = 0; i < 10; i++ )
{
printf ("\n Enter a
number..:");
scanf ("%d", &brr[i]);
}
for ( j = 0; j < 10; j++ )
{
f = 0;
for ( i = 0; i < 10; i++ )
{
if ( brr[j] == arr[i] )
{
f = 1;
break;
}
}
if ( f == 1 )
{
inter[k++] = brr[j];
}
}
printf("\n Intersection
array..:");
for ( i = 0; i < k; i++ )
{
printf ("%d,", inter[i]);
}
getch ( );
}
Ans (ii)
#include <stdio.h>
#include <conio.h>
void
main ( )
{
int arr[5], brr[5], uni[10], i, j, k = 0, f;
clrscr ( );
printf ("\n Enter numbers in 1st
array..:");
for ( i = 0; i < 5; i++ )
{
printf ("\n Enter a
number..:");
scanf ("%d",&arr[i]);
uni[k++]=arr[i];
}
printf("\n Enter numbers in 2nd
array..:");
for ( i = 0; i < 5; i++ )
{
printf ("\n
Enter a number..:");
scanf ("%d",&brr[i]);
}
for ( j = 0; j < 5; j++ )
{
f = 0;
for ( i = 0; i < 5; i++ )
{
if ( brr[j] == arr[i] )
{
f = 1;
break;
}
}
if ( f == 0 )
{
uni[k++] = brr[j];
}
}
printf ("\n Union
array..:");
for ( i = 0; i < k; i++ )
{
printf ("%d,",uni[i]);
}
getch ( );
}
Click here to contact us
Question 3: Write a program to crypt its
input according to a specified transformation scheme. The transformation scheme
will consist of two strings: a string of characters and then a string of
replacement characters. The idea is that your program replaces every instance
of the ith character in the initial string with the (i+2) character (of English
alphabets) in the replacement string. When no substitution is defined for a
character, the program just passes it through to the output unchanged. Blank
spaces and the other symbols remains the same. The program should inform the
user of any errors in the transformation scheme. Your program should display
the phrase before and after the substitutions have been made.
Example:
Original String: I
know C programming.
String after the transformation: K mpqy E rtqitcookpi.
Ans:
#include <stdio.h>
#include <conio.h>
void
main ( )
{
int i;
char str[40], rstr[40];
clrscr ( );
printf ("\n Enter the
string..:");
gets ( str );
for ( i = 0; str[i]! = '\0'; i++)
{
if (( str[i] >= 'a' && str[i] <= 'x' ) || ( str[i] >= 'A' && str[i] <= 'X' ))
{
rstr[i] = str[i] + 2;
}
else
{
rstr[i] = str[i];
}
}
printf ("\n Original
string.:%s",str);
printf ("\n Replaced
string.:%s",rstr);
getch ( );
}
Thank u very much sir for posting here solved assignment mcs-031.
ReplyDeletethanks a lot sir.