Simple Programs
- Doing Programs gives you the great understanding about what you have learned.
- Doing Programs gives you the Confidence.
- So let's Start by doing some programming.
Q1: declare variable length,breadth,take value of length and breadth from user and calculate area of rectangle
-
Solution:
#include<stdio.h> //header file void main() { float area,lenght ,breadth; printf("enter the length"); scanf("%f",&lenght); // taking value of length from the user printf("enter the breadth"); scanf("%f",&breadth); //taking value of breadth from the user area = lenght*breadth; printf("Area of rectangle is %f",area); }
Output:
enter the length> 12.5
enter the breadth> 5.5
Area of rectangle is 68.750000 sqcm. -
Exercise:
Q2: declare variable radius,take value from the user and calculate area of circle
hint:Formula for Area of Circle is pi*radius2
Q3: declare Character variable ch_v take value from the user and print the previous and the next character.
-
Solution:
#include<stdio.h> //header file void main() { char char_v,b; //declaring variable printf("Enter the character>"); char_v = getchar(); b = char_v - 1; char_v = char_v +1; printf("previous character is %c \n next character is %c ",b,char_v); }
Output:
Enter the character>D
previous character is C
next character is E -
Exercise
Q4: declare Character variable ch_v take value from the user and print the previous to previous character and the next to next character.
Hint:Add and subtract 2 from the Variable.
-
Q5: declare Character variable ch_v take value from the user and print the Ascii value of that Character.
Solution:
#include<stdio.h> //header file void main() { char char_v; printf("Enter the character>"); char_v = getchar(); printf("Ascii value is %d",char_v); }
Output:
Enter the character>A
Ascii value is 65 -
Exercise:
Q6: declare two variable char_v1 and char_v2 and take value from user and print the difference of their Ascii Values.
HINT:Use '%d' to get Ascii Value of the Character.
0 Comments
Post a Comment