C-Programs List

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

C -Programming

1. Prime or not
Step 1: Start
Step 2: Initialize variables n,flag=1,i
Step 3: Read n from user
Step 4: If n=1
Display "n is not a prime number"
Goto step 7
Step 5: Repeat the steps until i<=[(n/2)]
5.1 If n % i equals to 0,
Set flag=0
Goto step 6
5.2 i=i+1
Step 6: If flag==0,
Display n is not prime number"
Else
Display n is a prime number"
Step 7: Stop

Program:
#include <stdio.h>
int main()
{
int n, i, flag = 1;
printf("Enter a positive integer: ");
scanf("%d", &n);

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


{
if (n % i == 0)
{
flag = 0;
break;
}
}

if (n == 1) {
printf("%d is neither prime nor composite.",n);
}
else {
if (flag == 1)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
}

return 0;
}
#include <stdio.h>
int main()
{
int a[10][10], transpose[10][10], r, c, i, j;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
printf("\nEnter matrix elements:\n");
for (i = 0; i < r;i++)
for (j = 0; j < c;j++)
{
printf("Enter element a%d%d: ", i, j);
scanf("%d", &a[i][j]);
}
printf("\nEntered matrix: \n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}

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


for (j = 0; j < c;j++) {
transpose[j][i] = a[i][j];
}

printf("\nTranspose of the matrix:\n");


for (i = 0; i < c;i++)
{
for (j = 0; j < r; j++)
{
printf("%d ", transpose[i][j]);
}
printf("\n");
}
return 0;
}

Algorithm:
Step 1:Start

Step 2:Declare all the necessary variables a[10][10], transpose[10][10], r, c, i, j

Step 3: Enter the order of matrix r, c

Step 4: Enter the elements of matrix row-wise using loop

Step 5: Display the entered matrix in standard format

Step 6: Assign number of rows with number of column

Step 7: Swap (i, j)th element with (j, i)th


Step 8: Store the new elements as element of transposed matrix

Step 9: Print the elements of transpose matrix in format using loop

Step 10: Stop

FlowChart

2. sum of series

#include<stdio.h>

#include<math.h>

int main()

int num,i;

float sum=0,m,n;

printf("Enter limit: ");

scanf("%d",&num);

for(i=1;i<=num;i++)

m=(float)1/i;

sum=sum+pow(m,i);

printf("\n\nSum of Series=%f\n\n\n",sum);

return 0;

You might also like