CSE List of Sample Program
CSE List of Sample Program
Output
Output
Enter the Radius of a Circle: 2
Output
Output
Enter two no: 5 6
Sum: 11
#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
#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
Output
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);
return 0;
}
Output
Enter length of the rectangle: 5
Enter width of the rectangle: 10
Perimeter of rectangle = 30.000000
int main()
{
14. float principle, time, rate, 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;
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &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);
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;
#include <stdio.h>
int main() {
double number, sum = 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