Thursday, 29 September 2011

Program to find total no. of digit in a given no.

/* WAP to print the total no. of digit in a given number. Eg. If the entered no. is 13423 then print the total no. pf digit =5 and for 34 total no. of digit=2 */


#include<stdio.h>

#include<conio.h>
void main()
{
int n,i=0,t=0;
clrscr();
for(i=n;i>0;i=i/10)
t++;
printf("The total no. of digit = %d ",t);
getch();
}






Note: This program is only for integer data types that means it will give correct output for max value entered 32767 and greater than this no. output will be 0 beacause of incorrect data specifier..

Facotrial of a number in c language

/* Program to print the factorial of a number */




#include<stdio.h>
#include<conio.h>
void main()
{
int i=0,n;
long double f=1;
clrscr();
printf("Enter the number : ");
scanf("%d",&n);
for(i=n;i>0;i--)
f=f*i;
printf("\n The factorial of the entered number= %ld",f);
getch();
}

Friday, 23 September 2011

Break statement in C language

Use of Break(); in program. 
Program to find whether the number is prime or not...


#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,t=0;
clrscr();
printf("Enter the number: ");
scanf("%d",&n);
for(i=2;i<n/2+1;i++)
{
if(n%i==0)
{
printf("Not prime");
t=1;
break;
}
}
if(t==0)
printf("The number is prime.");
getch();
}

Thursday, 22 September 2011

Program to find all factors of a +ve number in C language


All factors of a given positive number in c


#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
clrscr();
printf("Enter the number: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
printf("\n%d",i);
}
getch();
}

Prime number in C.

Program to check whether the number is prime or not.
Method 1: looping (n-1) times

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i=1,t=0;
clrscr();
printf("Enter the number: ");
scanf("%d",&n);
for(i=1;i<n;i++)
{
if(n%i==0)
t++;
}
if(t==1)
printf("The entered number is prime.");
else
printf("The entered number is not prime.");
getch();
}

Method 2: looping (n-2) times

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i=2,t=0;
clrscr();
printf("Enter the number: ");
scanf("%d",&n);
for(i=2;i<n;i++)
{
if(n%i==0)
t++;
}
if(t==0)
printf("The entered number is prime.");
else
printf("The entered number is not prime.");
getch();
}

Method 3: Best performance looping (n/2+1) times



#include<stdio.h>
#include<conio.h>
void main()
{
int n,i=2,t=1;
clrscr();
printf("Enter the number: ");
scanf("%d",&n);
for(i=2;i<n/2+1;i++)
{
if(n%i==0)
t++;
}
if(t==1)
printf("The entered number is prime.");
else
printf("The entered number is not prime.");
getch();
}




Program to print all even no. from 1 to 100 in C

simple program to implement for loop and if....


#include<stdio.h>
#include<conio.h>
void main()
{
int a=1;
clrscr();
for(a=1;a<=100;a++)
{
if(a%2==0)
printf("%d \n",a);
}
getch();
}

Wednesday, 21 September 2011

How to print table in C

Program to print table of any given no.




#include<stdio.h>
#include<conio.h>
void main()
{
int a,i=0,s=1;
clrscr();
printf("Enter the number: ");
scanf("%d",&a);
for(i=1;i<=10;i++)
{
s=a*i;
printf(" %d ",s);
printf("\n");
}
getch();
}

Tuesday, 20 September 2011

Loop in C language

Program to calculate average of n numbers..

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i=0;
float num,sum=0,avg=0;
clrscr();
printf("How many numbers you want to be inserted: ");
scanf("%d",&n);
printf("Enter the numbers: ");
for(i=0;i<n;i++)
{
scanf("%f",&num);
sum=sum+num;
}
avg=sum/n;
printf("The average of n numbers is= %f",avg);
getch();
}

Monday, 19 September 2011

control statements in C

Program to input the radius of a circle and if radius is greater than 10 then calculate area, circumference,diameter otherwise calculate only are.
#include<stdio.h>
#include<conio.h>
void main()
{
float r,a,c,d;
clrsc();
printf("Enter the radius of the circle: ");
scanf("%f",&r);
if(r>10)
{
d=2*r;
c=2*3.14*r;
a=3.14*r*r;
printf("\nThe diameter of the circle is: %f",d);
}
else
{
a=3.14*r*r;
c=2*3.14*r;
}
printf("\nThe area of the circle is: %f",a);
printf(\nThe circumference is: %f",c);
getch();
}

control statement in C

Example of if statement..
Que: Program in, input the salary of en emplyee and if salary is greater than Rs. 5000 then calculate total salary by bonus 5 %.

#include<stdio.h>
#include<conio.h>
void main()
{
float sal,bonus=0;
clrscr();
printf("Enter the salary of the emplyee: ");
scanf("%f",&sal);
if(sal>5000)
bonus=sal*5/100;
sal=sal+bonus;
printf("Total salary is : %f",sal);
getch();
}

Explanation:
if(condition)
{
true body;
operations;
..........;
.........;
.........;
}
else
{
false body;
operations;
............;
............;
............;
}
(if true body is of single line statement then there is no need of braces({}).)
a statement terminated by semicolon(;)

program of addition,subtraction,multiplication and division..

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
float c=0;
clrscr();
printf("Enter the two numbers: ");
scanf("%d %d",&a,&b);
c=a+b;
printf("Addition is: %f",c);
c=a-b;
printf("Subtraction is: %f",c);
c=a*b;
printf("Multiplication is: %f",c);
c=a/b;
printf("Division is: %f",c)
getch();
}

Program of leap year in C

LEAP YEAR
list of leap years(1804,1808,1812..... and so on..)
list of non leap years(1700,1800,1900,2100....)
#include<stdio.h>
#include<conio.h>
void main()
{
int y;
clrscr();
printf("Enter the year: ");
scanf("%d",&y);
if((y%4==0 && y%100!=0) || (y%400==0))
printf("Leap year..");
else
printf("Not a leap year...");
getch();
}
Run this program and if you have any problem in understanding the concept then do comment.

Friday, 16 September 2011

How to find even or odd in c

Program to find even or odd in c


#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("Enter the number : ");
scanf("%d",&a);
if(a%2==0)
printf("The number is even.");
else
printf("The number is odd.");
getch();
}

Thursday, 15 September 2011

How to swap two numbers without using third variable.

Swapping of two numbers without using third variable....


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter the two numbers: ");
scanf("%d %d",&a,&b);     // let user input a=10 b=20 //
a=a+b;       // a=10+20  i.e. now a=30//
b=a-b;       // b=30-20  i.e. now b=10//
a=a-b;       // a=30-10  i.e. now a=20//
printf("\n After swapping values are : \n a=%d \nb=%d",a,b);
getch();
}

How to swap two numbers in c

Swapping of two numbers using third variable.


#include<stdio.h>
#include<conio.h>
void main()
{
int a=10,b=20,c=0;
clrscr(); 
printf("a=%d \nb=%d",a,b); 
c=a;
a=b;
b=c;
printf("After swapping values of a & b are: \n "); 
printf("a=%d \nb=%d",a,b);
getch();


Add two numbers in c language.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter the first number: ");
scanf("%d",&a);
printf("Enter the second number: ");
scanf("%d",&b);
c=a+b;
printf("The sum of the above two number is = %d ",c);
getch();
}

Hello World!

This is my first program in c language....


#include<stdio.h>
int main()
{
printf("Hello World!");
return 0;
}