Elementary Programs in C Part2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

GURUTOOL

Free Self Help Tutorial for AMIE Preparation

Designed by Michael Sony


AMIE, ME
Foreword
It is a well known fact that passing AMIE is a Herculean task. The reason for the
same is multi fold.
 An AMIE student gets just notes and they have to themselves learn every
thing.
 AMIE students are working, hence no much time to study
 In the absence of class room training no continuous motivation.
 C being the toughest aspect for AMIE students as most diploma students
are scared about the same.

Due to these things I have thought of giving free discussions on various subjects
of AMIE. Section A & Section B (Electrical only).I intend to teach the subjects on
Saturday and Sundays. Being an AMIE passed candidate I have thought of
helping my fellow brethren with it. I can be contacted on

Michael Sony
H.no 50/A, St.Pedro
Ribander Goa
403006
Tel:09881079529
Email:michaelsonygoa@gmail.com

My Best wishes for those who seek knowledge…..

Michael Sony
How to refer this Tutorial
This Assignments is designed for Computer and Informatics to
take care of C needs of AMIE students. Hence the no prerequisite
for these tutorials.

I intend to give you a compact and programming oriented approach


to C with hands on experience with some programs. Conceptually
it will meet the needs of students as a whole, but in addition to it, I
recommend to solve minimum five consecutive questions to get
the feel of programs asked.

A sequential reading is recommended for the first reading and


subsequently selective reading is recommended. But free of cost
contact program is recommended to understand these concepts. I
can be contacted for free of cost contact programs with prior
appointments.
EXERCISE B
Elementary Programs using Printf and scanf
1.#include <stdio.h>

main()
{
int sum = 50;
float modulus;

modulus = sum % 10;


printf("The %% of %d by 10 is %.2f\n", sum, modulus);
}

The % of 50 by 10 is 0.00

2. #include <stdio.h>

main()
{
int count = 0, loop;

loop = ++count; /* same as count = count + 1; loop = count; */


printf("loop = %d, count = %d\n", loop, count);
loop = count++; /* same as loop = count;
count = count + 1; */
printf("loop = %d, count = %d\n", loop, count);
}
3. #include <stdio.h>

main()
{
int sum, loop, kettle, job;
char whoknows;

sum = 9;
loop = 7;
whoknows = 'A';
printf( "Whoknows = %c, Kettle = %d\n", whoknows, kettle
);
}

4. Sample program illustrating use of scanf() to read integers, characters and floats
#include < stdio.h >

main()
{
int sum;
char letter;
float money;

printf("Please enter an integer value ");


scanf("%d", &sum );

printf("Please enter a character ");


/* the leading space before the %c ignores space characters in the
input */
scanf(" %c", &letter );

printf("Please enter a float variable ");


scanf("%f", &money );

printf("\nThe variables you entered were\n");


printf("value of sum = %d\n", sum );
printf("value of letter = %c\n", letter );
printf("value of money = %f\n", money );
}

TEST YOUR SKILLS

1. Use a printf statement to print out the value of the integer variable sum

2. Use a printf statement to print out the text string "Welcome", followed by a newline.

3. Use a printf statement to print out the character variable letter

4. Use a printf statement to print out the float variable discount

5. Use a printf statement to print out the float variable dump using two decimal places

6. Use a scanf statement to read a decimal value from the keyboard, into the integer
variable sum

7. Use a scanf statement to read a float variable into the variable discount_rate

8. Use a scanf statement to read a single character from the keyboard into the variable
operator. Skip leading blanks, tabs and newline characters.

Answers: Practise Exercise 3: printf() and scanf()


1. Use a printf statement to print out the value of the integer variable sum

printf("%d", sum);

2. Use a printf statement to print out the text string "Welcome", followed by a newline.

printf("Welcome\n");

3. Use a printf statement to print out the character variable letter


printf("%c", letter);

4. Use a printf statement to print out the float variable discount

printf("%f", discount);

5. Use a printf statement to print out the float variable dump using two decimal places

printf("%.2f", dump);

6. Use a scanf statement to read a decimal value from the keyboard, into the integer
variable sum

scanf("%d", &sum);

7. Use a scanf statement to read a float variable into the variable discount_rate

scanf("%f", &discount_rate);

8. Use a scanf statement to read a single character from the keyboard into the variable
operator. Skip leading blanks, tabs and newline characters.

scanf(" %c", &operator);


GURUTOOL
Free Self Help Tutorial for AMIE Preparation

Michael Sony
H.no 50/A
St.Pedro Ribander
Goa- 403006
Tel:-09881079529

You might also like