Example -1
AIM:
Write a C program that will output this passage by Michael Singer. Make sure your output looks exactly as shown here (including spacing, line breaks,punctuation, and the title and author). Use Required Escape Sequence and ASCII Value.
There are three shapes in the output: Smiling Face, Diamond & Heart.
The ASCII Value for Smiling face is 1.
The ASCII Value for Diamond is is 4.
The ASCII Value for Heart is is 3.
PROGRAM CODE:
#include<stdio.h>
#include<conio.h>
int main()
{
char a=1,b=4,c=3;
clrscr();
printf("%c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c\n",a,b,c,a,b,c,a,b,c,a,b,c,a,b,c,a,b,c,a,b,c,a,b,c,a,b,c,a,b,c,a,b,c,a,b,c,a);
printf("%c",b);
printf("\"If you are resisting something,you are feeling it.\t\t\t");
printf("%c\n",b);
printf("%c",c);
printf("\tAny energy you fight, you are feeding it.\t\t\t");
printf("%c\n",c);
printf("%c",a);
printf("\t\tIf you are pushing something away,\t\t\t");
printf("%c\n",a);
printf("%c",b);
printf("\t\t\tYou are inviting it to stay.\"by Michel singer. ");
printf("%c\n",b);
printf("%c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c\n",a,b,c,a,b,c,a,b,c,a,b,c,a,b,c,a,b,c,a,b,c,a,b,c,a,b,c,a,b,c,a,b,c,a,b,c,a);
getch();
return 0;
}
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Example-2
AIM:
Ramesh‟s basic salary is input through the keyboard. His dearness allowance is 40% of basic salary, and house rent allowance is 20% of basic salary. Write a program to calculate his gross salary.
PROGRAM CODE:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int s,da,ha,ts;
printf("please enter ramesh's salary\n");
scanf("%d",&s);
da=(s*40)/100;
printf("ramesh's dearness allowance is %d\n",da);
ha=(s*20)/100;
printf("ramesh's house rant allowance is %d\n",ha);
ts=s+ha+da;
printf("ramesh total salary is %d",ts);
getch();
return 0;
}
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Example-3
AIM:
Write a program to calculate area of two circle. (πr2). Use Preprocessor directive named macro expansion for the symbol π (Symbolic Constant) without argument and with argument. Use typedef to rename the floatdatatype.
PROGRAM CODE:
#include<stdio.h>
#include<conio.h>
#define PI 3.14
#define PERIMETER(r) 2*PI*r
void main()
{
typedef float circle;
circle r1,r2,a1,a2;
printf("\n\n\n\tEnter the value of r1:");
scanf("%f",&r1);
printf("\tEnter the value of r2:");
scanf("%f",&r2);
a1=PI*r1*r1;
a2=PI*r2*r2;
printf("\tArea of radius %f is %.2f.\n",r1,a1);
printf("\tArea of radius %f is %.2f.\n",r2,a2);
printf("\tPerimeter of r1 is %2f\n",PERIMETER(r1));
printf("\tPerimeter of r2 is %2f",PERIMETER(r2));
getch();
}
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Example-4
AIM:
Write a program to do following:
Input an amount and convert it into rupees and paisa. (For Ex. 25.67Rs = 25 Rs and 67 Paisa).(Implicit type Conversion)
PROGRAM CODE:
#include<stdio.h>
#include<conio.h>
int main()
{
int R,P;
float amount;
clrscr();
printf("enter the amount\n");
scanf("%f",&amount);
R=amount;
printf("your Rupees is %d\n",R);
P=(amount-R)*100;
printf("your paisa is %d\n",P);
getch();
return 0;
}
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Example-5
AIM:
While purchasing certain items, a discount of 10% is offered if the quantity purchased is more than 1000.If quantity and price per item are input through the keyboard, write a program to calculate the total expenses. Use Simple If statement.
PROGRAM CODE:
#include<stdio.h>
#include<conio.h>
int main()
{
int n,ppr;
float expenses;
printf("please enter a number of item you perchased\n");
scanf("%d",&n);
printf("please enter price per item\n");
scanf("%d",&ppr);
if(n>1000)
{
expenses=0.9*ppr*n;
printf("your total expenses is %f\n",expenses);
}
else
{
expenses=(ppr*n);
printf("your total expenses is %f\n",expenses);
}
getch();
return 0;
}
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Example-6
AIM:
Given three points (x1, y1), (x2, y2) and (x3, y3), write a program to check if all the three points fall on one straight line. Use fabs() function of <maths.h>header file. Use if…elsestatement
PROGRAM CODE:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int x1,x2,x3,y1,y2,y3;
float s1,s2:
printf("\nEnter x1 ");
scanf("%d",&x1);
printf("Enter y1 ");
scanf("%d",&y1);
printf("Enter x2 ");
scanf("%d",&x2);
printf("Enter y2 ");
scanf("%d",&y2);
printf("Enter x3 ");
scanf("%d",&x3);
printf("Enter y3 ");
scanf("%d",&y3);
s1=fabs((y2-y1)/(x2-x1));
s2=fabs((y3-y2)/(x3-x2));
if(s1==s2)
{
printf("The points are colinear.");
}
else
{
printf("The points non colinear .");
}
getch();
}
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Example-7
AIM:
If the three sides of a triangle are entered through the keyboard, write a program to check whether the triangle is valid or not. The triangle is valid if the sum of two sides is greater than the largest of the three sides. Use nested if…else statement.
PROGRAM CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
int s1,s2,s3;
printf("\nEnter three sides of triangle");
scanf("\n%d %d %d",&s1,&s2,&s3);
if((s1+s2)>s3)
{
if((s2+s3>s1))
{
if((s3+s1)>s2)
{
printf("The triangle is valid.");
}
}
}
else
{
printf("The triangle is not valid.");
}
getch();
}
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Example-8
AIM:
An Insurance company follows following rules to calculate premium.
(1) If a person‟s health is excellent and the person is between 25 and 35 years of age and lives in a city and is a male then the premium is Rs. 4 per thousand and his policy amount cannot exceed Rs. 2 lakhs.
(2) If a person satisfies all the above conditions except that the gender is female then the premium is Rs. 3 per thousand and her policy amount cannot exceed Rs. 1 lakh.
(3) If a person‟s health is poor and the person is between 25 and 35 years of age and lives in a village and is a male then the premium is Rs. 6 per thousand and his policy cannot exceed Rs. 10,000.
(4) In all other cases the person is not insured. Write a program to output whether the person should be insured or not, his/her premium rate and maximum amount for which he/she can be
insured. Use Else…if Ladder.
PROGRAM-CODE:
#include<stdio.h>
#include<conio.h>
int main()
{
char health,place,gender;
int age;
printf("please enter your health like for excelant press e and for poor press p\n");
scanf("%c",&health);
fflush(stdin);
printf("please enter your place like for city press c and for village press v\n");
scanf("%c",&place);
fflush(stdin);
printf("please enter your gender like for male press m and for female press f\n");
scanf("%c",&gender);
fflush(stdin);
printf("please enter your age\n");
scanf("%d",&age);
if((health=='e'||health=='E')&&(place=='c'||place=='C')&&(gender=='m'||gender=='M')&&(age>=25)&&(age<=35))
{
printf("the primium is rs 4 per thousand and his policy amount can not exceed rs 2 lakh\n");
}
if((health=='e'||health=='E')&&(place=='c'||place=='C')&&(gender=='f'||gender=='F')&&(age>=25)&&(age<=35))
{
printf("the primium is rs 3 per thousand and his policy amount can not exceed rs 1 lakh\n");
}
if((health=='p'||health=='P')&&(place=='v'||place=='V')&&(gender=='m'||gender=='M')&&(age>=25)&&(age<=35))
{
printf("the primium is rs 6 per thousand and his policy amount can not exceed rs 10000\n");
}
else
{
printf("u r not valid for policy\n");
}
getch();
return 0;
}
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Example-9
AIM:
Write a program to input a character using getchar() and print the character using putchar() and check the character category. Also convert uppercase alphabet to lower case and vice versa. (Use Character Test Functions : isalnum(), isalpha(), isdigit(), islower(), isprint(), ispunct(), isspace(), isupper()) and (toupper() & tolower()) of <ctype.h> header file.
PROGRAM CODE:
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char a;
printf("\nEnter your character ");
a=getchar();
if((isalnum(a))&&(isprint(a)))
{
printf("\nThe input is printable\n");
if(isalpha(a))
{
printf("\nThe character is an alphabet\n");
if(isupper(a))
{
printf("\nThe character is in upper case\n");
a=tolower(a);
putchar(a);
}
else if(islower(a))
{
printf("\nThe character is in lower case\n");
a=toupper(a);
putchar(a);
}
}
if(isdigit(a))
{
printf("\nThe character is digit.");
putchar(a);
}
}
else if((ispunct(a))&&(isprint(a)))
{
printf("\nThe input is a punctuation.");
putchar(a);
}
else if((isspace(a))&&(isprint(a)))
{
printf("\nThe input is a blank space and not printable.");
putchar(a);
}
getch();
}
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Example-10
AIM:
Write a program to find the grace marks for a student using Switch Statement. The user should enter the class obtained by the student and the number of subjects he has failed in.
1. If the student gets first class and the number of subjects he failed in is greater than 3, then he does not get any grace. If the number of subjects he failed in is less than or equal to 3 then the grace is of 5 marks per subject.
2. If the student gets second class and the number of subjects he failed in is greater than 2, then he does not get any grace. If the number of subjects he failed in is less than or equal to 2 then the grace is of 4 marks per subject.
3. If the student gets third class and the number of subjects he failed in is greater than 1, then he does not get any grace. If the number of subjects he failed in is equal to 1 then the grace is of 5 marks per subject.
PROGRAM CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
Int fail_sub;
char Class;
clrscr();
printf("\nEnter class : ");
scanf("%c",&Class);
printf("\nEnter number of subjects stdent is failing : ");
scanf("%d",&fail_sub);
switch(Class)
{
case 'f': if(fail_sub>3)
{
printf("\nNo grace marks given");
}
else if(fail_sub<=3)
{
printf("\n5 grace marks per subject are given");
}
break;
case 's': if(fail_sub>2)
{
printf("\nNo grace marks given.");
}
else if(fail_sub<=2)
{
printf("\n4 grace marks per subject are given.");
}
break;
case 't': if(fail_sub>1)
{
printf("\nNo grace marks given.");
}
else if(fail_sub>=1)
{
printf("5 grace marks per subject are given.");
}
break;
default:
printf("Please enter correctly.");
}
getch();
}
Thank YOU.
Post a Comment