Download Programs - C Programming

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Mathematics of radio engineering wikipedia , lookup

Series (mathematics) wikipedia , lookup

Addition wikipedia , lookup

Elementary arithmetic wikipedia , lookup

Transcript
WHILE, DO-WHILE AND FOR
LOOPS
Program to check whether a given number is prime or not.
void main()
{ int i=1, n, count=0;
printf(“Enter a number”);
scanf(“%d”,&n);
while(i<=n)
{ if(n%i==0)
count++;
i++;
}
if(count==2)
printf(“It is prime number”);
else
printf(“Not a prime number”);
}
Class work Problems
1 write a C program to print “Welcome to C
programming” 5 times, each time in new line.
2 Write a C program to display all the prime
numbers between 50 and 100.
Program to find the factorial of a given integer using entry
control while loop
void main()
{
int i=1, n, fact=1;
printf(“Enter the number”);
scanf(“%d”, &n);
while(i<=n)
{
fact = fact * i;
i++;
}
printf(“Factorial=%d”, fact);
}
Program to find the factorial of a given integer using exit
control do-while loop
void main()
{
int i=1, n, fact=1;
printf(“Enter the number”);
scanf(“%d”, &n);
do
{
fact = fact * i;
i++;
} while(i<=n);
printf(“Factorial=%d”, fact);
}
Difference between While and Do- While Loop
WHILE LOOP
It is an Entry Control Loop.
Condition or Test expression is
checked at the beginning.
DO-WHILE LOOP
It is an Exit Control Loop.
Condition or Test expression is
checked at the last.
Statements inside the while
loop will be executed only if
the
condition
or
test
expression is true.
Statements inside the dowhile loop will execute at least
once even if the condition or
test expression is false.
Difference Example
void main()
{
int a=5,b=6;
while(a>b)
{
printf(“\n%d”, a);
a++;
}
getch();
}
void main()
{
int a=5,b=6;
do
{
printf(“\n%d”, a);
a++;
} while(a>b);
getch();
}
Sum of digits of a number
void main()
{
int n, i, sum=0, rem;
printf(“Enter any number”);
scanf(“%d”, &n);
while(n>0)
{
rem = n % 10;
sum = sum + rem;
n = n / 10;
}
printf(“Sum of digits of number=%d”, sum);
}
Program to reverse a number
void main()
{
int n, rev=0, rem;
printf(“Enter any number”);
scanf(“%d”, &n);
while(n>0)
{
rem = n % 10;
rev = rev * 10 + rem;
n = n / 10;
}
printf(“Reverse of given number=%d”, rev);
}
Classwork problems
1 write a program to check whether a given
number is Harshad number or not using dowhile loop or exit control loop.
2 write a program to print all the harshad
numbers between 1 and 100 using entry
control loop or while loop.
3 write a program to check whether a given
number is palindrome or not.
For loop
for(initialization ; test expression ; updation)
{
statements;
}
Example to print first n odd natural numbers
void main()
{
int limit, i;
printf(“Enter the limit”);
scanf(“%d”, &limit);
for(i=1 ; i<=limit ; i++)
{
if( i % 2!= 0 )
printf(“%d”, i);
}
getch();
}
Classwork problems
1 write a program to check whether the given
number is perfect or not using for loop.
Program to generate Fibonacci series
void main()
{ int a=0, b=1, c, i, limit;
printf(“Enter the limit”);
scanf(“%d”, &limit);
if(limit==1)
printf(“%5d”, a);
else if(limit==2)
printf(“%5d%5d”, a, b);
else
{
for(i=1 ; i<=n-2 ; i++)
{
c = a + b;
printf(“%5d”, c);
a = b;
b = c;
}
getch()
}
Classwork problems
1 write a program to generate the Lucas
sequence.
2 write a program to check whether a given 3
digit number is armstrong or not?
Program to generate Lucas Sequence
void main()
{ int a=1,b=3,c,n,i;
clrscr();
printf("Enter the limit\n");
scanf("%d",&n);
if(n==1)
printf("%5d",a);
else if(n==2)
printf("%5d%5d",a,b);
else
{
printf("%5d%5d",a,b);
for(i=1;i<=n-2;i++)
{
c=a+b;
printf("%5d",c);
a=b;
b=c;
}
}
getch();
}
Program to check whether a given integer is a Harshad number or
not.
void main()
{
int n,sum=0,i,rem,num;
clrscr();
printf("Enter the number\n");
scanf("%d",&n);
num=n;
for(;n>0;)
{
rem=n%10;
sum=sum+rem;
n=n/10;
}
if(num%sum==0)
printf("number is Harshad Number");
else
printf("number is not a Harshad Number");
getch();
}
Program to check whether a number is Armstrong or not.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,i,rem,num,digit_count=0,sum=0;
clrscr();
printf("Enter the number\n");
scanf("%d",&n);
num=n;
while(n>0)
{
n=n/10;
digit_count++;
}
n=num;
do
{
rem=n%10;
sum=sum+pow(rem,digit_count);
n=n/10;
} while(n>0);
if(num==sum)
printf("number is Armstrong Number");
else
printf("number is not an Armstrong Number");
getch();
}
Unconditional Statements
1 break
2 continue
3 goto
Program to check prime number using break statement.
void main()
{
int n,i,flag=0;
clrscr();
printf("Enter the number\n");
scanf("%d",&n);
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
flag=1;
break;
//Using break Here…
}
}
if(flag==0)
printf("Prime number");
else
printf("Not a prime number");
getch();
}
Program using Continue Statement.
void main()
{
int i;
clrscr();
for(i=200;i<=300;i++)
{
if(i%2==0)
continue; // Continue used Here…
printf("%5d",i);
}
getch();
}
goto statement example 1
//To count the number of digits of a number.
void main()
{
int i,n,count=0,L1;
clrscr();
printf("Enter the number to count its number of digits");
scanf("%d",&n);
L1: if(n>0)
{
n=n/10;
count++;
goto L1;
}
printf("digit count=%d",count);
getch();
}