C Programs

The best way to learn C programming is by practicing examples. The page contains examples on basic concepts of C programming. You are advised to take the references from these examples and try them on your own.

All the programs on this page are tested and should work on all platforms.

  1. Array In C
Method 1 #include <stdio.h> #include <stdlib.h> int main() { int *ptr,sum=0,n,i; int a[10]; ptr=a; printf(“enter the number of elements\n”); scanf(“%d”,&n); printf(“enter the array elements\n”); for(i=0;i<n;i++) scanf(“%d”,&a[i]); printf(“MEMORY LOCATION OF THE FIRST VALUE %u\n”,&ptr[0]); i=0; while(i<n) { sum=sum+(*ptr); i++; ptr++; } printf(“sum is %d\n”,sum); printf(“FINAL MEMORY LOCATION AFTER ACCESSING %d INTEGERS IS ptr=%u”,n,ptr); return 0; } […]
  1. Number System In C
Method 1 #include<stdio.h> #include<conio.h> #include<math.h> void dec_oct(long int num) // Function Definition { long int rem[50],i=0,length=0; while(num>0) { rem[i]=num%8; num=num/8; i++; length++; } printf("nOctal number : "); for(i=length-1;i>=0;i--) printf("%ld",rem[i]); } void main() { long int num; clrscr(); printf("Enter the decimal number : "); scanf("%ld",&num); dec_oct(num); // Calling function getch(); } Output Enter the decimal number […]
  1. Number System In C
Method 1 #include<stdio.h> int main() { long int count = 0; char hexadecimal_value[85], binary_value[85]; printf("\nEnter Hexa - Decimal Value:\t"); scanf("%s", hexadecimal_value); printf("\nBinary Value of the Hexadecimal Number:\t"); while(hexadecimal_value[count]) { switch(hexadecimal_value[count]) { case 'A': printf("1010"); break; case 'B': printf("1011"); break; case 'C': printf("1100"); break; case 'D': printf("1101"); break; case 'E': printf("1110"); break; case 'F': printf("1111"); break; […]
  1. Number System In C
Method 1 – Using loops #include <stdio.h> int main() {     int OCTALVALUES[] = {0, 1, 10, 11, 100, 101, 110, 111};          long long octal, tempOctal, binary, place;          int rem;          printf("Enter any Octal number: ");          scanf("%lld", &octal);          tempOctal = octal;     binary = 0;          place  = 1;          while(tempOctal > 0) […]