You are on page 1of 21

KALYANI GOVERNMENT ENGINEERING COLLEGE

(AFFILIATED TO MAKAUT)

PCA-2 ASSIGNMENT

Name Tanmay Ghosh


Department Electronics and Communication Engineering
Year 1st
Roll number 10200322079
Subject Programming for Problem-solving
Date 02/05/2023
Question 1: Find the factorials of 1 to 10 using do while loop.

Input:
#include<stdio.h>
void main()
{
int n=1, fact=1;
do
{
for(int i=1;i<=n;i++)
{
fact=fact*i;

}
printf("Factorial of %d is %d .\n",n,fact);
n++;
fact=1;
}

while(n<=10);
}

Output:

Question 2: Write a program which accepts a number n and prints all


prime factors of n.

Input:
#include<stdio.h>
int main()
{
int n, i, j, p=0, c=0;
printf("Enter any number:\n");
scanf("%d", &n);
printf("\nThe prime factors of %d are:\n", n);
for(i=2; i<=n; i++)
{
if(n%i==0)
{
for(j=1;j<=i; j++)
{
if(i%j==0)
{
c++;
}
}
if(c==2)
{
printf("%d\n", i);
}
c=0;
}
}
}

Output:

Question 3: Write a C program which accepts 100 integers and


displays the count of positives, negatives and zeroes entered.
Input:
#include<stdio.h>
int main()
{
int a[100];
int i, pos=0, neg=0, zer=0;
printf("To input 100 integer numbers\n");
printf("____________________________\n");
for(i=0; i<100; i++)
{
printf("\nEnter integer number %d:", i+1);
scanf("%d", &a[i]);
if(a[i]>0)
{
pos++;
}
else if(a[i]<0)
{
neg++;
}
else
{
zer++;
}

}
printf("\nThe number of positive integers is: %d", pos);
printf("\nThe number of negative integers is: %d", neg);
printf("\nThe number of zeroes is: %d", zer);
}

Output:
Question 4: Write a menu driven program for accepting for accepting
values in two square matrices of 3x3 dimension and generate their sum,
difference and product.

