3
EAST WEST UNIVERSITY Department of Electronics & Communications Engineering Output Semester: Fall 2014 ETE 105: Computer Fundamentals and Programming Language Lecture 5: Simple C Programs with Arithmetic Operators Example1: Write the output of the following program: #include<stdio.h> int main() { printf("The color: %s\n", "blue"); printf("First number: %d\n", 12345); printf("Second number: %04d\n", 25); printf("Third number: %i\n", 1234); printf("Float number: %3.2f\n", 3.14159); printf("Unsigned value: %u\n", 150); printf("Just print the percentage sign %%\n", 10); return 0; } Example 2: Write a C Program that reads in three float point numbers from the user and displays the sum of the numbers. #include<stdio.h> int main () { float x, y, z, sum; printf("Enter three numbers:\n"); scanf("%f %f %f", &x, &y, &z); sum=x+y+z; printf("The sum of the number is: %5.2f", sum); return 0; }

Ete105 fall2014 lec5 (East West University)

Embed Size (px)

DESCRIPTION

East West University ETE lecture for all.

Citation preview

Page 1: Ete105 fall2014 lec5 (East West University)

EAST WEST UNIVERSITY

Department of Electronics & Communications Engineering

Output

Semester: Fall 2014

ETE 105: Computer Fundamentals and Programming Language

Lecture 5: Simple C Programs with Arithmetic Operators

Example1: Write the output of the following program:

#include<stdio.h>

int main()

{

printf("The color: %s\n", "blue");

printf("First number: %d\n", 12345);

printf("Second number: %04d\n", 25);

printf("Third number: %i\n", 1234);

printf("Float number: %3.2f\n", 3.14159);

printf("Unsigned value: %u\n", 150);

printf("Just print the percentage sign

%%\n", 10);

return 0;

}

Example 2: Write a C Program that reads in three float point numbers from the user and displays

the sum of the numbers.

#include<stdio.h>

int main ()

{

float x, y, z, sum;

printf("Enter three numbers:\n");

scanf("%f %f %f", &x, &y, &z);

sum=x+y+z;

printf("The sum of the number is: %5.2f", sum);

return 0;

}

Page 2: Ete105 fall2014 lec5 (East West University)

EAST WEST UNIVERSITY

Department of Electronics & Communications Engineering Example 3: The simple interest is calculated can found using the formula:

interest = principle×rate×time.

Write a C Program to calculate the simple interest if principal, rate and time is entered.

#include <stdio.h>

int main()

{

float p, r, si;

int t;

printf("Enter the values of principle, rate of interest (in %%)

and times (in years)\n");

scanf ("%f %f %d", &p, &r, &t);

si = (p * r * t)/ 100.0;

printf ("Amount = BDT %7.2f\n", p);

printf ("Rate = %4.2f%%\n", r);

printf ("Time = %d years\n", t);

printf ("Simple interest = %7.2f\n", si);

return 0;

}

Page 3: Ete105 fall2014 lec5 (East West University)

EAST WEST UNIVERSITY

Department of Electronics & Communications Engineering

Example 4: Write the output of the following program:

#include<stdio.h>

int main()

{

int i=8, j=5, k,l;

float x=0.5, y=-0.01;

char c='c', d='d';

k=(3*i-2*j);

l=2*((i/5)+(4*(j-3))%(i+j-2));

printf("k=%d\n", k);

printf("l=%d\n", l);

printf("i=%d\n", i++);

printf("j=%d\n", ++j);

printf("i=%d\n", --i);

/* For the following questions, TRUE=1, FALSE=0*/

printf("Is x less than or equal to y?\n %d\n", (x<=y));

printf("Is c greater than or equal to d?\n %d\n", (c>=d));

printf("!(x>0) True or False?\n %d\n", !(x>0));

printf("(i>0)||(j<5) True or False?\n %d\n", (i>0)||(j<5));

printf("(x>y)&&(i>0)&&(j<5) True or False?\n %d\n",

(x>y)&&(i>0)&&(j<5));

return 0;

}