Looping statements
• For (Initializes, condition checking, executes the body statements , at last update)
• While (Initializes, condition checking, executes the body statements , updation done inside
the body.)
• do-while(Executes the body statements , condition checking)
Further these loops classified into two types
Entry Controlled loops: In Entry controlled loops the test condition is checked before entering
the main body of the loop. For Loop and While Loop is Entry-controlled loops.
Exit Controlled loops: In Exit controlled loops the test condition is evaluated at the end of the
loop body. The loop body will execute at least once, irrespective of whether the condition is true
or false. do-while Loop is Exit Controlled loop.
For Loop
Syntax :-
for (initialization; condition; updating Statement)
{
// statements inside the body of loop
}
For loop
• For loop is also known as definite loop ,because programmer knows
exactly how many times loop will repeat.
• if we place a ; after for loop it will consider as null statement.
Complier will not return any error in this situation, rather treated it as
null statement which causes the time delays
• Multiple initialization separate with comma.
• Initialization can also be skipped by just putting the semicolon.
Working of for loop
Initialization
Condition
For loop body statement
Updating
False
True
Exit
1. #include <stdio.h>
2. int main()
3. {
4. int i;
5. for(i=1;i<=10;i++)
6. {
7. printf("%dn",i);
8. }
9. return 0;
10. }
1. #include <stdio.h>
2. int main()
3. {
4. int i;
5. for(i=1;i<=10;i++)
6. {
7. printf("%dn",i*2);
8. }
9. return 0;
10. }
• PRINT YOUR NAME 5 TIMES
#include <stdio.h>
int main()
{ int a;
for(a=1;a<=10;a++)
{
printf("navn");
}
return 0;
}
Condition is not mentioned(infinite
loop)
• #include <stdio.h>
• int main()
• {
•
• for(int a = 0; ; a++)
• {
• printf("%d ",a);
• }
•
• return 0;
• }
Infinite loop
• #include <stdio.h>
• int main()
• {
•
• for(int a = 10; a> 0 ; a++)
• {
• printf("%d ",a);
• }
•
• return 0;
• }
Infinite loop
#include <stdio.h>
int main()
{
for(int a = 0; a < 10 ; a--)
{
printf("%d",a);
}
return 0;
}
• #include <stdio.h>
• int main()
• {
• int a,b,c;
• for(a=0,b=0;a<4,b<4;a+
+,b++)
• {
• printf("%d %d n",a,b);
•
• }
• }
#include<stdio.h>
int main()
{
int a,i;
printf("enter value of an");
scanf("%d",&a);
for(i=2;i<a;i++)
{
if(a%i==0)
break;
}
if(i==a)
{
printf("prime numbre %d",i);
}
else
{
printf("Not a prime numbre");
}
return 0;
}
#include<stdio.h>
int main()
{
int i;
for(i=32;i<=126;i++)
{
printf("The character attached with ASCCI %d is %cn ",
i,i);
}
return 0;
}
• #include <stdio.h>
• int main()
• {
• int sum=0, avg;
• for (int i=1;i<=300;i++)
• {
• if(i%7==0)
• {
• sum=sum+i;
• avg= sum/i;
• }
• }
• printf("sum is %dn",sum);
• return 0;
• }
While Loop
In which condition is evaluated before processing a body of the loop. If a
condition is true then and only then the body of a loop is executed. After
the body of a loop is executed then control again goes back at the
beginning, and the condition is checked if it is true, the same process is
executed until the condition becomes false. Once the condition becomes
false, the control goes out of the loop. It is also known as top checking loop,
since control condition placed at the first line.
** if condition not updated it will go into the infinite loop.
Syntax
• while (condition) {
• statements;
• }
• #include<stdio.h>
• int main()
• {
• int a=1;
• while(a<=10)
• {
• printf("%dn",a);
• a++;
• }
• return 0;
• }
• #include<stdio.h>
• int main()
• {
• int a=1,sum=0;;
• while(a!=0)
• {
• printf("enter value of an");
• scanf("%d",&a);
• sum =sum+a;
• }
• printf("SUM IS %d",sum);
• return 0;
• }
if condition not updated it will go
into the infinite loop.
• #include <stdio.h>
• int main()
• {
• int i=1;
• while(i)
• {
• printf("%d",i);
• i++;
• }
• return 0;
• }
• WAP to calculate the sum of numbers from m to n.
#include<stdio.h>
int main()
{
int m,n,sum=0;
printf("enter the value of m");
scanf("%d",&m);
printf("enter the value of n");
scanf("%d",&n);
while(m<=n)
{
sum =sum+m;
printf("sum = %dn",sum);
m =m+1;
}
return 0;
}
SUM of first 10 numbers
• #include<stdio.h>
• int main()
• {
• int a=0,sum ;
• while(a<=10)
• {
• sum =sum+a;
•
• a =a+1;
• }
• printf("sum = %dn",sum);
• return 0;
• }
Do-While loop
It is similar to the while loop except that the condition is
always executed after the body of a loop. It is also called an
exit-controlled loop. In do-while loop, the while condition is
written at the end and terminates with a semi-colon (;)
• Syntax
do {
statements
} while (expression);
• #include<stdio.h>
• int main()
• {
• int a=1;
• do
• {
• printf("%dn",a);
• a++;
• }while(a<=10);
• return 0;
• }
• #include <stdio.h>
• int main()
• {
• int i = 0;
• do {
• printf("namen");
• i++;
• } while (i < 5);
• return 0;
• }
Nested for loop
• Loop inside loop
• For loop used to control the number of times that particular set of statements
executed. Another outer loop could be used to control the number of times that a
whole loop is repeated.
Syntax:-
for ( initialization; condition; increment ) //outer loop
{
for ( initialization; condition; increment )//inside loop
{
// statement of inside loop
}
// statement of outer loop
}
• #include <stdio.h>
• int main()
• {
• int i,j;
• for (i=1;i<=5;i++)
•
• {
• printf("n");
• for (j=1;j<=10;j++)
• {
• printf("*");
• }
• }
• return 0;
• }
• #include <stdio.h>
• int main()
• {
• int i,j;
• for (i=1;i<=5;i++)
•
• {
• printf("n");
• for (j=1;j<=i;j++)
• {
• printf("*");
• }
• }
• return 0;
• }
• #include <stdio.h>
• int main()
• {
• int i,j;
• for (i=1;i<=5;i++)
•
• {
• printf("n");
• for (j=1;j<=i;j++)
• {
• printf("%d",j);
• }
• }
• return 0;
• }
• #include <stdio.h>
• int main()
• {
• int i,j;
• for (i=1;i<=5;i++)
•
• {
• printf("n");
• for (j=1;j<=i;j++)
• {
• printf("%d",i);
• }
• }
• return 0;
• }
Floyd's Triangle
• #include <stdio.h>
• int main()
• {
• int i,j,k=1;
• for (i=1;i<=4;i++)
•
• {
• printf("n");
• for (j=1;j<=i;j++)
• {
• printf("%d",k++);
• }
• }
• return 0;
• }
• #include <stdio.h>
• int main()
• {
• int i,j;
• for(i=1;i<=5;i++)
• {
• for(j=5;j>=i;j--)
• {
• printf("*");
• }
• printf("n");
• }
•
• return 0;
• }
•
#include<stdio.h>
int main()
{
int i,j;
for(i=0;i<5;i++)
{
for(j=5;j>i;j--)
{
printf(" ");
}
for(j=0;j<=i;j++)
{
printf("*");
}
printf("n");
}
return 0;
}
#include<stdio.h>
int main()
{
int i,j;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if (i==0||i==4)
{
printf("*");
}
else
{
if(j==0||j==4)
{
printf("*");
}
else
{
printf(" ");
}
}
}
printf("n");
}
return 0;
}
• #include <stdio.h>
• int main()
• {
• int i,j,k,l;
• for (i=0;i<=8;i++)
• {
• for (j=8;j>i;j--)
• {
• printf(" ");
• }
• for (k=0;k<i;k++)
• {
• printf("*");
• printf(" ");
• }
•
• printf("n");
• }
• for (i=0;i<=6;i++)
• {
• for (j=0;j<=i;j++)
• {
• printf(" ");
• }
• for (k=6;k>=i;k--)
• {
• printf("*");
• printf(" ");
• }
•
• printf("n");
• }
•
• return 0;
• }
Nested while loop
• while(condition)
• {
• // statements
•
• while(condition)
• {
• // Inner loop statements
• }
• // statements
• }
• #include <stdio.h>
• int main()
• {
• int i=1;
• while(i<=3)
• {
• int j=1;
• while(j<=3)
• {
• printf("*");
• j++;
• }
• i++;
• printf(" n");
• }
• return 0;
• }
• #include <stdio.h>
• int main()
• {
• int i=1;
• while(i<=3)
• {
• int j=1;
• while(j<=i)
• {
• printf("*");
• j++;
• }
• i++;
• printf(" n");
• }
• return 0;
• }
Nested do-while loop
• do{
• do{
•
• // statement of inside loop
• }while(condition);
• // statement of outer loop
• }while(condition);
• #include <stdio.h>
• int main()
• {
• int i=1;
• do
• {
• int j=1;
• do
• {
• printf("*");
• j++;
• }
• while(j<=5);
• printf("n");
• i++;
• }
• while(i<=3);
•
•
• return 0;
• }
• #include <stdio.h>
• int main()
• {
• int i=1;
• do
• {
• int j=1;
• do
• {
• printf("*");
• j++;
• }
• while(j<=i);
• printf("n");
• i++;
• }
• while(i<=3);
•
•
• return 0;
• }
Jumping statements
Jump Statement makes the control jump to another section of the
program unconditionally when encountered. It is usually used to
terminate the loop or switch-case instantly. It is also used to escape the
execution of a section of the program.
• goto
• continue
• break
Break;
• #include <stdio.h>
• int main() {
• int i;
• for (i = 1; i <= 15; i++) {
• printf("%dn", i);
• if (i == 10)
• break;
• }
• return 0;
• }
continue
• #include <stdio.h>
• int main() {
• int i, j;
• for (i = 1; i < 3; i++) {
• for (j = 1; j < 5; j++) {
• if (j == 2)
• continue;
• printf("%dn", j);
• }
• }
• return 0;
• }
Goto Statement and Reverse goto
• #include <stdio.h>
• int main() {
• int i, j;
• for (i = 1; i < 5; i++) {
• if (i == 2)
• goto some;
• printf("%dn", i);
• }
• some:
• printf("Two");
• return 0;
• }
• #include <stdio.h>
• int main()
• { int a,i;
• table:
• printf("Enter the number for printing the
table ");
• scanf("%d",&a);
• for(i=1;i<=10;i++)
• {
• printf("%d x %d = %dn",a,i,a*i);
• if(i==10)
• goto table;
• }
• return 0;
• }
Palindrome number
• #include <stdio.h>
• int main()
• {
• int num, mod , rev=0,temp;
• printf("Enter an numbern");
• scanf("%d", &num);
• temp = num;
while (temp > 0)
• {
• mod = temp % 10;
• rev = rev *10+mod;
• temp = temp/10;
• }
• if (num == rev)
• printf("%d is a palindrome number.n", num);
• else
• printf("%d isn't a palindrome number.n", num);
• return 0;
• }
• Armstrong number is a number that is equal to
the sum of cubes of its digits.
#include <stdio.h>
int main()
{
int num, mod , sum=0,temp;
printf("Enter an numbern");
scanf("%d", &num);
temp = num;
while (temp > 0)
{
mod = temp % 10;
sum = sum+(mod*mod*mod);
temp = temp/10;
}
if (num == sum)
printf("%d is a armstrong number.n", num);
else
printf("%d isn't a armstrong number.n", num);
return 0;
}
palindrome number using for loop
• #include <stdio.h>
• int main()
• {
• int num, rem , rev=0,temp;
• printf("Enter an numbern");
• scanf("%d", &num);
• temp = num;
• for (temp =num;temp>0;temp=temp/10)
• {
• rem = temp % 10;
• rev = rev *10+rem;
• }
• if (num == rev)
• printf("%d is a palindrome number.n", num);
• else
• printf("%d isn't a palindrome number.n", num);
• return 0;
• }
Fibonacci Series
• #include <stdio.h>
• int main() {
• int a=0, b=1, SUM, i,n;
• printf("enter number for how long u want to
print the fibonacci seriers ");
• scanf("%d",&n);
• printf("%d %d ",a,b);
• for(i = 2; i <= n; i++) {
• SUM = a + b;
• printf("%d ", SUM);
•
• a = b;
• b = SUM;
• }
•
• return 0;
• }
Factorial
• #include<stdio.h>
• int main()
• {
• int i,f=1,n;
• printf("Enter a number: ");
• scanf("%d",&n);
• for(i=1;i<=n;i++){
• f=f*i;
• }
• printf("Factorial of %d is: %d",n,f);
• return 0;
• }
Sum of series 1+2+3……..n
#include<stdio.h>
int main()
{
int i,sum=0,n;
printf("Enter a number: ");
scanf("%d",&n);
for(i=1;i<=n;i++){
sum+=i;
}
printf("sum is: %d",sum);
return 0;
}
Sum of series 1^2+2^2+3^2....n^2
#include<stdio.h>
int main()
{
int i,sum=0,n;
printf("Enter a number: ");
scanf("%d",&n);
for(i=1;i<=n;i++){
sum=sum+(i*i);
}
printf("sum is: %d",sum);
return 0;
}
• 1+1/2+1/3+1/4………..1/n
#include<stdio.h>
int main()
{
int i,sum=0,n;
printf("Enter a number: ");
scanf("%d",&n);
for(i=1;i<=n;i++){
sum=sum+1/i; //use typecasting
}
printf("sum is: %d",sum);
return 0;
}
• X+x^2+x^3+x^4……x^n
#include<stdio.h>
#include<math.h>
int main()
{
int i,n,x,sum=0;
printf("Enter a number: ");
scanf("%d %d",&n,&x);
for(i=1;i<=n;i++){
sum= sum+pow(x,i);
}
printf("sum is: %d",sum);
return 0;
}
• #include <stdio.h>
• int main()
• {
• int i,j,k,l,n;
•
• for(i=0;i<1;i++)
• {
• for(j=10;j>i;j--)
• {
• printf("*");
• }
• printf("n");
• }
• for(i=0;i<10;i++)
• {
• for(k=0;k<2;k++)
• {
• printf("*");
• printf(" ");
• }
• printf("n");
• }
• for(l=0;l<10;l++)
• {
• printf("*");
• }
•
• return 0;
• }
•
•

Looping statements.pptx for basic in c language

  • 1.
    Looping statements • For(Initializes, condition checking, executes the body statements , at last update) • While (Initializes, condition checking, executes the body statements , updation done inside the body.) • do-while(Executes the body statements , condition checking) Further these loops classified into two types Entry Controlled loops: In Entry controlled loops the test condition is checked before entering the main body of the loop. For Loop and While Loop is Entry-controlled loops. Exit Controlled loops: In Exit controlled loops the test condition is evaluated at the end of the loop body. The loop body will execute at least once, irrespective of whether the condition is true or false. do-while Loop is Exit Controlled loop. For Loop Syntax :- for (initialization; condition; updating Statement) { // statements inside the body of loop }
  • 2.
    For loop • Forloop is also known as definite loop ,because programmer knows exactly how many times loop will repeat. • if we place a ; after for loop it will consider as null statement. Complier will not return any error in this situation, rather treated it as null statement which causes the time delays • Multiple initialization separate with comma. • Initialization can also be skipped by just putting the semicolon.
  • 3.
    Working of forloop Initialization Condition For loop body statement Updating False True Exit
  • 4.
    1. #include <stdio.h> 2.int main() 3. { 4. int i; 5. for(i=1;i<=10;i++) 6. { 7. printf("%dn",i); 8. } 9. return 0; 10. } 1. #include <stdio.h> 2. int main() 3. { 4. int i; 5. for(i=1;i<=10;i++) 6. { 7. printf("%dn",i*2); 8. } 9. return 0; 10. }
  • 5.
    • PRINT YOURNAME 5 TIMES #include <stdio.h> int main() { int a; for(a=1;a<=10;a++) { printf("navn"); } return 0; } Condition is not mentioned(infinite loop) • #include <stdio.h> • int main() • { • • for(int a = 0; ; a++) • { • printf("%d ",a); • } • • return 0; • }
  • 6.
    Infinite loop • #include<stdio.h> • int main() • { • • for(int a = 10; a> 0 ; a++) • { • printf("%d ",a); • } • • return 0; • } Infinite loop #include <stdio.h> int main() { for(int a = 0; a < 10 ; a--) { printf("%d",a); } return 0; }
  • 7.
    • #include <stdio.h> •int main() • { • int a,b,c; • for(a=0,b=0;a<4,b<4;a+ +,b++) • { • printf("%d %d n",a,b); • • } • } #include<stdio.h> int main() { int a,i; printf("enter value of an"); scanf("%d",&a); for(i=2;i<a;i++) { if(a%i==0) break; } if(i==a) { printf("prime numbre %d",i); } else { printf("Not a prime numbre"); } return 0; }
  • 8.
    #include<stdio.h> int main() { int i; for(i=32;i<=126;i++) { printf("Thecharacter attached with ASCCI %d is %cn ", i,i); } return 0; } • #include <stdio.h> • int main() • { • int sum=0, avg; • for (int i=1;i<=300;i++) • { • if(i%7==0) • { • sum=sum+i; • avg= sum/i; • } • } • printf("sum is %dn",sum); • return 0; • }
  • 9.
    While Loop In whichcondition is evaluated before processing a body of the loop. If a condition is true then and only then the body of a loop is executed. After the body of a loop is executed then control again goes back at the beginning, and the condition is checked if it is true, the same process is executed until the condition becomes false. Once the condition becomes false, the control goes out of the loop. It is also known as top checking loop, since control condition placed at the first line. ** if condition not updated it will go into the infinite loop. Syntax • while (condition) { • statements; • }
  • 10.
    • #include<stdio.h> • intmain() • { • int a=1; • while(a<=10) • { • printf("%dn",a); • a++; • } • return 0; • } • #include<stdio.h> • int main() • { • int a=1,sum=0;; • while(a!=0) • { • printf("enter value of an"); • scanf("%d",&a); • sum =sum+a; • } • printf("SUM IS %d",sum); • return 0; • }
  • 11.
    if condition notupdated it will go into the infinite loop. • #include <stdio.h> • int main() • { • int i=1; • while(i) • { • printf("%d",i); • i++; • } • return 0; • } • WAP to calculate the sum of numbers from m to n. #include<stdio.h> int main() { int m,n,sum=0; printf("enter the value of m"); scanf("%d",&m); printf("enter the value of n"); scanf("%d",&n); while(m<=n) { sum =sum+m; printf("sum = %dn",sum); m =m+1; } return 0; }
  • 12.
    SUM of first10 numbers • #include<stdio.h> • int main() • { • int a=0,sum ; • while(a<=10) • { • sum =sum+a; • • a =a+1; • } • printf("sum = %dn",sum); • return 0; • }
  • 13.
    Do-While loop It issimilar to the while loop except that the condition is always executed after the body of a loop. It is also called an exit-controlled loop. In do-while loop, the while condition is written at the end and terminates with a semi-colon (;) • Syntax do { statements } while (expression);
  • 14.
    • #include<stdio.h> • intmain() • { • int a=1; • do • { • printf("%dn",a); • a++; • }while(a<=10); • return 0; • } • #include <stdio.h> • int main() • { • int i = 0; • do { • printf("namen"); • i++; • } while (i < 5); • return 0; • }
  • 15.
    Nested for loop •Loop inside loop • For loop used to control the number of times that particular set of statements executed. Another outer loop could be used to control the number of times that a whole loop is repeated. Syntax:- for ( initialization; condition; increment ) //outer loop { for ( initialization; condition; increment )//inside loop { // statement of inside loop } // statement of outer loop }
  • 16.
    • #include <stdio.h> •int main() • { • int i,j; • for (i=1;i<=5;i++) • • { • printf("n"); • for (j=1;j<=10;j++) • { • printf("*"); • } • } • return 0; • } • #include <stdio.h> • int main() • { • int i,j; • for (i=1;i<=5;i++) • • { • printf("n"); • for (j=1;j<=i;j++) • { • printf("*"); • } • } • return 0; • }
  • 17.
    • #include <stdio.h> •int main() • { • int i,j; • for (i=1;i<=5;i++) • • { • printf("n"); • for (j=1;j<=i;j++) • { • printf("%d",j); • } • } • return 0; • } • #include <stdio.h> • int main() • { • int i,j; • for (i=1;i<=5;i++) • • { • printf("n"); • for (j=1;j<=i;j++) • { • printf("%d",i); • } • } • return 0; • }
  • 18.
    Floyd's Triangle • #include<stdio.h> • int main() • { • int i,j,k=1; • for (i=1;i<=4;i++) • • { • printf("n"); • for (j=1;j<=i;j++) • { • printf("%d",k++); • } • } • return 0; • } • #include <stdio.h> • int main() • { • int i,j; • for(i=1;i<=5;i++) • { • for(j=5;j>=i;j--) • { • printf("*"); • } • printf("n"); • } • • return 0; • } •
  • 19.
    #include<stdio.h> int main() { int i,j; for(i=0;i<5;i++) { for(j=5;j>i;j--) { printf(""); } for(j=0;j<=i;j++) { printf("*"); } printf("n"); } return 0; } #include<stdio.h> int main() { int i,j; for(i=0;i<5;i++) { for(j=0;j<5;j++) { if (i==0||i==4) { printf("*"); } else { if(j==0||j==4) { printf("*"); } else { printf(" "); } } } printf("n"); } return 0; }
  • 20.
    • #include <stdio.h> •int main() • { • int i,j,k,l; • for (i=0;i<=8;i++) • { • for (j=8;j>i;j--) • { • printf(" "); • } • for (k=0;k<i;k++) • { • printf("*"); • printf(" "); • } • • printf("n"); • } • for (i=0;i<=6;i++) • { • for (j=0;j<=i;j++) • { • printf(" "); • } • for (k=6;k>=i;k--) • { • printf("*"); • printf(" "); • } • • printf("n"); • } • • return 0; • }
  • 21.
    Nested while loop •while(condition) • { • // statements • • while(condition) • { • // Inner loop statements • } • // statements • }
  • 22.
    • #include <stdio.h> •int main() • { • int i=1; • while(i<=3) • { • int j=1; • while(j<=3) • { • printf("*"); • j++; • } • i++; • printf(" n"); • } • return 0; • } • #include <stdio.h> • int main() • { • int i=1; • while(i<=3) • { • int j=1; • while(j<=i) • { • printf("*"); • j++; • } • i++; • printf(" n"); • } • return 0; • }
  • 23.
    Nested do-while loop •do{ • do{ • • // statement of inside loop • }while(condition); • // statement of outer loop • }while(condition);
  • 24.
    • #include <stdio.h> •int main() • { • int i=1; • do • { • int j=1; • do • { • printf("*"); • j++; • } • while(j<=5); • printf("n"); • i++; • } • while(i<=3); • • • return 0; • } • #include <stdio.h> • int main() • { • int i=1; • do • { • int j=1; • do • { • printf("*"); • j++; • } • while(j<=i); • printf("n"); • i++; • } • while(i<=3); • • • return 0; • }
  • 25.
    Jumping statements Jump Statementmakes the control jump to another section of the program unconditionally when encountered. It is usually used to terminate the loop or switch-case instantly. It is also used to escape the execution of a section of the program. • goto • continue • break
  • 26.
    Break; • #include <stdio.h> •int main() { • int i; • for (i = 1; i <= 15; i++) { • printf("%dn", i); • if (i == 10) • break; • } • return 0; • } continue • #include <stdio.h> • int main() { • int i, j; • for (i = 1; i < 3; i++) { • for (j = 1; j < 5; j++) { • if (j == 2) • continue; • printf("%dn", j); • } • } • return 0; • }
  • 27.
    Goto Statement andReverse goto • #include <stdio.h> • int main() { • int i, j; • for (i = 1; i < 5; i++) { • if (i == 2) • goto some; • printf("%dn", i); • } • some: • printf("Two"); • return 0; • } • #include <stdio.h> • int main() • { int a,i; • table: • printf("Enter the number for printing the table "); • scanf("%d",&a); • for(i=1;i<=10;i++) • { • printf("%d x %d = %dn",a,i,a*i); • if(i==10) • goto table; • } • return 0; • }
  • 28.
    Palindrome number • #include<stdio.h> • int main() • { • int num, mod , rev=0,temp; • printf("Enter an numbern"); • scanf("%d", &num); • temp = num; while (temp > 0) • { • mod = temp % 10; • rev = rev *10+mod; • temp = temp/10; • } • if (num == rev) • printf("%d is a palindrome number.n", num); • else • printf("%d isn't a palindrome number.n", num); • return 0; • } • Armstrong number is a number that is equal to the sum of cubes of its digits. #include <stdio.h> int main() { int num, mod , sum=0,temp; printf("Enter an numbern"); scanf("%d", &num); temp = num; while (temp > 0) { mod = temp % 10; sum = sum+(mod*mod*mod); temp = temp/10; } if (num == sum) printf("%d is a armstrong number.n", num); else printf("%d isn't a armstrong number.n", num); return 0; }
  • 29.
    palindrome number usingfor loop • #include <stdio.h> • int main() • { • int num, rem , rev=0,temp; • printf("Enter an numbern"); • scanf("%d", &num); • temp = num; • for (temp =num;temp>0;temp=temp/10) • { • rem = temp % 10; • rev = rev *10+rem; • } • if (num == rev) • printf("%d is a palindrome number.n", num); • else • printf("%d isn't a palindrome number.n", num); • return 0; • }
  • 30.
    Fibonacci Series • #include<stdio.h> • int main() { • int a=0, b=1, SUM, i,n; • printf("enter number for how long u want to print the fibonacci seriers "); • scanf("%d",&n); • printf("%d %d ",a,b); • for(i = 2; i <= n; i++) { • SUM = a + b; • printf("%d ", SUM); • • a = b; • b = SUM; • } • • return 0; • } Factorial • #include<stdio.h> • int main() • { • int i,f=1,n; • printf("Enter a number: "); • scanf("%d",&n); • for(i=1;i<=n;i++){ • f=f*i; • } • printf("Factorial of %d is: %d",n,f); • return 0; • }
  • 31.
    Sum of series1+2+3……..n #include<stdio.h> int main() { int i,sum=0,n; printf("Enter a number: "); scanf("%d",&n); for(i=1;i<=n;i++){ sum+=i; } printf("sum is: %d",sum); return 0; } Sum of series 1^2+2^2+3^2....n^2 #include<stdio.h> int main() { int i,sum=0,n; printf("Enter a number: "); scanf("%d",&n); for(i=1;i<=n;i++){ sum=sum+(i*i); } printf("sum is: %d",sum); return 0; }
  • 32.
    • 1+1/2+1/3+1/4………..1/n #include<stdio.h> int main() { inti,sum=0,n; printf("Enter a number: "); scanf("%d",&n); for(i=1;i<=n;i++){ sum=sum+1/i; //use typecasting } printf("sum is: %d",sum); return 0; } • X+x^2+x^3+x^4……x^n #include<stdio.h> #include<math.h> int main() { int i,n,x,sum=0; printf("Enter a number: "); scanf("%d %d",&n,&x); for(i=1;i<=n;i++){ sum= sum+pow(x,i); } printf("sum is: %d",sum); return 0; }
  • 33.
    • #include <stdio.h> •int main() • { • int i,j,k,l,n; • • for(i=0;i<1;i++) • { • for(j=10;j>i;j--) • { • printf("*"); • } • printf("n"); • } • for(i=0;i<10;i++) • { • for(k=0;k<2;k++) • { • printf("*"); • printf(" "); • } • printf("n"); • } • for(l=0;l<10;l++) • { • printf("*"); • } • • return 0; • } • •