0% found this document useful (0 votes)
23 views3 pages

C Code Collection

The document contains a C program that demonstrates various algorithms and patterns, including a diamond pattern with numbers, binary search, reversing a number, inserting an element in an array, matrix multiplication, and generating prime numbers. It also includes three different patterns for printing numbers in a structured format. The code is commented out for some sections, indicating that they are not currently active.

Uploaded by

Aditya kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views3 pages

C Code Collection

The document contains a C program that demonstrates various algorithms and patterns, including a diamond pattern with numbers, binary search, reversing a number, inserting an element in an array, matrix multiplication, and generating prime numbers. It also includes three different patterns for printing numbers in a structured format. The code is commented out for some sections, indicating that they are not currently active.

Uploaded by

Aditya kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

// Online C compiler to run C program online

#include <stdio.h>

int main() {
// Diamond pattern with numbers
// int n=6;
// for(int i=1; i<=n;i++){
// for(int j=1; j<=n-i; j++){
// printf(" ");
// }
// for(int k=1;k<=i;k++){
// printf("%d",k);
// }
// for (int m=i-1;m>0;m--){
// printf("%d",m);
// }
// printf("\n");
// }

// Binary search
// int n,mid,arr[50],t,f=0,s=0,e;
// printf("Give the size");
// scanf("%d",&n);
// printf("Enter the the elements in the array");
// for(int i=0;i<n;i++){
// scanf("%d",&arr[i]);
// }
// printf("Enter the target value");
// scanf("%d",&t);
// e=n-1;
// while(s<=e){
// mid=(s+e)/2;
// if(t==arr[mid]){
// f=1;
// break;
// }
// if(t>arr[mid]){
// s=mid+1;
// }
// if(t<arr[mid]){
// e=mid-1;
// }
// }
// if(f){
// printf("Target found at %d ",mid);
// }
// else{
// printf("Target not found");
// }

// Reverse a number
// int n,sum=0;
// printf("Enter the number you want to reverse");
// scanf("%d",&n);
// while(n!=0){
// int r=n%10;
// sum=(sum*10)+r;
// n=n/10;
// }
// printf("The reverse num is %d",sum);

// Insert element in array at position


// int arr[5]={1,2,3,4,5};
// int size=5;
// int pos=4;
// int val=16;
// for(int i=size;i>pos;i--){
// arr[i]=arr[i-1];
// }
// arr[pos]=val;
// size++;
// for(int i=0;i<size;i++){
// printf("%d,",arr[i]);
// }

// Matrix multiplication
// int arr[2][2]={{1,2},{3,4}};
// int brr[2][2]={{5,6},{7,8}};
// int crr[2][2];
// int size=2;
// for(int i=0;i<size;i++){
// for(int j=0;j<size;j++){
// crr[i][j]=0;
// for(int k=0;k<size;k++){
// crr[i][j]+=arr[i][k]*brr[k][j];
// }
// }
// }
// for(int i=0;i<size;i++){
// for(int j=0; j<size; j++){
// printf("%d \t", crr[i][j]);
// }
// printf("\n");
// }

// Prime numbers from 2 to N


// int N, i, j, isPrime;
// printf("Enter the value of N: ");
// scanf("%d", &N);
// printf("Prime numbers from 2 to %d are:\n", N);
// for (i = 2; i <= N; i++) {
// isPrime = 1;
// for (j = 2; j < i; j++) {
// if (i % j == 0) {
// isPrime = 0;
// break;
// }
// }
// if (isPrime) {
// printf("%d ", i);
// }
// }
// printf("\n");

// Pattern 1: Repeating row number


int rows = 4;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= rows - i; j++) {
printf(" ");
}
for (int k = 1; k <= i; k++) {
printf("%d ", i);
}
printf("\n");
}

// Pattern 2: 1 to i on each row


for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= rows - i; j++) {
printf(" ");
}
for (int k = 1; k <= i; k++) {
printf("%d ", k);
}
printf("\n");
}

// Pattern 3: Continuous numbers


int num = 1;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= rows - i; j++) {
printf(" ");
}
for (int k = 1; k <= i; k++) {
printf("%d ", num);
num++;
}
printf("\n");
}
return 0;
}

You might also like