C HOW TO PROGRAM

12
Jonas Kgomo | CENG | April 4, 2013 C HOW TO PROGRAM STRUCTURE,PROGRAM CONTROL AND FUNCTIONS

Transcript of C HOW TO PROGRAM

Jonas Kgomo | CENG | April 4, 2013

C HOW TO PROGRAM STRUCTURE,PROGRAM CONTROL AND FUNCTIONS

PAGE 1

1 Write a program that prints “Welcome to C”

#include<stdio.h>

int main() {

printf(“ Welcome to C ”);

return 0;

}

2 Add two numbers from user, calculate their sum

#include<stdio.h>

int main() {

int num1,num2,sum; /*numbers input by user and sum*/

printf(“ Enter first number \n”); */prompt*/

scanf(“%d”,&num1);

printf(“ Enter second number \n”);

scanf(“%d”,&num2);

sum=num1+num2;

printf(“Sum is %d ”,sum);

return 0;

}

3 Compare two number given by user ,say whether one number is equal ,less

than or greater than the other

Here we use if statements, relational and equality operators

If a=b (a==b) → else if a≥b(a>=b) → else if(a<=b)

PAGE 2

#include<stdio.h>

int main() {

int num1,num2;

printf(“ enter two numbers and I will tell you \n”);

printf(“ the relationship they satisfy \n”);

scanf(“%d%d”,&num1,&num2);

if(num1==num2)

{

printf(“%d is equal to %d\n”,num1,num2);

}

else if(num1<=num2)

{

printf(“%d is less than %d\n”,num1,num2);

}

else if(num1>=num2)

{

printf(“%d is greater than %d\n”,num1,num2);

}

else {

printf(“please enter real numbers! \n”);

}

return 0;

}

4 If a student gets more than or equal 60 print PASSED or FAILED if student

gets less than 60

