Monday, May 27, 2013

for loop

for loop

 
 A loop construct found in many procedural languages which repeatedly executes some instructions while a condition is true.
In C, the for loop is written in the form; 

for(INITIALISATION;CONDITION;INCRIMENTATON /DECREMENTATION )STATEMENT;
where INITIALISATION is an expression that is evaluated once before the loop, CONDITION is evaluated before each iteration and the loop exits if it is false, AFTER is evaluated after each iteration, and STATEMENT is any statement, including a compound statement within braces "
..", that is executed if CONDITION is true.
Syntex:

int i;
 for (i = 0; i < 10; i++);
        (INITIALISATION;CONDITION;INCRIMENTATON /DECREMENTATION )
 printf("Hello\n");

prints "Hello" 10 times.

Friday, May 17, 2013

if else if

if else if


An if statement can be followed by an optional else if...else statement, which is very useful to test various conditions using single if...else if statement.
Rule of if else if:

  •   An if can have zero or one else's and it must come after any else if's.
  •   An if can have zero to many else if's and they must come before the else.
  •   Once an else if succeeds, none of the remaining else if's or else's will be tested.

Syntax:

[ Note: /*......................*/ is used only for comment ] 

The syntax of an if...else if...else statement in C programming language:

If (expression 1)
{
   /* Executes when the expression 1 is true */
}
else if (expression 2)
{
   /* Executes when the expression 2 is true */
}
else if (expression 3)
{
   /* Executes when the expression 3 is true */
}
else 
{
   /* executes when the none of the above condition is true */
}

What is Prime Number

Prime Number

Prime number is a number which is  divisor by 1 and itself. It is a number that can't divisor except 1 or itself. The first few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23, and 29.
On the other hand, A prime number is a whole number greater than 1, whose only two whole-number factors are 1 and itself..
Suppose n is a whole number, and we want to test it to see if it is prime.   First, we take the square root (or the 1/2 power) of n; then we round this number up to the next highest whole number.  Call the result m.  We must find all of the following quotients:
qm = n / m
q(m-1) = n / (m-1)
q(m-2) = n / (m-2)
q(m-3) = n / (m-3)
. . .
q3 = n / 3
q2 = n / 2
The number n is prime if and only if none of the q's, as derived above, are whole numbers.

if and statement

if and statement

The  if   statement controls conditional branching. The body of an if statement is executed if the value of the expression is nonzero. The syntax for the if statement has two forms.

Syntax
Selection-statement:
if ( expression ) statement
if ( expression ) statement else statement

In both forms of the if statement, the expressions, which can have any value except a structure, are evaluated, including all side effects.

In the first form of the syntax, if expression is true (nonzero), statement is executed. If expression is false, statement is ignored. In the second form of syntax, which uses else, the second statement is executed if expression is false. With both forms, control then passes from the if statement to the next statement in the program unless one of the statements contains a break, continue, or goto.

The following are examples of the if statement: 

if ( i > 0 )
    y = x / i;
else
{
    x = i;
    y = f( x );
}


Monday, May 6, 2013

Sum of serise: The natural logarithm can be approximated by the following series.

#include<stdio.h>
void main()
{
float x,a,b=1,sum=0;
printf("Enter the value of X: ");
scanf("%f"&x);
a=(x-1)/x;
for(int k=1;k<=7;k++)
{
b=b*a;
sum+=b;
}
sum-=a;
sum/=2;
sum+=a;
printf("\n\n\t The Natural algorithm of %f=%f",x,sum);
return 0;
}

Pattern: printf pattern./ print following pattern

Write a program which will print following pattern...

#include<stdio.h>
#include<math.h>
void main()
{
int i,j,k=1,l=0;
for(i=0;i<4;i++)
{
for(j=3;j<4;j++)
{
k=i-abs(j);
if(k<0||(k%2)!=0)
printf("\t");
else
printf("%8d",++l);
}
printf("\n");
}
return 0;
}

Sunday, May 5, 2013

String:Reverse string

Write a program in c which will reverse a string 
#include <stdio.h>
#include <string.h>
int main()
{
   char rose[100];
   printf("Enter a string to reverse\n");
   gets(rose);
   strrev(rose);
   printf("After reverse the  string is \n%s\n",rose);
   return 0;
}
 

Saturday, May 4, 2013

Sum the selected digits

If a four digit integer number is input through the keyboard then write a c program to find the sum of first and  before last digit.
#include<stdio.h>
main()
{
    int i,j,num,first,sum=0,beforelast,temp=0;
    printf("Enter the number:");
    scanf("%d",&num);
    for(i=1;i<=2;i++)
    {
       for(j=1;j<=i;j++)
        first=num%10;
        num/=10;
    }
    while(num!=0)
{
beforelast=num;
num=num/10;
}
sum=first+beforelast;
printf("\nSum of first digit and  before last digit is:%d\n",sum);
}
 

Friday, May 3, 2013

Base current,Base voltage,Base impedence flow

ZB=VB/IB   [where IB=(VA)B/VB)]
VB=ZB*IB
IB=VB/ZB
 #include<stdio.h>
main()
{
    char m;
    float a,b,c,d,sum=0.0;
    c=d/b;
    printf("\n\t-:IT'S A PROGRAM FOR BASE CURRENT ,VOLTAGE AND IMPEDENCE:-\n");
    printf("  _________________________________________________________________\n");
    printf("  For ZB=VB/IB press 'A',For VB=ZB*IB press'B' and For IB=ZV/VB press 'C'\n");
    printf("__________________________________________________________________________\n");
    scanf("%c",&m);
    switch(m)
    {
        case'A':
        printf("\nEnter the value of VB:");
        scanf("%f",&b);
        printf("\nEnter the value of IB:");
        scanf("%f",&c);
        sum=(b/c);
        printf("\n\tZB:-%f\n",sum);
        break;
        case'B':
        printf("\nEnter the value of ZB:");
        scanf("%f",&a);
        printf("\nEnter the value of IB:");
        scanf("%f",&c);
        sum=(a*c);
        printf("\n\tVB:-%f\n",sum);
        break;
        case'C':
        printf("\nEnter the value of ZB:");
        scanf("%f",&a);
        printf("\nEnter the value of VB:");
        scanf("%f",&b);
        sum=(a/b);
        printf("\n\tIB:-%f\n",sum);
        break;

    }

}

Voltage:Sum of total voltage in circuit.

Write a program in c to find total voltage in circuit
  V=V1+V2+V3+.....+Vn 




#include<stdio.h>
main()
{
    int V,I,R,n,sum=0,i;
    printf("\n\tIT'S A PROGRAM TO FIND THE TOTAL VALUE OF VOLTAGE IN A CIRCUIT\n");
    printf("\n\tEnter the range value of series:");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        printf("\nEnter the  value of current:");
        scanf("%d",&I);
        printf("\nEnter the value of resistance:");
       scanf("%d",&R);
       V=(I*R);
       sum+=V;
    }
   printf("\n\tTotal voltage is: %d\n",sum);
   return 0;
}
 


Thursday, May 2, 2013

Check Positive or Negative number in C

Write a program which can Check Positive or Negative number in C.  (If enter 0 it will be break)




 #include<stdio.h>
#include<conio.h>
main()
{
    int input;
    read:
    scanf("%d",&input);
    if(input==0)
    goto stop;
    if(input>0)
    {
        printf("Positive");
        goto read;
    }
    else
    printf("Negative");
    goto read;
    stop:
    return 0;
}