Module -1
List of Sample Program
Course: Principles of Programming using C Semester: I
Course Code: 22POP13/23
Q.No Question
Write a c program to print area of rectangle?
#include<stdio.h>
int main()
{
int length, breadth, area;
printf("\nEnter the Length of Rectangle : ");
scanf("%d", &length);
printf("\nEnter the Breadth of Rectangle : ");
scanf("%d", &breadth);
area = length * breadth;
1. printf("\nArea of Rectangle : %d", area);
return (0);
}
Output
Enter the Length of Rectangle : 5
Enter the Breadth of Rectangle : 4
Area of Rectangle : 20
Write a c program to print area of circle?
#include<stdio.h>
int main()
{
float r,area;
printf("Enter the Radius of a Circle");
scanf("%f",&r);
area = pi*radius*radius;
2.
printf("Area of Circle is: %f",area);
Output
Enter the Radius of a Circle: 2
Area of Circle is: 12.560000
Write a c program to print area of square
#include<stdio.h>
int main()
{
3. int side, area;
printf("\nEnter the Length of Side : ");
scanf("%d", &side);
area = side * side;
printf("\nArea of Square : %d", area);
return (0);
}
Output
Enter the Length of Side : 5
Area of Square : 25
Write a c program to print sum of two no’s?
#include<stdio.h>
int main()
{
int a, b, sum;
printf("\nEnter two no: ");
scanf("%d %d", &a, &b);
sum = a + b;
4. printf("Sum : %d", sum);
return(0);
}
Output
Enter two no: 5 6
Sum: 11
Write a c program to Swapping of two numbers without third variable
#include<stdio.h>
int main()
{
int a=10, b=20;
printf("Before swap a=%d b=%d",a,b);
a=a+b; //a=30 (10+20)
b=a-b; //b=10 (30-20)
5. a=a-b; //a=20 (30-10)
printf("\nAfter swap a=%d b=%d",a,b);
return 0;
}
Output:
Before swap a=10 b=20
After swap a=20 b=10
Write a c Program to Display "Hello, World!
#include<stdio.h>
int main( )
{
6.
printf(“hello world”);
return 0;
}
Output: hello world
Write a cProgram to Add Two Integers
#include <stdio.h>
int main()
{
int number1, number2, sum;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
sum= number1 + number2;
7. printf("%d + %d = %d", number1, number2, sum);
return 0;
}
Output
Enter two integers: 12
11
12 + 11 = 23
Write a C Program to Find ASCII Value of a Character
#include <stdio.h>
int main()
{
char c;
Printf("Enter a character: ");
scanf("%c", &c);
8.
printf("ASCII value of %c = %d", c, c);
return 0;
}
Output
Enter a character: GASCII value of G = 71
Write a C Program to Find the Size of Variables
#include<stdio.h>
int main()
{
int intType;
float floatType;
double doubleType;
9. char charType;
printf("Size of int: %ld bytes\n", sizeof(intType));
printf("Size of float: %ld bytes\n", sizeof(floatType));
printf("Size of double: %ld bytes\n", sizeof(doubleType));
printf("Size of char: %ld byte\n", sizeof(charType));
return 0;
}
Output
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte
Write a C Program to Swap Numbers Using Temporary Variable
#include<stdio.h>
int main()
{
double first, second, temp;
printf("Enter first number: ");
scanf("%lf", &first);
printf("Enter second number: ");
scanf("%lf", &second);
temp = first;
first = second;
10. second = temp;
printf("\nAfter swapping, first Number = %.2lf\n", first);
printf("After swapping, second Number = %.2lf", second);
return 0;
}
Output
Enter first number: 1.20
Enter second number: 2.45
After swapping, first Number = 2.45
After swapping, second Number = 1.20
Write aC Program to Area of Right Angled Triangle?#include<stdio.h>
int main()
{
int base, height;
float area;
printf("\nEnter the base of Right Angle Triangle : ");
scanf("%d", &base);
printf("\nEnter the height of Right Angle Triangle : ");
scanf("%d", &height);
area = 0.5 * base * height;
printf("\nArea of Right Angle Triangle : %f", area);
11.
return (0);
}
Output
Enter the base of Right Angle Triangle : 4
Enter the height of Right Angle Triangle: 8
Area of Right Angle Triangle: 16.0
How to print multiple line messages using single printf in C language
12. #include <stdio.h>
intmain()
{
printf("This is line 1.");
printf("This is line 2.");
printf("This is line 3.");
return 0;
}
This is line 1.This is line 2.This is line 3
Write a c program to print perimeter of rectangle?
#include <stdio.h>
int main()
{
float length, width, perimeter;
/*
* Input length and width of rectangle from user
*/
printf("Enter length of the rectangle: ");
scanf("%f", &length);
printf("Enter width of the rectangle: ");
scanf("%f", &width);
13. /* Calculate perimeter of rectangle */
perimeter = 2 * (length + width);
/* Print perimeter of rectangle */
printf("Perimeter of rectangle = %f units ", perimeter);
return 0;
}
Output
Enter length of the rectangle: 5
Enter width of the rectangle: 10
Perimeter of rectangle = 30.000000
Write a c program to Find simple interest?
#include <stdio.h>
int main()
{
14. float principle, time, rate, SI;
/* Input principle, rate and time */
printf("Enter principle (amount): ");
scanf("%f", &principle);
printf("Enter time: ");
scanf("%f", &time);
printf("Enter rate: ");
scanf("%f", &rate);
/* Calculate simple interest */
SI = (principle * time * rate) / 100;
/* Print the resultant value of SI */
printf("Simple Interest = %f", SI);
return 0;
}
OUTPUT
Enter principle (amount): 1200
Enter time: 2
Enter rate: 5.4
Simple Interest = 129.600006
Module -2
List of Sample Program
Q.No Question
1 Program to demonstrate increment & decrement operator
#include<stdio.h>
void main()
{
int a,b;
a=1;
printf("value of a = %d \n", a);
b=a++; //POST INCREMENT
printf("value of b after a++ = %d \n", b);
printf("value of a after a++= %d \n", a);
a=1;
printf("value of a = %d \n", a);
b=++a; //PRE INCREMENT
printf("value of b after ++a = %d \n",b);
printf("value of a after ++a = %d \n",a);
a = 5;
printf("value of a = %d \n", a);
b = a--; //POST DECREMENT
printf("value of b after a-- = %d \n",b);
printf("value of a after a-- = %d \n",a);
a = 5;
printf("value of a = %d \n", a);
b = --a; //PREDECREMENT
printf("value of b after --a = %d \n",b);
printf("value of a after --a = %d \n",a);
}
Output
value of a = 1
value of b after a++ = 1
value of a after a++= 2
value of a = 1
value of b after ++a = 2
value of a after ++a = 2
value of a = 5
value of b after a-- = 5
value of a after a-- = 4
value of a = 5
value of b after --a = 4
value of a after --a = 4
2 Write a c Program to demonstrate Arithmetic operator
#include<stdio.h>
void main()
{
int c,d,e,f,g;
int a=10,b=2;
c = a+b;
d=a-b;
e=a*b;
f=a/b;
g=a%b;
printf("%d + %d= %d \n",a,b,c);
printf("%d-%d=%d\n", a, b,d);
printf("%d*%d=%d\n", a, b,e);
printf("%d/%d=%d\n", a,b,f);
printf("%d %% %d=%d\n", a,b,g);
}
Output
10 + 2= 12
10-2=8
10*2=20
10/2=5
10 % 2=0
3. Write a C program to use integer arithmetic to convert a given number of days into months and
days
#include<stdio.h>
void main()
{
int days,month,rem_day;
printf(“Enter the number of days :");
scanf("%d",&days);
month = days / 30;
rem_day= days % 30;
printf(“Months = %d \n Days = %d", month,
rem_day);
}
Output
Enter the number of days:265
Months = 8
Days = 25
4. Program to show working of Relational operators.
#include<stdio.h>
void main()
{
int a=10,b=20;
printf("%d<%d =%d \n",a,b,a<b);
printf("%d>%d =%d\n",a,b,a>b);
printf("%d<=%d =%d\n",a,b,a<=b);
printf("%d>=%d =%d\n",a,b,a>=b);
printf("%d==%d =%d\n",a,b,a==b);
printf("%d!=%d =%d\n",a,b,a!=b);
}
Output
10<20=1
10>20=0
10<=20=1
10>=20=0
10==20 = 0
10!=20 = 1
5. Program to show working of logical operators.
#include<stdio.h>
void main() {
int a=10,b=11,c=12,d;
d=(a<b)&&(c<b);
printf(" d = %d",d);
d= (a<b)||(c<b);
printf("\n d = %d",d);
}
Output
d=0
d=1
6. Program to demonstrate assignment operators
#include<stdio.h>
void main()
{
int a=5,b;
b=a;
printf("\n b= %d",b);
b+=a;
printf("\n b=%d",b);
b-=a;
printf("\n b=%d",b);
b*=a;
printf("\n b=%d",b);
b/=a;
printf("\n b=%d",b);
}
Output
b= 5
b=10
b=5
b=25
b=5
7. Program to demonstrate conditional operators
#include<stdio.h>
void main() {
int a=5 ,b=4,c;
c=a>b?a:b
;
printf("c=%d",c);
}
Output
c=5
8. Write C program to find largest of 3 numbers using ternary operators
#include<stdio.h>
void main()
{
int a,b,c;
int result;
printf("enter the values of 3
numbers");
scanf("%d%d%d",&a,&b,&c);
result = (a>b && a>c)?a:(b>c)?b:c;
printf("%d is biggest ",result);
}
Output
enter the values of 3 numbers 6
45
6
45 is biggest
9. Program to demonstrate bitwise operation
#include<stdio.h>
void main()
{
int a=60 ,b=13;
int c;
c=a&b;
printf("\n bitwise and : c= %d",c);
c=a|b;
printf("\n bitwise or : c=%d",c);
c=~a;
printf("\n bitwise complement : c=%d",c);
c=a ^ b;
printf("\n bitwise xor : c=%d",c);
c=a<<2;
printf("\n left shift : c=%d",c);
c=a>>2;
printf("\n right shift : c=%d",c);
}
OUTPUT:
bitwise and : c= 12
bitwise or : c=61
bitwise complement : c=-61
bitwise xor : c=49
left shift : c=240
right shift : c=15
10. Program to display a number if it is negative
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
// true if number is less than 0
if (number < 0) {
printf("You entered %d.\n", number);
}
printf("The if statement is easy.");
return 0;
}
Output
Enter an integer: -2
You entered -2.
The if statement is easy.
11. Check whether an integer is odd or even
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
// True if the remainder is 0
if (number%2 == 0) {
printf("%d is an even integer.",number);
}
else {
printf("%d is an odd integer.",number);
}
return 0;
}
Output
Enter an integer: 7
7 is an odd integer.
12. Program to relate two integers using =, > or < symbol
#include <stdio.h>
int main() {
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
//checks if the two integers are equal.
if(number1 == number2) {
printf("Result: %d = %d",number1,number2);
}
//checks if number1 is greater than number2.
else if (number1 > number2) {
printf("Result: %d > %d", number1, number2);
}
//checks if both test expressions are false
else {
printf("Result: %d < %d",number1, number2);
}
return 0;
}
Output
Enter two integers: 12
23
Result: 12 < 23
13. Print numbers from 1 to 10 using for loop
#include <stdio.h>
int main() {
int i;
for (i = 1; i < 11; ++i)
{
printf("%d ", i);
}
return 0;
}
Output
1 2 3 4 5 6 7 8 9 10
14. Program to add numbers until the user enters zero using do while
#include <stdio.h>
int main() {
double number, sum = 0;
// the body of the loop is executed at least once
do {
printf("Enter a number: ");
scanf("%lf", &number);
sum += number;
}
while(number != 0.0);
printf("Sum = %.2lf",sum);
return 0;
}
Output
Enter a number: 1.5
Enter a number: 2.4
Enter a number: -3.4
Enter a number: 4.2
Enter a number: 0
Sum = 4.70
15. Print numbers from 1 to 5 using while loop
#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
printf("%d\n", i);
++i;
}
return 0;
}
Output
1
2
3
4
5