C Programs
Short Description
dgx...
Description
1. Program to Print Hello World! #include int main() { printf("Hello World!\n"); return 0; }
2. Output : Hello Aliens ! Welcome to our planet Earth. #include int main() { printf("Hello Aliens ! Welcome to our planet Earth. \n"); return 0; }
3. Think that you are a scientist and you have invented a Humanoid Robo.You want to introduce your Robo in a public meeting.You need to feed the information that the Robo has to speak in the public meeting.So feed the basic information into the Robo using C program.
Sample Input and output: Enter the Name : Chitti Enter the Creator Name : Dr.Vasegran Enter the Purpose : militaryservice Memory Space : 22
Speed : 1.1 My Details : I am the Robot named Chitti. I was created by Dr.Vasegran. I am created for the purpose of militaryservice. My memory space is around 22Gb and my speed is 1.1Tb.
#include int main () { char a[20], b[20], c[20]; int d; float e; printf("Enter the Name :\n"); scanf("%s", a); printf("Enter the Creator Name :\n"); scanf("%s", b); printf("Enter the Purpose :\n"); scanf("%s", c); printf("Memory Space :\n"); scanf("%d", &d); printf("Speed :\n"); scanf("%f", &e); printf("My Details :\n"); printf("I am the Robot named %s.\n",a); printf("I was created by %s.\n", b); printf("I am created for the purpose of %s.\n", c); printf("My memory space is around %dGb and my speed is %.1fTb.\n", d,e); return 0; }
4. NEWSPAPER REPORT In Japan ,there was a very huge Tsunami. Millions and millions worth buildings and properties were destroyed. Many people lost their lives. Most of them were injured and few were safe. A news reporter arrives to the spot to take the current survey regarding the situation of the people alive , dead and injured. He wanted to publish it in the newspaper and ask the other countries to help the affected people. Can you please help him in this noble cause by writing a C program to generate the newspaper report?
SAMPLE INPUT AND OUTPUT FORMAT: Enter the number of people dead: 2000 Enter the number of people injured: 3000 Enter the number of people safe: 10000 TSUNAMI REPORT OF JAPAN The number of people 1)Dead:2000 2)Injured:3000 3)Safe:10000 Please help the people who are suffering!!!
#include int main() { int dead, injured, safe; printf("Enter the number of people dead:\n"); scanf("%d", &dead); printf("Enter the number of people injured:\n"); scanf("%d", &injured); printf("Enter the number of people safe:\n"); scanf("%d", &safe); printf("TSUNAMI REPORT OF JAPAN\n"); printf("The number of people\n");
printf("1)Dead:%d\n", dead); printf("2)Injured:%d\n", injured); printf("3)Safe:%d\n", safe); printf("Please help the people who are suffering!!!\n"); return 0; }
Operators and Expressions 1. Splitting into Teams During the Physical Education hour, PD sir Mr. Sundar has decided to conduct some team games. He wants to split the students in the class into equal sized teams. In some cases, there may be some students who are left out from teams and he wanted to use the left out students to assist him in conducting the team games. For instance, if there are 50 students in the class and if the class has to be divided into 7 equal sized teams, 7 students will be there in each team and 1 student will be left out. PD sir asks your help to automate this team splitting task. Can you please help him out? Input Format: Input consists of 2 integers. The first integer corresponds to the number of students in the class and the second integer corresponds to the number of teams. Output Format: Refer sample input and output for formatting specifications. Sample Input and Output: [All text in bold corresponds to input and the rest corresponds to output] Enter the number of students in the class 60 Enter the number of teams 8 The number of students in each team is 7 and the number of students left out is 4 #include int main(){ int dividend, divisor; printf("Enter the number of students in the class"); scanf("%d",÷nd);
printf("Enter the number of teams"); scanf("%d",&divisor); printf("The number of students in each team is %d\n and the number of students left out is %d",dividend/divisor, dividend%divisor); return 0; } 2. Alice was bored that day,so she was sitting on the riverbank .Suddenly she notices a talking, White Rabbit with a pocket watch .It ran fast,and she followed it, down a rabbit hole .She fell into the hole and found a magical wonderland with dark trees, beautiful flowers.She found many ways numbered from 1,2,3,........18.she was confused which is the right way that will lead her to her home. She found a cute bird, standing in one of the tree. Alice asked the bird the way to go back to her home.The bird said a two digit number( say 23 ) and asked her to find the sum of its digits (2+3=5) and that numbered way will lead her to her home.Alice was already confused, so pls help Alice in finding the route to her home.... Input Format: Input consists of an integer corresponding to the 2-digit number. Output Format: Output consists of an integer corresponding to the sum of its digits. Refer sample input and output for formatting specifications.
[All text in bold corresponds to input and the rest corresponds to output] SAMPLE INPUT OUTPUT 1 : The bird said: 23 Alice must go in path-5 to find her way to home SAMPLE INPUT OUTPUT 2 : The bird said: 99
Alice must go in path-18 to find her way to home
#include int main() { int a,b,c,d; printf("The bird said:\n"); scanf("%d",&a); b=a%10; c=a/10; d=b+c; printf("Alice must go in path-%d to find her way to home”, d); return 0; } 3. The college ground is rectangular in shape. The Management decides to build a fence around the ground. In order to help the construction workers to build a straight fence, they planned to place a thick rope around the ground. They wanted to buy only the exact length of the rope that is needed. They also wanted to cover the entire ground with a thick carpet during rainy season. They wanted to buy only the exact quantity of carpet that is needed. They requested your help.
Can you please help them by writing a C program to find the exact length of the rope and the exact quantity of carper that is needed?
Input Format: Input consists of 2 integers. The first integer corresponds to the length of the ground and the second integer corresponds to the breadth of the ground.
Output Format:
Refer Sample Input and Output for exact formatting specifications.
Sample Input and Output: [All text in bold corresponds to input and the rest corresponds to output] Enter the length of the ground 50 Enter the width of the ground 20 The length of the rope needed is 140m The quantity of carpet needed is 1000sqm #include int main() { int a,b; printf("Enter the length of the ground\n"); scanf("%d",&a); printf("Enter the width of the ground\n"); scanf("%d",&b);
printf("The length of the rope needed is %dm\n", 2*(a+b)); printf("The quantity of carpet needed is %dsqm\n ", a*b); return 0; }
4. The shape of the college ground is Square. For the Independence day Flag Hoisting Function, it has been decided to place the flag post at the exact center of the ground. Can you please help them in placing the flag post at the exact center?
Given the coordinates of the left bottom vertex of the square ground and the length of the side, you need to write a program to determine the coordinates of the centre of the ground.
[Assumption --- Length of the side is always even]
Input Format: Input consists of 3 integers. The first integer corresponds to the x-coordinate of the left bottom vertex. The second integer corresponds to the y-coordinate of the left bottom vertex. The third integer corresponds to the length of the square.
Output Format: Refer Sample Input and Output for exact formatting specifications.
Sample Input and Output: [All text in bold corresponds to input and the rest corresponds to output] Enter the x-coordinate of the left bottom vertex 4 Enter the y-coordinate of the left bottom vertex O Enter the length of a side 8 The centre of the ground is at (8,4)
#include int main() { int a, b, c; printf("Enter the x-coordinate of the left bottom vertex\n"); scanf("%d", &a); printf("Enter the y-coordinate of the left bottom vertex\n"); scanf("%d", &b); printf("Enter the length of a side\n"); scanf("%d", &c); a = c / 2 + a; b = c / 2 + b; printf("The centre of the ground is at (%d,%d)\n", a, b); return 0; } 5. The mathematics teacher has recently taught the formula for computing the distance between 2 points to her students. She gave them an asssignment on calculating the distance between the points. In order to prevent copying, she gave each student a different set of questions. But she found it very difficult to correct all 100 assignments. She seeks your help. Can you please help her out?
Given the vertices of a triangle ABC, determine the sides of the triangle AB, BC and AC. Can you write a C program to calculate the sides?
Input Format Input consists of 6 integers. The first 2 integers correspond to the x-coordinate and ycoordinate of vertex A. The next 2 integers correspond to the x-coordinate and y-coordinate of vertex B. The next 2 integers correspond to the x-coordinate and y-coordinate of vertex C.
Output Format: Refer Sample Input and Output for exact formatting specifications. [All floating pont values are displayed correct to 1 decimal place]
Sample Input and Output: [All text in bold corresponds to input and the rest corresponds to output]
Enter the x-coordinate of vertex A 5 Enter the y-coordinate of vertex A 10 Enter the x-coordinate of vertex B 10 Enter the y-coordinate of vertex B 10 Enter the x-coordinate of vertex C 8 Enter the y-coordinate of vertex C 4 Length of side AB is 5.0 Length of side BC is 6.3 Length of side AC is 6.7
#include #include void main() { float distance1, distance2, distance3; int x1, y1, x2, y2, x3, y3; int d1, d2, d3, d4, d5, d6; printf("Enter the x-coordinate of vertex A\n"); scanf("%d",&x1); printf("Enter the y-coordinate of vertex A\n"); scanf("%d",&y1);
printf("Enter the x-coordinate of vertex B\n"); scanf("%d",&x2); printf("Enter the y-coordinate of vertex B\n"); scanf("%d",&y2); printf("Enter the x-coordinate of vertex C\n"); scanf("%d",&x3); printf("Enter the y-coordinate of vertex C\n"); scanf("%d",&y3); d1 = x2-x1; d2 = y2-y1; d3 = x3-x2; d4 = y3-y2; d5 = x3-x1; d6 = y3-y1; distance1=sqrt(d1*d1+d2*d2); distance2=sqrt(d3*d3+d4*d4); distance3=sqrt(d5*d5+d6*d6); printf("Length of side AB is %.1f\n", distance1); printf("Length of side BC is %.1f\n", distance2); printf("Length of side AC is %.1f\n", distance3); }
6. Emily is a very popular Maths Teacher in School. She retires from her service today. Her 7th class students like her very much and they wanted to give her a grand farewell at school. The school HeadMistress is a very strict person and she didn't give permission for them to conduct the farewell in the school premises. The students decided to conduct the farewell at Emily Mam's house itself. They know the street in which Emily mam lives. The student leader asked Emily Mam to tell her house number. Emily Mam's last class was on Equation for a straight line. She said that a straight line can be represented by the equation y=mx+c and you know how to find the xintercept and y-intercept of the line and my house number is the sum of the x-intercept and y-intercept of the line. The students were puzzled. Can you help the students find the house number of Emily Mam? Given the values of m and c of the line equation y=mx+c, write a C program to find the sum of x-intercept and y-intercept. Input Format:
Input consists of 2 integers. The first integer corresponds to m and the second integer corresponds to c. Output Format: Refer Sample Input and Output for exact formatting specifications. [Assume that the inputs are such that the intercept values are always integers] Sample Input and Output: [All text in bold corresponds to input and the rest corresponds to output] Enter the value of m 5 Enter the value of c 10 The line equation is y=5x+10 The x intercept is -2 The y intercept is 10 The house number is 8 #include int main() { int a,b; printf("Enter the value of m\n"); scanf("%d",&a); printf("Enter the value of c\n"); scanf("%d",&b); printf("The line equation is y=%dx+%d\n",a,b); printf("The x intercept is %d\n",-b/a); printf("The y intercept is %d\n",b); printf("The house number is %d\n",b+(-b/a)); return 0; } Ajay, Binoy and Chandru were very close friends at school. They were very good in Mathematics and they were the pet students of Emily Mam. Their gang was known as 3idiots. Ajay, Binoy and Chandru live in the same locality. A new student Dinesh joins their class and he wanted to be friends with them. He asked Binoy about his house address. Binoy wanted to test Dinesh's mathematical skills. Binoy told Dinesh that his house is at the midpoint of the line joining Ajay's house and Chandru's house. Dinesh was puzzled. Can you help Dinesh out? Given the coordinates of the 2 end points of a line (x1,y1) and (x2,y2), write a C program to find the midpoint of the line.
Input Format: Input consists of 4 integers. The first integer corresponds to x1 . The second integer corresponds to y1. The third and fouth integers correspond to x2 and y2 respectively. Output Format: Refer Sample Input and Output for exact formatting specifications. [All floating point values are displayed correct to 1 decimal place] Sample Input and Output: [All text in bold corresponds to input and the rest corresponds to output] Enter x1 2 Enter y1 4 Enter x2 10 Enter y2 15 Binoy's house is located at (6.0 , 9.5)
#include #include int main() { int x1, y1, x2, y2; float a,b; int c=2; printf("Enter x1\n"); scanf("%d",&x1); printf("Enter y1\n"); scanf("%d",&y1); printf("Enter x2\n"); scanf("%d",&x2); printf("Enter y2\n"); scanf("%d",&y2); a=x2+x1; b=y2+y1; printf("Binoy’s house is located at (%.1f , %.1f)", a/c,b/c); return 0; }
7. Dinesh also joined the group of 3 idiots and now their group is called Four Seasoners. Meanwhile, Binoy has moved to a new house in the same locality. Now the houses of Ajay, Binoy and Chandru are in the located in the shape of a triangle. Dinesh also has moved to a house in the same locality. When Ajay asked Dinesh about the location of his house , Dinesh said that his house is equidistant from the houses of the other 3. Though Ajay was good in Mathematics, he was puzzled. Can you please help Ajay out?
Given the 3 vertices {(x1,y1), (x2,y2) and (x3,y3)} of a triangle, write a C program to determine the point which is equidistant from all the 3 vertices. Input Format: Input consists of 6 integers. The first integer corresponds to x1 . The second integer corresponds to y1. The third and fouth integers correspond to x2 and y2 respectively. The fifth and sixth integers correspond to x3 and y3 respectively. Output Format: Refer Sample Input and Output for exact formatting specifications. [All floating point values are displayed correct to 1 decimal place] Sample Input and Output: [All text in bold corresponds to input and the rest corresponds to output] Enter x1 2 Enter y1 4 Enter x2 10 Enter y2 15 Enter x3 5 Enter y3 8 Dinesh’s house is located at (5.7 , 9.0)\ #include int main() { int x1,x2,x3,y1,y2,y3; printf(“Enter x1”); scanf(“%d”,&x1); printf(“\nEnter y1”); scanf(“%d”,&y1); printf(“\nEnter x2”); scanf(“%d”,&x2); printf(“\nEnter y2”); scanf(“%d”,&y2); printf(“\nEnter x3”); scanf(“%d”,&x3); printf(“\nEnter y3”); scanf(“%d”,&y3); printf(“\nDinesh’s house is located at (%.1f , %.1f)”,(x1+x2+x3)/3.0,(y1+y2+y3)/3.0); return 0; } 8.PROFIT CALCULATOR Each Sunday, a newspaper agency sells x copies of a certain newspaper for Rs.a per copy. The cost to the agency of each newspaper is Rs.b . The agency pays a fixed cost for storage, delivery and so on of Rs.100 per Sunday. The newspaper agency wants to calculate the profit obtained on sundays. Can you please help them out by writing a C program to compute the profit given x, a and b.
Input Format: Input consists of 3 integers --- x, a and b. X is the number of copies sold, a is the cost per copy and b is the cost the agency spends per copy. Output Format: Refer Sample Input and Output for exact formatting specifications. Sample Input and Output: [All text in bold corresponds to input and the rest corresponds to output] Enter the number of copies sold 1000 Enter the cost of 1 copy of the newspaper 2 Enter the cost spent by the agency on 1 copy of the newspaper 1 The profit obtained is Rs.900 #include int main() { int a,b,c,x; printf("Enter the number of copies sold"); scanf("%d",&a); printf("\nEnter the cost of 1 copy of the newspaper"); scanf("%d",&b); printf("\nEnter the cost spent by the agency on 1 copy of the newspaper"); scanf("%d",&c); x=(a*b)-(a*c)-100; printf("\nThe profit obtained is Rs.%d",x); return 0; } 9.A grocer has a sale of Rs. s1, Rs. s2, Rs. s3, Rs. s4 and Rs. s5 for 5 consecutive months. How much sale must he have in the sixth month so that he gets an average sale of Rs. x? Write a C program to compute the sale in the 6th month. Input Format: Input consists of 5 integers and 1 float. The five integers correspond to s1, s2, s3, s4 and s5. The float input corresponds to x. Output Format: Refer sample input and output for formatting specifications. The float values are displayed correct to 2 decimal places. Sample Input and Output: [All text in bold corresponds to input and the rest corresponds to output] Enter sale in first month 6435 Enter sale in second month
6927 Enter sale in third month 6855 Enter sale in fourth month 7230 Enter sale in fifth month 6562 Enter the average sales in 6 months 6500 The sale in the sixth month is Rs.4991.00 #include int main() { float avg,m1,m2,m3,m4,m5; printf("Enter sale in first month"); scanf("%f",&m1); printf("\nEnter sale in second month"); scanf("%f",&m2); printf("\nEnter sale in third month"); scanf("%f",&m3); printf("\nEnter sale in fourth month"); scanf("%f",&m4); printf("\nEnter sale in fifth month"); scanf("%f",&m5); printf("\nEnter the average sales in 6 months"); scanf("%f",&avg); printf("\nThe sale in the sixth month is Rs.%.2f",(6*avg-(m1+m2+m3+m4+m5))); return 0; }
Conditional statements 1. Write a program to find whether a given integer is an odd number or even number. Input Format: Input consists of a single integer. Output Format: Output consists of a single line. Refer sample output for the format. Sample Input 1: 3 Sample Output 1: 3 is an odd number Sample Input 2: 44 Sample Output 2: 44 is an even number #include int main() { int n; scanf("%d",&n); if(n%2==0) printf("%d is an even number",n); else printf("%d is an odd number",n); return 0; } 2. Write a program to find whether a given number is divisible by both 7 and 3. Input Format: Input consists of a single integer. Output Format: Output consists of a single line. Refer sample output for the format. Sample Input 1: 21 Sample Output 1 : 21 is divisible by both 7 and 3 Sample Input 2: 18 Sample Output 2: 18 is not divisible by both 7 and 3 #include int main() { int n; scanf("%d",&n); if(n%7==0 && n%3==0) printf("%d is divisible by both 7 and 3",n);
else printf("%d is not divisible by both 7 and 3",n); return 0; } 3. divisible by 2 or 3 #include int main() { int n; scanf("%d",&n); if(n%2==0 || n%3==0) printf("yes"); else printf("no"); return 0; }
4. maximum of 2 #include int main() { int a,b; scanf("%d%d",&a,&b); if(a>b) printf("%d is the maximum number",a); else printf("%d is the maximum number",b); return 0; }
5. maximum of 3 #include int main() { int a,b,c; scanf("%d%d%d",&a,&b,&c); if(a>b&&a>c) printf("%d is the maximum number",a); else if(b>a&& b>c) printf("%d is the maximum number",b); else printf("%d is the maximum number",c);
return 0; } 6. leap year
#include int main() { int n; scanf("%d",&n); if(n%400==0) printf("%d is a leap year",n); else if (n%100==0) printf("%d is not a leap year",n); else if (n%4==0) printf("%d is a leap year",n); else printf("%d is not a leap year",n); return 0; } 7. calculate grade #include int main() { int x,y,z,w; scanf("%d%d%d",&x,&y,&z); w=(x+y+z)/3; if(w>=90) printf("The grade is A"); else if(w=80) printf("The grade is B"); else if (w=70) printf("The grade is C"); else if(w=60) printf("The grade is D"); else if (w=50) printf("The grade is E"); else printf("The grade is F"); return 0; }
8. character upper or lower #include #include int main() { char s; scanf("%c",&s); if(isalpha(s)) { if(islower(s)) printf("%c is lowercase letter",s); else printf("%c is uppercase letter",s); } else printf("%c is neither an uppercase or lowercase letter",s); return 0; } 9. in or out #include int main() { int n,s; printf("Enter the number of problems given"); scanf("%d",&n); printf("\nEnter the number of problems solved"); scanf("%d",&s); if(s>n/2) printf("\nIN"); else printf("\nOUT"); return 0; } 10. new or old #include int main() { int n,m; printf("Enter the year of establishment of college 1");
scanf("%d",&n); printf("\nEnter the year of establishment of college 2"); scanf("%d",&m); if(n=sp || n>= tp) { if(fpb) printf("\nCollege 1 is better"); else printf("\nCollege 2 is better"); return 0; }
16. y2k problem
#include int main() { int b,c,x; printf("Enter Year of Birth"); scanf("%d",&b); printf("\nEnter Current year"); scanf("%d",&c); if(c>b) { x=c-b; printf("\nYour age is %d",x); } else { c+=100; x=c-b; printf("\nYour age is %d",x); } return 0; } 17. months
#include int main() { int m; scanf("%d",&m); switch(m) { case 1:printf("January\n");
break; case 2:printf("February\n"); break; case 3:printf("March\n"); break; case 4:printf("April\n"); break; case 5:printf("May\n"); break; case 6:printf("June\n"); break; case 7:printf("July\n"); break; case 8:printf("August\n"); break; case 9:printf("September\n"); break; case 10:printf("October\n"); break; case 11:printf("November\n"); break; case 12:printf("December\n"); break; default :printf("Invalid month\n"); } return 0; }
18. basic calculator #include int main() { int a,b; char c; scanf("%d\n",&a); scanf("%c\n",&c); scanf("%d",&b); switch(c) { case '+' :printf("The sum is %d",a+b); break;
case '-' :printf("The difference is %d",a-b); break; case '*' :printf("The product is %d",a*b); break; case '/' :printf("The quotient is %d",a/b); break; case '%':printf("The remainder is %d",a%b); break; default : printf("Invalid Input"); } return 0; }
19. fees calculator #include int main() { char d; int p,f,a,af,tot; scanf("%c",&d); scanf("%d%d%d%d",&p,&f,&a,&af); switch(d) { case 'A': tot=(p*f)+(a*af); printf("The fee to be paid is Rs.%d",tot); break; case 'B': tot=((p*f)+(a*af))+5000; printf("The fee to be paid is Rs.%d",tot); break; case 'C': tot=((p*f)+(a*af))+1500; printf("The fee to be paid is Rs.%d",tot); break; case 'D': tot=((p*f)+(a*af))+6500; printf("The fee to be paid is Rs.%d",tot); break; }
return 0; }
20. hotel tariff #include int main() { int m; float r,d,tar; scanf("%d",&m); scanf("%f%f",&r,&d); switch(m) { case 1: tar=r*d; printf("Hotel Tariff: Rs.%.2f\n",tar); break; case 2: tar=r*d; printf("Hotel Tariff: Rs.%.2f\n",tar); break; case 3: tar=r*d; printf("Hotel Tariff: Rs.%.2f\n",tar); break; case 4: tar=(r*d)+2*((r*d)/10); printf("Hotel Tariff: Rs.%.2f\n",tar); break; case 5: tar=(r*d)+2*((r*d)/10); printf("Hotel Tariff: Rs.%.2f\n",tar); break; case 6:tar=(r*d)+2*((r*d)/10); printf("Hotel Tariff: Rs.%.2f\n",tar); break; case 7:tar=r*d; printf("Hotel Tariff: Rs.%.2f\n",tar); break; case 8:tar=r*d; printf("Hotel Tariff: Rs.%.2f\n",tar); break;
case 9:tar=r*d; printf("Hotel Tariff: Rs.%.2f\n",tar); break; case 10:tar=r*d; printf("Hotel Tariff: Rs.%.2f\n",tar); break; case 11: tar=(r*d)+2*((r*d)/10); printf("Hotel Tariff: Rs.%.2f\n",tar); break; case 12: tar=(r*d)+2*((r*d)/10); printf("Hotel Tariff: Rs.%.2f\n",tar); break; default :printf("Invalid Input\n"); } return 0; }
Conditional Statements Looping
1. print1 #include int main() { int a,b,i; printf("Enter the value of a"); scanf("%d",&a); printf("\nEnter the value of b"); scanf("%d",&b); for(i=a;i=b;i--) printf("\n%d",i); return 0; }
3. print 3
#include int main() { int a,b,i; printf("Enter the value of a"); scanf("%d",&a); printf("\nEnter the value of b"); scanf("%d",&b); if(a>b) { for(i=a;i>=b;i--)
printf("\n%d",i); } else { for(i=a;i
View more...
Comments