Practical Assignment
Computer Programming
1. Program to print “Welcome message on screen”.
Input –
#include <stdio.h>
int main() {
printf("Welcome");
return 0;
}
Output –
Welcome
2. Program to find sum and average of two numbers.
Input –
#include <stdio.h>
int main() {
int x,y,sum;
float avg;
printf("Enter first number : ");
scanf("%d",&x);
printf("Enter second number : ");
scanf("%d",&y);
sum = x+y;
avg = (float)(x+y)/2;
printf("Sum is %d",sum);
printf("\nAverage is %f",avg);
return 0;
}
Output –
Enter first number : 40
Practical Assignment
Computer Programming
Enter second number : 50
Sum is 90
Average is 45.000000
3. Program to display the details entered by user.
Input –
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
printf("You entered: %d", number);
return 0;
}
Output -
Enter an integer :
You entered :
4. Program to demonstrate working of logical operators.
Input –
#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);
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);
Practical Assignment
Computer Programming
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
5. Program to check whether a character is a Vowel or not. (if…..else)?
Input –
#include <stdio.h>
int main() {
char ch;
printf("Enter any character: ");
scanf("%c", &ch);
if ((ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' ||
ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U'))
{
printf("%c is a vovel",ch);
}
else
{
printf("%c is a constant", ch);
}
return 0;
}
Practical Assignment
Computer Programming
Output –
Enter any character :
6. Program to calculate area of different shapes (circle, rectangle, square). (switch…..case).
Input –
#include <stdio.h>
int main() {
int fig_code;
float side, length, breadth, area, radius;
printf("1.Circle\n");
printf("2.Rectangle\n");
printf("3.Square\n");
printf("Enter the figure number\n");
scanf("%d", &fig_code);
switch(fig_code)
{
case 1:
printf("Enter the radius\n");
scanf("%f", &radius);
area = 3.142 * radius * radius;
printf ("Area of circle = %f\n", area);
break;
case 2:
printf("Enter the breadth and length\n");
scanf("%f %f", &breadth, &length);
area = breadth * length;
printf("Area of a Reactangle = %f\n", area);
break;
case 3:
printf("Enter the side\n");
scanf("%f", &side);
area = side * side;
printf("Area of a Square=%f\n", area);
Practical Assignment
Computer Programming
break;
default:
printf("Error in the figure\n");
break;
}
return 0;
}
Output –
1.Circle
2.Rectangle
3.Square
Enter the figure number
7. Program to generate Fibonacci series up to a given input number.
Input –
#include <stdio.h>
int main() {
int i, n, x1 = 0, x2 = 1, nextTerm;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (i = 1; i <= n; ++i) {
printf("%d, ", x1);
nextTerm = x1 + x2;
x1 = x2;
x2 = nextTerm;
}
return 0;
}
Output –
Enter the number of terms:
8. Program to print the number from 1 to 20 except 3 and 17. (using continue).
Input –
#include <stdio.h>
Practical Assignment
Computer Programming
int main() {
int i;
for (int i=1; i<=20; ++i)
{
if (i == 3 || i == 17)
{
continue;
}
printf("%d ", i);
}
return 0;
}
Output –
1 2 4 5 6 7 8 9 10 11 12 13 14 15 16 18 19 20