[ CP LAB – SAMPLE PROGRAMS]
A “C” PROGRAMS ON DATATYPES:
01. Write a program to print basic datatypes like integers, characters, float and double.
Aim: To write a program to print different datatypes.
Code:
#include <stdio.h>
int main()
{
char a='A';
int b=123;
float c=3.14;
double d=123.456789;
printf("Character: %c\n", a);
printf("Integer: %d\n", b);
printf("Float: %f\n", c);
printf("Double: %lf\n", d);
return 0;
}
Output:
Character: A
Integer: 123
Float: 3.140000
Double: 123.456789
02. Write a program to print void datatype.
Aim: To write a program to print void datatype.
Code:
#include <stdio.h>
#include<conio.h>
void main()
{
Clrscr();
printf("hello! welcome");
getch();
Output:
hello! Welcome
A “C” PROGRAMS ON operators:
03. Write a program to print arithmetic operators.
Aim: To write a program to print arithmetic operators.
Code:
1|Page
[ CP LAB – SAMPLE PROGRAMS]
#include <stdio.h>
int main()
{
int a = 10, b = 4, res;
// Addition
res = a + b;
printf("a + b is %d\n", res);
// Subtraction
res = a - b;
printf("a - b is %d\n", res);
// Multiplication
res = a * b;
printf("a * b is %d\n", res);
// Division
res = a / b;
printf("a / b is %d\n", res);
// Modulus
res = a % b;
printf("a %% b is %d\n", res);
return 0;
}
Output:
a + b is 14
a - b is 6
a * b is 40
a / b is 2
a % b is 2
04. Write a program to print relational operator.
Aim: To Write a program to print relational operator.
Code:
#include <stdio.h>
int main()
{
int a = 5, b = 5;
printf("%d == %d is %d \n", a, b, a == b);
printf("%d > %d is %d \n", a, b, a > b);
printf("%d < %d is %d \n", a, b, a < b);
printf("%d != %d is %d \n", a, b, a != b);
printf("%d >= %d is %d \n", a, b, a >= b);
printf("%d <= %d is %d \n", a, b, a <= b);
return 0;
}
2|Page
[ CP LAB – SAMPLE PROGRAMS]
Output:
5 == 5 is 1
5 > 5 is 0
5 < 5 is 0
5 != 5 is 0
5 >= 5 is 1
5 <= 5 is 1
05. Write a program to print bitwise operator.
Aim: To write a program to print bitwise operator.
Code:
int main()
{
// a = 5 (00000101 in 8-bit binary)
// b = 9 (00001001 in 8-bit binary)
unsigned int a = 5, b = 9;
// The result is 00000001
printf("a&b = %u\n", a & b);
// The result is 00001101
printf("a|b = %u\n", a | b);
// The result is 00001100
printf("a^b = %u\n", a ^ b);
return 0;
}
Output:
a&b = 1
a|b = 13
a^b = 12
06. Write a program to print logical operator.
Aim: To write a program to print logical operator.
Code:
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;
result = (a == b) && (c > b);
printf("(a == b) && (c > b) is %d \n", result);
3|Page
[ CP LAB – SAMPLE PROGRAMS]
result = (a == b) && (c < b);
printf("(a == b) && (c < b) is %d \n", result);
result = (a == b) || (c < b);
printf("(a == b) || (c < b) is %d \n", result);
result = (a != b) || (c < b);
printf("(a != b) || (c < b) is %d \n", result);
result = !(a != b);
printf("!(a != b) is %d \n", result);
result = !(a == b);
printf("!(a == b) is %d \n", result);
return 0;
}
Output:
(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0
07. Write a program to print assignment operator.
Aim: To write a program to print assignment operator.
Code:
#include <stdio.h>
int main()
{
int a = 5, c;
c = a;
printf("c = %d\n", c);
c += a;
printf("c = %d\n", c);
c -= a;
printf("c = %d\n", c);
c *= a;
printf("c = %d\n", c);
c /= a;
printf("c = %d\n", c);
c %= a;
printf("c = %d\n", c);
return 0;
}
Output:
c=5
4|Page
[ CP LAB – SAMPLE PROGRAMS]
c = 10
c=5
c = 25
c=5
c=0
08. Write a program to print conditional operator.
Aim: To write a program to print conditional operator.
Code:
#include <stdio.h>
int main()
{
int score = 75;
char grade;
grade = (score >= 60) ? 'P' : 'F';
printf("Your score is: %d\n", score);
printf("Your grade is: %c\n", grade);
return 0;
}
Output:
Your score is: 75
Your grade is: P
09. Write a program to print increment and decrement operator.
Aim: To write a program to print increment and decrement operator.
Code:
#include <stdio.h>
int main()
{
int a = 10,b=20;
printf("++a = %d \n", ++a);
printf("a++ = %d \n", a++);
printf("--b = %d \n", --b);
printf("b-- = %d \n", b--);
return 0;
}
Output:
++a = 11
a++ = 11
--b = 19
b-- = 19
10. Write a program to print special operator like sizeof() operator.
5|Page
[ CP LAB – SAMPLE PROGRAMS]
Aim: To write a program to print special operator like sizeof() operator.
Code:
#include <stdio.h>
int main()
{
int a;
float b;
double c;
char d;
printf("Size of int=%lu bytes\n",sizeof(a));
printf("Size of float=%lu bytes\n",sizeof(b));
printf("Size of double=%lu bytes\n",sizeof(c));
printf("Size of char=%lu byte\n",sizeof(d));
return 0;
}
Output:
Size of int=4 bytes
Size of float=4 bytes
Size of double=8 bytes
Size of char=1 byte
A “C” PROGRAMS ON variables initialization:
11. Write a program to perform compile-time initialization.
Aim: To write a program to perform compile-time initialization.
Code:
#include <stdio.h>
int main()
{
int x = 10, y = 20, z = 30;
printf("x: %d, y: %d, z: %d\n", x, y, z);
return 0;
}
Output:
x: 10, y: 20, z: 30
12. Write a program to perform run-time initialization.
Aim: To write a program to perform run-time initialization.
Code:
#include <stdio.h>
int main()
6|Page
[ CP LAB – SAMPLE PROGRAMS]
{
int age;
printf("Please enter your age: ");
scanf("%d", &age);
printf("Your age is: %d\n", age);
return 0;
}
Output:
Please enter your age: 18
Your age is: 18
A “C” PROGRAMS ON CONSTANTS:
13. Write a program to define constant.
Aim: To write a program to define constant.
Code:
#include <stdio.h>
int main()
{
const int a = 10;
printf("%d", a);
return 0;
}
Output:
Please enter your age: 18
Your age is: 18
A “C” PROGRAMS ON TYPE CONVERSION AND TYPE CASTING:
14. Write a program to perform implicit type conversion.
Aim: To write a program to perform implicit type conversion.
Code:
#include <stdio.h>
int main()
{
int a = 15, b = 2;
float div;
div = a / b;
printf("The result is %f\n", div);
return 0;
}
Output:
The result is 7.000000
7|Page
[ CP LAB – SAMPLE PROGRAMS]
15. Write a program to perform explicit type conversion.
Aim: To write a program to perform explicit type conversion.
Code:
#include <stdio.h>
int main()
{
float v1=3.14;
int v2;
v2=(int)v1;
printf("Original float value: %f\n",v1);
printf("Converted integer value: %d\n",v2);
return 0;
}
Output:
Original float value: 3.140000
Converted integer value: 3
2|Page