Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
12 views2 pages

Document 1

The document contains various C programming code snippets demonstrating the use of pointers for different tasks such as checking even or odd numbers, reversing a number, calculating the perimeter of a circle, and more. Each code snippet illustrates a specific function or operation, highlighting the versatility of pointers in memory management and data manipulation. The document serves as a reference for implementing basic algorithms and operations using pointers in C.

Uploaded by

mohitedition4
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)
12 views2 pages

Document 1

The document contains various C programming code snippets demonstrating the use of pointers for different tasks such as checking even or odd numbers, reversing a number, calculating the perimeter of a circle, and more. Each code snippet illustrates a specific function or operation, highlighting the versatility of pointers in memory management and data manipulation. The document serves as a reference for implementing basic algorithms and operations using pointers in C.

Uploaded by

mohitedition4
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
You are on page 1/ 2

Check Even or Odd using b = (int*)malloc(n * Reverse of Number using

Pointer sizeof(int)); Pointer


#include <stdio.h> printf("Enter %d #include <stdio.h>
int main() { numbers:\n", n); int main() {
int num, *p;
printf("Enter an integer: ");
for(i = 0; i < n; i++) { int n, rev = 0, *p;
scanf("%d", &num); scanf("%d", &a[i]); printf("Enter number:
p = &num; if (a[i] != 0) { ");
if (*p % 2 == 0) b[j++] = a[i]; scanf("%d", &n);
printf("Even\n"); sum += a[i]; p = &n;
else
printf("Odd\n");
} while (*p) {
return 0; } rev = rev * 10 +
} printf("Sum of non-zero (*p % 10);
elements = %d\n", sum); *p /= 10;
. Perimeter of Circle using free(a); }
Macro free(b); printf("Reverse
#include <stdio.h> return 0; = %d\n", rev);
#define PI 3.1416 } }
int main() { Average of First n Numbers
float r, perimeter; using Pointer Check Prime using Pointer
printf("Enter radius: "); #include <stdio.h> #include <stdio.h>
scanf("%f", &r); int main() { void checkPrime(int *n) {
perimeter = 2 * PI * r; int n, i, *p, sum = 0; int i, c = 0;
printf("Perimeter = %.2f\n", printf("Enter n: "); for(i = 2; i <= *n/2; i++)
perimeter); scanf("%d", &n); if(*n % i == 0) c++;
return 0; int arr[n]; printf(c ? "Not
} p = arr; Prime\n" : "Prime\n");
printf("Enter %d }
Interchange Two Variables numbers:\n", n); int main() {
using Pointers for(i = 0; i < n; i++) { int n;
#include <stdio.h> scanf("%d", p + i); printf("Enter number:
int main() { sum += *(p + i); ");
int a, b, temp, *p1, *p2; } scanf("%d", &n);
printf("Enter two numbers: printf("Average = %.2f\n", checkPrime(&n);
"); (float)sum / n);
scanf("%d %d", &a, &b); return 0;} Fibonacci Series using DMA
p1 = &a; Area & Perimeter using
p2 = &b; Pointer #include <stdio.h>
temp = *p1; #include <stdio.h> #include <stdlib.h>
*p1 = *p2; #define PI 3.1416 int main() {
*p2 = temp; int main() { int n, i, *f;
printf("After swap: a = %d, float r, area, peri, *p; printf("Limit: ");
b = %d\n", a, b); printf("Enter radius: "); scanf("%d", &n);
return 0; scanf("%f", &r); f = malloc(n * sizeof(int));
} p = &r; f[0]=0; f[1]=1;
area = PI * (*p) * (*p); printf("%d %d ", f[0], f[1]);
Copy Non-zero Elements and peri = 2 * PI * (*p); for(i=2; i<n; i++) {
Sum (DMA) printf("Area = %.2f, f[i]=f[i-1]+f[i-2];
#include <stdio.h> Perimeter = %.2f\n", area, printf("%d ", f[i]);
#include <stdlib.h> peri); }
int main() { return 0; free(f);
int n, i, *a, *b, j = 0, sum = 0; } }
printf("Enter n: ");
scanf("%d", &n);
a = (int*)malloc(n *
sizeof(int));
separate functions to accept calculate length of string
Sum of Diagonal (2D Array + and display using pointer #include <stdio.h>
Pointer) #include <stdio.h> #include <string.h>
#include <stdio.h> void accept(int *ptr) {
int main() { scanf("%d", ptr); int main() {
int a[10][10], n, i, sum=0; } char str[100];
printf("Size: "); void display(int *ptr) { fgets(str, sizeof(str), stdin);
scanf("%d", &n); printf("%d\n", *ptr); str[strcspn(str, "\n")] = '\0';
for(i=0;i<n*n;i++) } // Remove newline
scanf("%d", *(a)+i); int main() { printf("Length: %lu\n",
for(i=0;i<n;i++) int num; strlen(str));
sum += *(*(a+i)+i); accept(&num); return 0;
printf("Diagonal sum display(&num); }
= %d\n", sum); return 0;
} } all basic arithmetic operations
#include <stdio.h>
Sum of Upper Diagonal (2D + store all even numbers in an int main() {
DMA) array int a, b;
#include <stdio.h> #include <stdio.h> printf("Enter two integers:
#include <stdlib.h> ");
int main() { int main() { scanf("%d %d", &a, &b);
int n, i, j, sum=0, **a; int arr[100], num, size = printf("Addition: %d\nSubtra
printf("Size: "); 0, n; ction: %d\nMultiplication: %d
scanf("%d", &n); scanf("%d", &n); \n", a + b, a - b, a * b);
a = malloc(n*sizeof(int*)); for (int i = 0; i < n; i++) if (b != 0) {
for(i=0;i<n;i++) { {
a[i] = malloc(n*sizeof(int)); scanf("%d", &num); printf("Division: %.2f\nModul
for(j=0;j<n;j++) if (num % 2 == 0) us: %d\n", (float)a / b, a % b);
scanf("%d", &a[i][j]); arr[size++] = num; } else {
} } printf("Division and
for(i=0;i<n;i++) for (int i = 0; i < size; Modulus: Undefined\n");
for(j=i+1;j<n;j++) i++) printf("%d ", arr[i]); }
sum += a[i][ return 0; return 0;}
}
array in the reverse square of given number using
#include <stdio.h> MACRO
command line arguments in #include <stdio.h>
void accept(int *p, int n) { reverse order
while (n--) scanf("%d", #include <stdio.h> #define SQUARE(x) ((x) *
p++); int main(int argc, char *argv[]) (x)) // Macro to calculate
} { square
for (int i = argc - 1; i > 0; i--) {
void displayRev(int *p, int n) { printf("%s ", argv[i]); int main() {
while (n--) printf("%d ", *(p } int num;
+ n)); printf("\n"); printf("Enter a number:
} return 0; ");
int main() { } scanf("%d", &num);
int a[100], n;
scanf("%d", &n); printf("Square of %d
accept(a, n); is: %d\n", num,
displayRev(a, n); SQUARE(num));
return 0;} return 0;
}

You might also like