C Language Introduction & examples
#include <stdio.h>
int main()
{
int a;
printf("Example programs\n");
scanf(“%d”,&a);
printf(“given value: %d”,a);
return 0;
}
Output
Example programs
5 Given value: 5 |
Example-2: add integers and store in float
#include <stdio.h>
int main()
{
int a,b;
float c;
scanf("%d %d",&a,&b);
c=a+b;
printf("sum: %f",c);
return 0;
}
Output
5
10 sum: 15.oooooo |
Example-3 : Arithmetic operators
#include <stdio.h>
int main()
{
int a,b;
scanf("%d %d",&a,&b);
printf("addition: %d \n",a+b);
printf("subtraction: %d \n",a-b);
printf("multiplication: %d \n",a*b);
printf("division: %d \n",a/b);
printf("modulus: %d \n",a%b);
return 0;
}
Output
15
12 addition: 27 subtraction: 3 multiplication: 180 division: 1 modulus: 3 |
Example-4: swap two numbers using a temporary variable
#include <stdio.h>
int main()
{
int a,b,c;
scanf(" %d %d",&a,&b);
printf("before swapping a = %d b = %d\n",a,b);
c=b;
b=a;
a=c;
printf("after swapping a = %d b = %d",a,b);
return 0;
}
Output
7 8
before swapping a = 7 b = 8 after swapping a = 8 b = 7 |
Example-5: swap two numbers without using temporary variable
#include <stdio.h>
int main()
{
int a,b;
scanf(" %d %d",&a,&b);
printf("before swapping a = %d b = %d\n",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("after swapping a = %d b = %d",a,b);
return 0;
}
Output
5 6
before swapping a = 5 b = 6 after swapping a = 6 b = 5 |