PDF- basic c programs for beginners -Model Checking C Programs with MSVL - C Programs

Description

Program to Print Hello World

! #include int main() { printf("Hello World

Output : Hello Aliens

#include int main() { printf("Hello Aliens

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

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

#include int main () { char a[20],

printf("Enter the Creator Name :\n")

printf("Enter the Purpose :\n")

printf("I am the Robot named %s

printf("I am created for the purpose of %s

printf("My memory space is around %dGb and my speed is %

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 ,

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,

printf("Enter the number of people dead:\n")

printf("Enter the number of people injured:\n")

printf("Enter the number of people safe:\n")

printf("TSUNAMI REPORT OF JAPAN\n")

printf("The number of people\n")

printf("Please help the people who are suffering

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,

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,

printf("Enter the number of students in the class")

printf("Enter the number of teams")

printf("The number of students in each team is %d\n and the number of students left out is %d",dividend/divisor,

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,

She fell into the hole and found a magical wonderland with dark trees,

She found many ways numbered from 1,2,3,

she was confused which is the right way that will lead her to her home

She found a cute bird,

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("Alice must go in path-%d to find her way to home”,

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")

printf("Enter the width of the ground\n")

printf("The length of the rope needed is %dm\n",

2*(a+b))

printf("The quantity of carpet needed is %dsqm\n ",

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

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)

printf("Enter the x-coordinate of the left bottom vertex\n")

printf("Enter the y-coordinate of the left bottom vertex\n")

printf("Enter the length of a side\n")

printf("The centre of the ground is at (%d,%d)\n",

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

#include #include void main() { float distance1,

printf("Enter the x-coordinate of vertex A\n")

printf("Enter the y-coordinate of vertex A\n")

printf("Enter the x-coordinate of vertex B\n")

printf("Enter the y-coordinate of vertex B\n")

printf("Enter the x-coordinate of vertex C\n")

printf("Enter the y-coordinate of vertex C\n")

printf("Length of side AB is %

printf("Length of side BC is %

printf("Length of side AC is %

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

printf("Enter the value of m\n")

printf("Enter the value of c\n")

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))

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

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

#include #include int main() { int x1,

printf("Binoy’s house is located at (%

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,

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

printf(“\nDinesh’s house is located at (%

PROFIT CALCULATOR Each Sunday,

a newspaper agency sells x copies of a certain newspaper for Rs

The cost to the agency of each newspaper is Rs

The agency pays a fixed cost for storage,

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,

Input Format: Input consists of 3 integers

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

printf("Enter the number of copies sold")

printf("\nEnter the cost of 1 copy of the newspaper")

printf("\nEnter the cost spent by the agency on 1 copy of the newspaper")

printf("\nThe profit obtained is Rs

A grocer has a sale of Rs

How much sale must he have in the sixth month so that he gets an average sale of Rs

? 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,

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

printf("Enter sale in first month")

printf("\nEnter sale in second month")

printf("\nEnter sale in third month")

printf("\nEnter sale in fourth month")

printf("\nEnter sale in fifth month")

printf("\nEnter the average sales in 6 months")

printf("\nThe sale in the sixth month is Rs

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

if(n%2==0) printf("%d is an even number",n)

else printf("%d is an odd number",n)

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

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)

divisible by 2 or 3 #include int main() { int n

if(n%2==0 || n%3==0) printf("yes")

maximum of 2 #include int main() { int a,b

if(a>b) printf("%d is the maximum number",a)

else printf("%d is the maximum number",b)

maximum of 3 #include int main() { int 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)

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)

calculate grade #include int main() { int x,y,z,w

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")

character upper or lower #include #include int main() { char 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)

in or out #include int main() { int n,s

printf("Enter the number of problems given")

printf("\nEnter the number of problems solved")

new or old #include int main() { int n,m

printf("Enter the year of establishment of college 1")

printf("\nEnter the year of establishment of college 2")

if(n=sp || n>= tp) { if(fpb) printf("\nCollege 1 is better")

else printf("\nCollege 2 is better")

#include int main() { int b,c,x

printf("\nEnter Current year")

switch(m) { case 1:printf("January\n")

default :printf("Invalid month\n")

basic calculator #include int main() { int a,b

switch(c) { case '+' :printf("The sum is %d",a+b)

case '-' :printf("The difference is %d",a-b)

case '*' :printf("The product is %d",a*b)

case '/' :printf("The quotient is %d",a/b)

case '%':printf("The remainder is %d",a%b)

default : printf("Invalid Input")

fees calculator #include int main() { char 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

case 'B': tot=((p*f)+(a*af))+5000

printf("The fee to be paid is Rs

case 'C': tot=((p*f)+(a*af))+1500

printf("The fee to be paid is Rs

case 'D': tot=((p*f)+(a*af))+6500

printf("The fee to be paid is Rs

hotel tariff #include int main() { int m

2f\n",tar)

2f\n",tar)

2f\n",tar)

case 4: tar=(r*d)+2*((r*d)/10)

2f\n",tar)

case 5: tar=(r*d)+2*((r*d)/10)

2f\n",tar)

2f\n",tar)

2f\n",tar)

2f\n",tar)

2f\n",tar)

2f\n",tar)

case 11: tar=(r*d)+2*((r*d)/10)

2f\n",tar)

case 12: tar=(r*d)+2*((r*d)/10)

2f\n",tar)

default :printf("Invalid Input\n")

Conditional Statements Looping

print1 #include int main() { int a,b,i

printf("Enter the value of a")

printf("\nEnter the value of b")

#include int main() { int a,b,i

printf("Enter the value of a")

printf("\nEnter the value of b")