Posts

Showing posts with the label chapter 2 C programming

Difference on Break and Continue Statement in C

Image
 

Program to decide whether a given number is positive, negative or zero number.

Image
Here, user will input a number and program will assign it in variable 'a' using scanf. After that, the value inside variable is checked, whether it is greater than 0 or not and if it is greater than 0 then inputted number is positive and if it is less than 0, the inputted number is negative. But if these two condition is not satisfied then the inputted number must be equal to 0.  Output  

Program 5: Find Simple Interest from given principal,time and rate.

Image
#include<stdio.h> #include<conio.h> void main() { clrscr(); int p=1000; int t=2; int r=3; float si; si=p*t*r/100; printf("Simple Interest=%.2f",si); getch(); } Output Simple Interest=60.00 Explanation Here, p is a principal, t is time and r is a rate variable and the value is already assigned on them and are declared as integer. si denotes to simple interest that stores floating value. After applying formula in the process section, the output is printed. To bring the result in two decimal value we kept .2f specifier. Exercise Find the errors in the following program #include<studio.h> #include<conio.h> void main() { integer a=30; integer b=40; integer c=60; float d; d=aXbXc/3 print("The average marks is=%d",d); getch(); }

Program 4 : Program to find the Area and Perimeter of a Rectange.

#include<stdio.h> #include<conio.h> void main() { clrscr(); int l=20, b=30; float a,p; a=l*b; p=2*(l+b); printf("Area of rectangle=%.2f", a); printf("\nPerimeter of rectangle=%.2f", p); getch(); } Output Area of rectangle=600.00 Perimeter of rectangle=100.00 Explanation "l" denotes to length and "b" denotes to breadth. Applying the mathematical formula of Area i.e, a=l*b  and perimeter, p=2*(l+b) we got the answer and displayed by the specifier %.2f that will will show the result in two decimal value after the point. Here a and p are declared as floating number because it may store floating number.

Program 3: Show addition, Subtraction, Multiplication, Division and Remainder of any two numbers

#include<stdio.h> #include<conio.h> void main() { clrscr(); int a=6; int b=5; int c,d,e,f; c=a+b; d=a-b; e=a*b; f=a/b; g=a%b; printf("addition=%d",c); printf("\nsubtraction=%d",d); printf("\nmultiplication=%d",e); printf("\ndivision=%d",f); printf("\nremainder=%d",g); getch(); } Output addition=11 subtraction=1 multiplication=30 division=1 remainder=1 Explanation Here two numbers are assigned in varriable a and b and other varriables c,d,e,f and g are declared for storing the results of addition, subtraction, multiplication, division and remainder respectively. "%" is used to find remainder. "%d" is a specifier of integer. Here "\n" is a specifier that is used to print the text in new line. Exercise:  Find out  simple interest where principal is 2000, time is 3 years and rate is 2% using a C program.

Program 2- Program to add two numbers

Image
#include<stdio.h> #include<conio.h> void main() { clrscr(); int a=5; int b=7; int c; c=a+b; printf("Addition=%d", c); getch(); } Output Addition=12 Explanation int denotes that variable a can store only integer number so number 5 is provided to it. Varriable c is declared as integer and addition of the number a and b is now placed to c for mathematical calculation. Now to print the result, we have given %d as specifier of interger that holds the value of c. Exercise: Make a program that add 3 numbers.

Program 1- To Print "Welcome To Bit"

Image
#include<stdio.h> #include<conio.h> void main() { clrscr(); printf("Welcome To Bit"); getch(); } Output Welcome To Bit Explanation: #include<stdio.h> and #include<conio.h> is a header file and should be written in the same manner, beginning with #include and written in angular bracket "<" , ">". Here stdio.h defines printf to perform its function and conio.h defines a compiler inorder to translate C program to machine language program. Exercise: Write a program to print "Welcome To Bit" five times.  hint: just use printf("Welcome to Bit") for five times in the block of C program.