If (grade>=60){

printf(“PASSED/n”);

else {

printf(“FAILED/n”);

}

5 Use while repetation,to print out the first power of 3 less than a 100

int product;

product =3;

PAGE 3

while (product<=100)

{

product = 3*product;

}

printf(“the first number is %d ”,);

LAB 1

1 Write a program to find the largest of three numbers

int x,y,z;

printf("enter 3 numbers:\n");

scanf("%d%d%d”,&x,&y,&z);

if(x>y && x>z ){

printf("%d is the maximum",x);}

else if(y>x && y>z){

printf("%d is the maximum",y);}

else if(z>x && z>y){

printf("%d is the maximum",z);}

else

printf("either two or three numbers are equal ");

2 Write a program that takes a student’s midterms and final results as an

integer, then calculates their average grade with a precision of two decimal

places.

if average is greater than 80 print Congrulations,otherwise print You need study hard. (The respective weights of midterms and final are 20%, 30% and 50%) Output should be like this:

Midterm-1 = 30

Midterm-2 = 40

PAGE 4

Final = 50

Average = 30*0.25+40*0.25+50*0.50= 42.50

#include<stdio.h>

int main(){

int mid1,mid2,final;

float average;

printf("enter midterm-1 score:");

scanf("%d",&mid1);

printf("enter a midterm-2 score:");

scanf("%d",&mid2);

printf("enter a final:");

scanf("%d",&final);

average=(mid1*0.2+mid2*0.3+final*0.5);

printf("Your average is %.2f",average);/*.2 here means 2 decimal place /*

if(average>=80)

printf("Congratulations");

else

printf(" You need to study hard");

return 0;

}

PAGE 5

3 Consider the function

Calculate the partial function below:

𝑓(𝑥) = {

𝑥 + 1, 𝑖𝑓 60 ≤ 𝑥 ≤ 100 50, 𝑖𝑓 𝑥 = 20𝑥 − 1, 𝑖𝑓 𝑥 = 0

0 , 𝑒𝑙𝑠𝑒

Example: Enter a value: 20 f(20) = 50 since x=20 2nd condition likewise f(84)=84+1=85 1st condition #include<stdio.h> int main(){ int x; printf("enter the value of x is "); scanf("%d",&x); if(x>=60 && x <=100) printf("The value of x is %d ",x+1); else if(x==20) printf("The value of x is %d ",50); else if(x==0) printf("The value of x is %d ",x-1); else printf("The value of x is %d ",0); return 0; }

5 Write program finds area of some geometric shape . Print a menu first

then read the choice.

Find Area of

1-Triangle

2-Square

3-Circumferences

4-Rectanglue.

Choose number : 1

Enter edge of square : 5

Area : 25

PAGE 6

#include<stdio.h>

int main(){

int x,a,b;

printf("Find area of geometric shape by choosing a number\n1 TRIANGLE\n2

SQUARE\n3 CIRCUMFERENCE\n4 RECTANGLE\n ");

scanf("%d",&x);

if (x==1)

{

printf("enter base and height of triangle \n");

scanf("%d\n",&a);

scanf("%d",&b);

printf("Area of triangle :%d ",((a*b)/2));

}

else if(x==2)

{

printf("enter side of square \n");

scanf("%d",&a);

printf("Area of square :%d ",a*a);

}

else if(x==3)

{

printf("enter radius of circle \n");

scanf("%d",&a);

printf("Circumference is %d \n",6*a);

}

else if(x==4){

printf("enter base and width rectangle \n");

scanf("%d\n",&a);

scanf("%d",&b);

printf("Area of triangle :%d ",a*b);

}

else

printf("oops!! geometry excedeed please enter a corresponing number to

the shape");

return 0;

}

PAGE 7

6 Write a program that prints a point, line, triangle, square, and pentagon

according to the choice of user. Print a menu first then read the choice. 1 point

2 line

3 triangle

4 square

5 pentagon

Enter your Choice: 2 ************** #include<stdio.h> int main(){ int x; printf("choose number corresponding to the\n name you would like to display\n1 POINT\n2 LINE\n3 TRIANGLE\n4 SQUARE\n5PENTAGON\n "); scanf("%d",&x); if(x==1) printf("*"); if(x==2){ printf("**********");} if(x==3){ printf(" *\n"); printf(" ***\n"); printf("*****\n"); }

if(x==4){

printf("****\n"); printf("* *\n"); printf("* *\n"); printf("****");} if(x==5) printf(" * * *\n"); printf(" * * \n"); printf("* *\n"); printf("* *\n"); printf("* *\n"); printf(" * *\n"); printf(" * * *");

return 0;

}

PAGE 8

LAB 2

1 Write a program that takes an integer from the user, checks if it is positive

or negative and displays a message accordingly. (Here, ZERO should be

considered as a positive number.

#include<stdio.h>

int main(){

int x;

printf("enter a to know its sign");

scanf("%d",&x);

if(x>0 || x==0)

printf("the number %d is positive",x);

else

printf("the number %d is negetvie",x);

return 0;

}

2 Write a program that takes the score from the student and checks if it is

greater or equal to, or smaller than 60, and displays different messages

according to this example:

Ex. 1 Ex. 2 Please enter your grade: 65 Please enter your grade: 55 You passed! You failed! Congratulations! You should retake this course.

#include<stdio.h>

int main(){

int x;

printf("Please enter your grade ");

scanf("%d",&x);

if(x>=60 && x<=100)

PAGE 9

printf("You passed!\n\Congradulations");

else if (x<60)

printf("You failed!\nYou should retake this course");

else

printf("The score not recognised");

return 0;

}

3 Write a program that takes an integer from user and assigns it to variable x.

Calculate y according to the following equation and display it on screen:

𝑦 =𝑥 − 3

𝑥+ 𝑥 × 4

Your program should check if x is 0 and if so y should not be evaluated due to divide by zero problem and you

should display print “Value should not be 0”. Test the program with positive and negative values and with 0 to

see if it prints the correct output.

#include<stdio.h>

int main(){

printf("enter a number ");

double x,y;

scanf("%lf",&x);

if(x==0){

printf("the value of y is undefined,x should not be 0");

}

else{

y=(x-3)/x+(4*x);

printf("the value of y is :%lf ",y);

}

return 0;

}

PAGE 10

4 Write a program that takes two integers from the user and finds out if the

first number is a multiple of the second.

Hint: Use modulus(%) operator. Example Please enter two integers: Please enter two integers:

12 24 24 12

12 is not a multiple of 24. 24 is a multiple of 12.

#include<stdio.h> int main(){ int x,y; printf("Please enter two integers : /n"); scanf("%d%d",&x,&y); if (x%y==0) printf("%d is a multiple of %d",x,y); else printf("%d is not multiple of %d",x,y); return 0;}

5 Add an ordinal suffix to the number entered by user. If user enters 1, add “st”

and print “1st”. User can enter numbers between 1 and 20.

#include<stdio.h>

int main(){

int x;

printf("enter a number ");

scanf("%d",&x);

if(x==1&& x<=20)

printf("%dst",x);

else if(x==2)

printf("%dnd",x);

else if(x==3)

printf("%drd",x);

else if(x>=4&& x<=20)

printf("%dth",x);

else

printf("the ordinal suffix is complex for us");

return 0;

PAGE 11

NB

Please note that this booklet is not a supplement of the lecture nor the

textbook, it is just a reference to programmers and teachers.Basic

introductions of some obvious codes have been omitted on purpose,example

#include<stdio.h>

int main(){

}

Any errors noticed or malfunction should not be ignored but rather bookmarked

to improve the publishing process of the booklet.

© copyright reserved

2013 this copy can be plagiarised by reputable institutions...