Basic Practice Programs in C

C “Hello World!” Program

Code:

#include<stdio.h>    
int main()
{    
printf("Hello World!");    
return 0;   
}

Output:

Hello World!

C Program to Print an Integer (Entered by the User)

Code:

#include <stdio.h>
int main()
{   
    int integer;
    printf("Enter a number: ");  
    scanf("%d", &integer);
    printf("Your entered integer number is %d.",integer);
    return 0;
}

Output:

Enter a number: 10
Your entered integer number is 10.

C Program to Add Two Integers

Code:

#include <stdio.h>
int main()
{
  int a = 10;
  int b = 20;
  int c ;
  c = a + b;
  printf("Sum of a and b is %d\n", c );
   return 0;
}

Output:

Sum of a and b is 30

C Program to Multiply Two Floating-Point Numbers

Code:

#include <stdio.h>
int main()
{
  float a = 10.5, b = 1.5;
  float c ;
  c = a * b;
  printf("Product of a and b is %.2f\n", c);
   return 0;
}

Output:

Product of a and b is 15.75

C Program to Find ASCII Value of a Character

Code:

#include<stdio.h>
int main()
{
    char i;
    for ( i = 65; i <= 122; i=i+1)
    {
        if(i>90 && i<97)
            continue;
        printf(", %c -%4d",i,i);
    }
    printf(",\n");
    return 0;
}

Output:

, A -  65, B -  66, C -  67, D -  68, E -  69, F -  70, G -  71, H -  72, I -  73, J -  74, K -  75, L -  76, M -  77, N -  78, O -  79, P -  80, Q -  81, R -  82, S -  83, T -  84, U -  85, V -  86, W -  87, X -  88, Y -  89, Z -  90, a -  97, b -  98, c -  99, d - 100, e - 101, f - 102, g - 103, h - 104, i - 105, j - 106, k - 107, l - 108, m - 109, n - 110, o - 111, p - 112, q - 113, r - 114, s - 115, t - 116, u - 117, v - 118, w - 119, x - 120, y - 121, z - 122,

C Program to Compute Quotient and Remainder

Code:

#include <stdio.h>
int main()
{
    int Div, Divr, Quot, Rem;
    printf("Enter first value: ");
    scanf("%d", &Div);
    printf("Enter second value: ");
    scanf("%d", &Divr);
    Quot = Div / Divr;
    Rem = Div % Divr;
    printf("Quotient is %d\n", Quot);
    printf("Remainder %d", Rem);
    return 0;
}

Output:

Enter first value: 60 
Enter second value: 12
Quotient is 5
Remainder 0

C Program to Find the Size of int, float, double and char

Code:

#include<stdio.h>
int main() {
    int a;
    float b;
    double c;
    char d;
    printf("Type      Size\n");
    printf("int     %ld bytes\n", sizeof(a));
    printf("float   %ld bytes\n", sizeof(b));
    printf("double  %ld bytes\n", sizeof(c));
    printf("char    %ld byte\n", sizeof(d));
    return 0;
}

Output:

Type      Size
int     4 bytes
float   4 bytes
double  8 bytes
char    1 byte

C Program to Swap Two Numbers

Code:

#include<stdio.h>
int a,b,temp;
int main()
{
  printf("Enter the value of a: ");
  scanf("%d",&a);
  printf("Enter the value for b: ");
  scanf("%d",&b);
  printf("Value of a is %d and b is %d before swapping.\n",a,b);
  temp=a;
  a=b;;
  b=temp;
  printf("Value of a is %d and b is %d after swapping.\n",a,b);
  return 0;
}

Output:

Enter the value of a: 5
Enter the value for b: 3
Value of a is 5 and b is 3 before swapping.
Value of a is 3 and b is 5 after swapping.