Input:
#include<stdio.h>
int main()
{
int a[50][50], b[50][50], r[50][50], ch, i, j, k, r1, c1, r2, c2, sum=0;
printf("Menu for matrix addition, subtraction and multiplication");
printf("\n__________________________________________________________\n");
printf("Enter the number of rows and columns of first matrix:\n");
scanf("%d%d", &r1, &c1);
printf("Enter the number of rows and columns of second matrix:\n");
scanf("%d%d", &r2, &c2);
printf("\nEnter the elements of first matrix:");
for(i=0; i<r1; i++)
{
for(j=0; j<c1; j++)
{
printf("\na[%d][%d]:", i, j);
scanf("%d", &a[i][j]);
}
}
printf("\nEnter the elements of second matrix:");
for(i=0; i<r2; i++)
{
for(j=0; j<c2; j++)
{
printf("\nb[%d][%d]:", i, j);
scanf("%d", &b[i][j]);
}
}
printf("\nPress:\n");
printf("1 for addition, 2 for subtraction and 3 for multiplication.\n");
printf("\nEnter your choice: \n");
scanf("%d", &ch);
switch(ch)
{
case 1:
if(r1!=r2 || c1!=c2)
{
printf("\nThe addition of these two matrices is not possible as the
matrices to be added must have the same size.");
}
else
{
for(i=0; i<r1; i++)
{
for(j=0; j<c1; j++)
{
r[i][j]=0;
r[i][j]= a[i][j]+b[i][j];
}
}
printf("\nThe first matrix is:\n");
for(i=0; i<r1; i++)
{
for(j=0; j<c1; j++)
{
printf("%3d", a[i][j]);
}
printf("\n");
}
printf("\nThe second matrix is:\n");
for(i=0; i<r2; i++)
{
for(j=0; j<c2; j++)
{
printf("%3d", b[i][j]);
}
printf("\n");
}
printf("\nThe matrix as a result of their addition is:\n");
for(i=0; i<r1; i++)
{
for(j=0; j<c1; j++)
{
printf("%3d", r[i][j]);
}
printf("\n");
}
}
break;
case 2:
if(r1!=r2 || c1!=c2)
{
printf("\nThe subtraction of these two matrices is not possible as the
matrices to be subtracted must have the same size.");
}
else
{
for(i=0; i<r1; i++)
{
for(j=0; j<c1; j++)
{
r[i][j]=0;
r[i][j]= a[i][j]-b[i][j];
}
}
printf("\nThe first matrix is:\n");
for(i=0; i<r1; i++)
{
for(j=0; j<c1; j++)
{
printf("%3d", a[i][j]);
}
printf("\n");
}
printf("\nThe second matrix is:\n");
for(i=0; i<r2; i++)
{
for(j=0; j<c2; j++)
{
printf("%3d", b[i][j]);
}
printf("\n");
}
printf("\nThe matrix as a result of their subtraction is:\n");
for(i=0; i<r1; i++)
{
for(j=0; j<c1; j++)
{
printf("%3d", r[i][j]);
}
printf("\n");
}
}
break;
case 3:
if(c1!=r2)
{
printf("\nThe multiplication of these matrices is not possible.");
printf("\nIn matrix multiplication, the number of columns of first matrix
and number of rows of second matrix must be same.");
}
else
{
for(i=0; i<r1; i++)
{
for(j=0; j<c2; j++)
{
r[i][j]=0;
for(k=0; k<r2; k++)
{
r[i][j]= r[i][j]+ a[i][k]*b[k][j];
}

}
}
printf("\nThe first matrix is: \n");
for(i=0; i<r1; i++)
{
for(j=0; j<c1; j++)
{
printf("%3d", a[i][j]);
}
printf("\n");
}
printf("\nThe second matrix is: \n");
for(i=0; i<r2; i++)
{
for(j=0; j<c2; j++)
{
printf("%3d", b[i][j]);
}
printf("\n");
}
printf("\nThe matrix as a result of their multiplication is: \n");
for(i=0; i<r1; i++)
{
for(j=0; j<c2; j++)
{
printf("%3d", r[i][j]);
}
printf("\n");
}
}
break;
default:
printf("\nInvalid choice.");
}
}
Output:
Example 1:

Example 2:

Example 3:
Question 5 (I): Write a program in C to reverse the contents of the
elements of an integer array.

Input:
#include<stdio.h>
int main()
{
int a[100], n, i;
printf("Enter the number of elements of the array:\n");
scanf("%d", &n);
printf("\nEnter the elements of the array:");
for(i=0;i<n;i++)
{
printf("\nElement number %d:", i+1);
scanf("%d", &a[i]);
}
printf("\nThe entered array is:");
for(i=0; i<n; i++)
{
printf("%3d", a[i]);
}
printf("\n");
printf("\nThe entered array in reversed order is:");
for(i=n-1; i>=0; i--)
{
printf("%3d", a[i]);
}
printf("\n");
}

Output:

Question 5 (II): Write a program to find even and odd no consecutively


from array elements and print their average.

Input:
#include<stdio.h>
int main()
{
int a[100], n, i, odd=0, ev=0;
float os=0, es=0;
printf("Enter the number of array elements:");
scanf("%d", &n);
printf("\nEnter integer numbers as array elements:");
for(i=0; i<n; i++)
{
printf("\nElement number %d:", i+1);
scanf("%d", &a[i]);
if(a[i]%2==0)
{
printf("\n%d is even.", a[i]);
ev++;
es= es+ a[i];
}
else
{
printf("\n%d is odd.", a[i]);
odd++;
os= os+ a[i];
}
}
printf("\nThe average of even numbers is: %f", (float)os/odd);
printf("\nThe average of odd numbers is: %f", (float)es/ev);
}

Output:

Question 6: Write a C program which accepts a full name from user and
prints the initials. E.g.: SRT for Sachin Ramesh Tendulkar.

Input:
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
int i, j, l=0;
printf("Enter your name:\n");
gets(str);
l= strlen(str);
for(i=0; i<l; i++)
{
if(str[i]>=65 && str[i]<=90)
str[i]= str[i] +32;
}
printf("\nThe initials of the entered name are:\n");
putchar(str[0]-32);
for(i=0; i<l; i++)
{
if(str[i]==' ')
{
printf("%c", str[i+1]-32);
}
}
}

Output:
Example 1:

Example 2:

Question 7: Write a program in C that will implement the following


functions
a. Count the total number of words in a string.
b. Find number of occurrences of a given word C.
c. Print individual characters of string in reverse order.
d. Count total number of vowels and consonants in a string.
e. Find the most frequent character in a string.
f. Count the number of occurrences of any two vowels in succession
Input:
#include<stdio.h>
#include<string.h>
int wrdcount(char *s);
int wrdocc(char *s);
void charev(char *s);
void vowcon(char *s);
void charfreq(char *s);
int vow(char *s);

void main()
{
char s[100];
int w,c,cnt;
printf("Enter any string:\n");
gets(s);
w= wrdcount(s);
printf("\nTotal number of words in the string is:%d\n", w);
printf("\nThe characters in reverse order are: \n");
charev(s);
printf("\n\nThe total number of vowels and consonants respectively are:");
vowcon(s);
printf("\n\nThe most frequent character in the string is:");
charfreq(s);
cnt= vow(s);
printf("\n\nThe number of occurences of two vowels in succession is: %d", cnt);
c= wrdocc(s);
printf("\nTotal number of times the given word appears in the entered string is:%d\n",
c);
}
int wrdcount(char *s)
{
int w=0, l=0, i;
l= strlen(s);

for(i=0; i<=l; i++)


{
if(s[i]== ' ' || s[i]== '\0')
{
w++;
}
}
return w;
}
void charev(char *s)
{
int l=0, i;
l= strlen(s);
for(i=l-1; i>=0; i--)
{
putchar(s[i]);
}
}
int wrdocc(char *s)
{
char w[50];
int l1=0, l2=0, i, j, c=0, f=1;
printf("\n\nEnter the word whose number of occurences is to be found:\n");
gets(w);
l1=strlen(s);
l2=strlen(w);
for(i=0;i<l2;i++)
{
if(w[i]>=65 && w[i]<=90)
{
w[i]= w[i]+32;
}
}
for(i=0; i<=l1-l2; i++)
{
f=1;
for(j=0; j<l2; j++)
{
if(s[j+i]!= w[j])
{
f=0;
}
if(f==0)
{
break;
}

}
if(f==1)
{
c++;
}

}
return c;
}
void vowcon(char *s)
{
int v=0, con=0, l=0, i;
l= strlen(s);
for(i=0;i<l;i++)
{
if(s[i]>=65 && s[i]<=90)
{
s[i]= s[i]+32;
}
}
for(i=0; i<l; i++)
{
if(s[i]>= 'a' && s[i]<= 'z')
{
if(s[i]== 'a' || s[i]== 'e' || s[i]== 'i' || s[i]== 'o' || s[i]== 'u')
{
v++;
}
else
{
con++;
}
}
}
printf("%d and %d", v, con);
}
void charfreq(char *s)
{
int l=0, fr=0, m=0, i, j;
char x;
l= strlen(s);
for(i=0; i<l; i++)
{
for(j=0; j<l; j++)
{
if(s[i]== s[j] && s[i]!= ' ')
{
fr++;
}
}
if(fr>m)
{
m=fr;
x= s[i];
}
fr=0;
}
putchar(x);
}
int vow(char *s)
{
int l=0, cnt=0, i;
l=strlen(s);
for(i=0; i<l; i++)
{
if((s[i]== 'a' || s[i]== 'e' || s[i]== 'i' || s[i]== 'o' || s[i]== 'u') &&
(s[i+1]== 'a' || s[i+1]== 'e' || s[i+1]== 'i' || s[i+1]== 'o' || s[i+1]== 'u'))
{
cnt++;
}
}
return cnt;
}

Output:
Question 8: Generate the pattern
a)
Input:
#include<stdio.h>
void main()
{
int i, j, k=1;
for(i=0; i< 4; i++)
{
for(j=0; j<=i; j++)
{
printf("%2d ", k);
++k;
}
printf("\n");
}
}

Output:
b)
Input:
#include<stdio.h>
void main()
{
int n, i, j, k, a=1;
printf("Enter the number of rows:\n");
scanf("%d", &n);
for(i=0; i<n; i++)
{
for(j=1; j<=n-i; j++)
{
printf(" ");
}
for(k=0; k<=i; k++)
{
if(i==0 || k== 0)
{
a=1;
}
else
{
a= a*(i-k+1)/k;
}
printf("%3d", a);
}
printf("\n");
}
}

Output:

c)
Input:
#include<stdio.h>
int main()
{
int n, i, j, k;
printf("Enter the number of rows: \n");
scanf("%d", &n);
for(i=n; i>=1; i--)
{
for(j=0; j<n-i; j++)
{
printf(" ");
}
for(k=i; k<= 2*i -1; k++)
{
printf("*");
}
for(k=0; k<i-1; k++)
{
printf("*");
}
printf("\n");
}
return 0;
}

Output:

d)
Input:
#include<stdio.h>
void main()
{

int n, p = 1, a[100][100], j, m, k, r;
printf("Enter the number of rows: ");
scanf("%d", &r);
for (j = 1; j <= r; j++)
{
m = 0;
n = j;
for (k = 1; k <= j; k++)
{
a[m++][--n] = p++;
}

for (j = 1; j <= r-1; j++)


{
m = j;
n = r-1;
for (k = 1; k<= r-j; k++)
{
a[m++][n--] = p++;
}
}

for (j = 0; j <= r-1; j++)


{
for (k = 0; k <= r-1; k++)
{
printf("%d ", a[j][k]);
}
printf("\n");
}
}

Output:

You might also like