Tuesday, August 16, 2011

C-Programming video lecture 2

C-Programming video lecture 3

C-Programming video lecture 1

Introduction to C Language

C program which asks the user for a number between 1 to 9 and shows the number. If the user inputs a number out of the specified range, the pr

Write a C program which asks the user for a number between 1 to 9 and shows the number. If the user inputs a number out of the specified range, the program should show an error and prompt
the user for a valid input ?
Program: Program for accepting a number in a given range.
#include
int getnumber();
int main() {
int input = 0;
//call a function to input number from key board
input = getnumber();
//when input is not in the range of 1 to 9,print error message
while (!((input <= 9) && (input >= 1))) {
printf("[ERROR] The number you entered is out of range");
//input another number
input = getnumber();
}
//this function is repeated until a valid input is given by user.
printf("\nThe number you entered is %d", input);
return 0;
}/
/this function returns the number given by user
int getnumber() {
int number;
//asks user for a input in given range
printf("\nEnter a number between 1 to 9 \n");
scanf("%d", &number);
return (number);
}
Output:
Enter a number between 1 to 9
45
[ERROR] The number you entered is out of range
Enter a number between 1 to 9
4
The number you entered is 4
Explanation:
getfunction() function accepts input from user. 'while' loop checks whether the number falls within range or not
and accordingly either prints the number(If the number falls in desired range) or shows error message(number is
out of range).

Program to display the multiplication table of a given number ?

program to display the multiplication table of a given number ?

Program: Multiplication table of a given number
#include
int main() {
int num, i = 1;
printf("\n Enter any Number:");

scanf("%d", &num);
printf("Multiplication table of %d: \n", num);
while (i <= 10) {
printf("\n %d x %d = %d", num, i, num * i);
i++;
}
return 0;
}
Output:
Enter any Number:5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
Explanation:
We need to multiply the given number (i.e. the number for which we want the multiplication table)
with value of 'i' which increments from 1 to 10.

Write a Program to find the square root of a given number

Write a Program to find the square root of a given number
#include
#include
float SquareRoot(float num);
void main()
{
float input, ans;
clrscr();
printf("\n Enter The Number : ");
scanf("%f", &input);
ans = SquareRoot(input);
printf("\n Square Root : %f", ans);
getch();
}

float SquareRoot(float num)
{
if(num >= 0)
{
float x = num;
int i;
for(i = 0; i < 20; i ++)
{
x = (((x * x) + num) / (2 * x));
}
return x;
}
}