0% found this document useful (0 votes)
8 views

C Programming 26 sep (1)

C programming basic programs

Uploaded by

jonadany2390
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

C Programming 26 sep (1)

C programming basic programs

Uploaded by

jonadany2390
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Program 1

#include <stdio.h>

void main()

int i,N,sum=0;

printf("Enter no of places in the array: ");

scanf("%d",&N);

printf("Enter %d elements in the array: ",N);

int A[N];

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

scanf("%d",&A[i]);

sum=sum+A[i];

printf("%d is the sum of the elements of the array",sum);

}
Program 2

#include <stdio.h>

int main()

int N;

float mean, median;

printf("Enter the number of elements in the array: ");

scanf("%d", &N);

int A[N];

printf("Enter %d elements:\n", N);

for (int i = 0; i < N; i++) {

scanf("%d", &A[i]);

int sum = 0;

for (int i = 0; i < N; i++) {

sum += A[i];

mean = sum / N;
for (int i = 0; i < N - 1; i++) {

for (int j = 0; j < N - i - 1; j++) {

if (A[j] > A[j + 1]) {

// Swap

int temp = A[j];

A[j] = A[j + 1];

A[j + 1] = temp;

median;

if (N % 2 == 0) {

median = (A[N/2 - 1] + A[N/2]) / 2.0;

} else {

median = A[N/2];

printf("Mean: %.2f\n", mean);

printf("Median: %.2f\n", median);

return 0;

}
Program 3

#include <stdio.h>

int main()

int N;

printf("Enter the number of elements in the array: ");

scanf("%d", &N);

int A[N];

printf("Enter %d elements:\n", N);

for (int i = 0; i < N; i++) {

scanf("%d", &A[i]);

printf("Prime numbers in the array A:\n");

for (int i = 0; i < N; i++) {

int num = A[i];

if (num <= 1) {

continue;

for (int j = 2; j <= num / 2; j++) {

if (num % j == 0) {

break;
}

if (j == num / 2) {

printf("%d ", num);

printf("\n");

return 0;

}
Program 4

#include <stdio.h>

int main() {

int N, div;

printf("Enter the number of elements in the array: ");

scanf("%d", &N);

int A[N];

printf("Enter %d elements:\n", N);

for (int i = 0; i < N; i++) {

scanf("%d", &A[i]);

printf("Enter the divisor: ");

scanf("%d", &div);

int B[N];

int x = 0;

for (int i = 0; i < N; i++) {

if (A[i] % div == 0) {

B[x] = A[i];

x++;

}
}

printf("Elements divisible by %d:\n", div);

for (int i = 0; i < x; i++) {

printf("%d ", B[i]);

printf("\n");

return 0;

}
Program 5

#include <stdio.h>

int main() {

int N;

printf("Enter the number of elements in the array: ");

scanf("%d", &N);

int A[N];

printf("Enter %d elements:\n", N);

for (int i = 0; i < N; i++) {

scanf("%d", &A[i]);

for (int i = 0; i < N - 1; i++) {

for (int j = 0; j < N - i - 1; j++) {

if (A[j] > A[j + 1]) {

int temp = A[j];

A[j] = A[j + 1];

A[j + 1] = temp;

if (N >= 2) {
printf("The two smallest elements are: %d and %d\n", A[0], A[1]);

} else if (N == 1) {

printf("Only one element in the array: %d\n", A[0]);

} else {

printf("The array is empty.\n");

return 0;

}
Program 6

#include <stdio.h>

int main() {

int N;

printf("Enter the number of elements in the array: ");

scanf("%d", &N);

int A[N];

printf("Enter %d elements:\n", N);

for (int i = 0; i < N; i++) {

scanf("%d", &A[i]);

for (int i = 0; i < N; i++) {

if (A[i] % 2 == 0) {

A[i] = 0;

} else {

A[i] = 1;

printf("Modified array:\n");

for (int i = 0; i < N; i++) {

printf("%d ", A[i]);


}

printf("\n");

return 0;

}
Program 7

#include <stdio.h>

int main() {

int x, y;

printf("Enter the number of elements in the first array: ");

scanf("%d", &x);

int A[x];

printf("Enter %d elements for the first array:\n", x);

for (int i = 0; i < x; i++) {

scanf("%d", &A[i]);

printf("Enter the number of elements in the second array: ");

scanf("%d", &y);

int B[y];

printf("Enter %d elements for the second array:\n", y);

for (int i = 0; i < y; i++) {

scanf("%d", &B[i]);

int COMBINED[x + y];

for (int i = 0; i < x; i++) {


COMBINED[i] = A[i];

for (int i = 0; i < y; i++) {

COMBINED[x + i] = B[i];

printf("Merged array:\n");

for (int i = 0; i < x + y; i++) {

printf("%d ", COMBINED[i]);

printf("\n");

return 0;

}
Program 8

#include <stdio.h>

int main() {

int N;

printf("Enter the number of elements in the array: ");

scanf("%d", &N);

int A[N];

int X[N];

printf("Enter %d elements in the array:\n", N);

for (int i = 0; i < N; i++) {

scanf("%d", &A[i]);

X[i] = 1;

for (int i = 0; i < N; i++) {

for (int j = i + 1; j < N; j++) {

if (A[i] == A[j]) {

X[i]++;

X[j] = -1;

printf("Non-repeated elements:\n");
for (int i = 0; i < N; i++) {

if (X[i] != -1) {

printf("%d ", A[i]);

printf("\n");

return 0;

You might also like