C program to print number while is less than 10

Ex: write a C program to print number while is less than 10. How to write a C program to print number while is less than 10. C program to print number while is less than 10.





   Step by step logic of the given program:

1. First we declare intiger variable no=5.

2. After that use one while loop to check number is less than 10 or not.

3. If number is less than 10 we print no inside the while loop.

4. After that we increment number by 1 (no++).

5. It will print (5 6 7 8 9 10) .




   C program to print number while  is less than 10:


#include<stdio.h>
int main()
{
int no=5;

while(no<=10)
{
printf("%d ",no);
no++;
}

return 0;
}



Above prgram shows the following output:



C program to print multiplication table of given number

Ex: Write a C program to print multiplication table of given number. How to write a C program to print multiplication table of given number. C program to print multiplication table of given number.


Input from user:

Enter the number: 12

Expected output:

Multiplication table of 12 is:

12x1=12
12x2=24
12x3=36
12x4=48
12x5=60
12x6=72
12x7=84
12x8=96
12x9=108
12x10=120






   Step by step logic of the given program:


1. Accept input number from user.

2. Use for loop to print multiplication table. for loop will iterate 1 to 10.   for(i=1;i<=10;i++)

3. inside the for loop print multiplication table using no*i.





   C program to print multiplication table of given number :


#include<stdio.h>
int main()
{
int no,i;

printf("Enter the number\n");
scanf("%d",&no);

printf("Multiplication table of %d is:\n",no);

for(i=1;i<=10;i++)
{
printf("%dx%d=%d\n",no,i,(no*i));
}

return 0;
}



Above prgram shows the following output:



C program to print odd numbers from 1 to n without using if statement

Ex: Write a C program to print odd numbers between 1 to n. How to write a C program to print odd numbers between 1 to n. C program to print odd number's between 1 to n.


Input from user: 

Enter the limit: 20

Expected output:

Odd numbers between 1 to 20:

1
3
5
7
9
11
13
15
17
19






   Step by step logic to print Odd numbers between 1 to n(without using if-statement):



1. Accept limit from user store it in variable no.

2. Run for loop which iterate upto n number's. for(i=1;i<=no;i+=2)

3. We used i=1 because odd numbers starts from 1.

4.  We increment loop by two to print odd numbers.

5. Print value of i inside the for loop.




   Program to print odd number's without using if-statement:


#include<stdio.h>
int main()
{

 int i,no;

 printf("Enter the limit: ");
 scanf("%d",&no);

  printf("Odd numbers between 1 to %d\n",no);

 for(i=1;i<=no;i+=2)
 {
   printf("%d\n",i);
 }

 return 0;
}





   Program to print odd number's using if-statement:


In below Program we print odd numbers from 1 to n using if-statement.


#include<stdio.h>
int main()
{

 int i,no;

 printf("Enter the limit: ");
 scanf("%d",&no);

  printf("Odd numbers between 1 to %d\n",no);

 for(i=1;i<=no;i++)
 {
   if(i%2==1)
   printf("%d\n",i);

 }

 return 0;

}



Above program's shows the following output:




C program to print Even number's from 1 to n without using if statement

Ex: Write a C program to print even numbers between 1 to n. How to write a C program to print even numbers between 1 to n. C program to print even number's between 1 to n.


Input from user: 

Enter the limit: 20

Expected output:

Even numbers between 1 to 20:

2
4
6
8
10
12
14
16
18
20





  Step by step logic to print even numbers between 1 to n(without using if-statement):



1. Accept limit from user store it in variable no.

2. Run for loop which iterate upto n number's. for(i=2;i<=no;i+=2)

3. We used i=2 because even number's starts from 2.

4.  We increment loop by two to print even numbers.

5. Print value of i inside the for loop.




  Program to print even numbers without using if-statement:


#include<stdio.h>
int main()
{

int i,no;

printf("Enter the limit: ");
scanf("%d",&no);

printf("Even numbers between 1 to %d\n",no);

for(i=2;i<=no;i+=2)
{
printf("%d\n",i);

}

return 0;
}




  Program to print even numbers using if-statement:


In below program we used if-statement to print even numbers from 1 to n.


#include<stdio.h>
int main()
{

 int i,no;

 printf("Enter the limit: ");
 scanf("%d",&no);

  printf("Even numbers between 1 to %d\n",no);

 for(i=1;i<=no;i++)
 {
   if(i%2==0)
   printf("%d\n",i);

 }

 return 0;
}



Above two programs will show the following output:


C program to print even numbers from 1 to n,  Even numbers from 1 to n

C program to print sum of 1 to n natural numbers

Ex: Write a C program to print sum of 1 to n natural numbers. C program to print sum of 1 to n natural numbers. How to write a C program to print sum of 1 to n natural numbers.


Input from user:

Enter the limit: 10


Expected output:

Sum is 55


  Step by step logic of the given program:

1.  First declare three intiger variable's no(accept limit), i(use in for loop) and sum(store addition of n natural numbers).
2. Accept limit from user.
3. Use for loop to get 1 to n natural numbers.
 for(i=1;i<=no;++i)
4. Add natural numbers one by one and store this in variable sum (sum=sum+i)
5. Print sum out of the for loop to print sum of all natural numbers.



    C program to print sum of 1 to n natural numbers:

#include<stdio.h>
int main()
{

int no,i,sum=0;


printf("Enter the limit:");
scanf("%d",&no);

for(i=1;i<=no;++i)
{
  sum=sum+i;
}
printf("\nsum is %d",sum);

return 0;
}



Above prgram shows the following output:






C Program to Print 1 to n Natural Numbers

Ex: Write a C program to print 1 to n natural numbers. How to write a C program to print 1 to n natural numbers. C program to print 1 to n natural numbers.

Input from user:

Enter the limit: 20

Expected output:

Natural numbers from 1 to 20:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20



     Step by step logic to print 1 to n natural numbers:


1. First declare two intiger variable's no for accept limit from user and i variable use in for loop.

2. Accept limit from user.

3. After that use one for loop to print 1 to n natural numbers.
loop like for(i=1; i<=no;i++)


     How It Works?

i=1 because we  need to print natural numbers from 1.

i<=no because we need to print natural numbers upto n. (n=no=20). 

i++ =i+1 because we need to increment it by one. 

 i=1=i+1=2=i+1=3=i+1=4=i+1=5...upto n.


4. Print value of i inside the for loop. Because we need to print natural numbers from 1 to n. (i will iterate from 1 to n)





    C program to print 1 to n natural numbers :



#include <stdio.h>
int main()
{
  int no,i;
  
  printf("Enter the limit:");
  scanf("%d",&no);
  
  printf("\nNatural numbers from 1 to %d : ",no);
  
  for(i=1;i<=no;i++)
  {
  printf("%d ",i);
  }
  return 0;

}




Above prgram shows the following output:





Loop's In C with Exercises and Examples

We use loops in program when we need to execute one or more statements multiple times until some conditions are satisfied. Means sometimes we want some part of code to be executed multiple times then we need to use loops.


  There are 3 types of loops in C-






Above three type of loops are used in C programming. Using this loops we can execute conditions multiple  times. below is the list of for, while and do-while loops in C programming examples.

So let's understand the the working of  loops with examples...


  Loops Exercises and Example's:


  Next: Array in C

do...while loop in C


In privious post we learned working of for loop and while loop, in this post we learn working of do....while loop.

do..while loop is similar to while loop only difference is-
do...while loop check condition at the end of loop. That means the statement inside the do...while loop are executed at least once even if condition is false.




    Syntax of do...while loop:



Syntax of do..while loop is shown above.




    Example program of do-while loop:


#include<stdio.h>
void main()
{
int i;
do 
{
   printf("You are inside the do while loop\n");
   printf("Press 1:to exit from the loop\n");
   scanf("%d",&i);
}
while(i!=1);

printf("You are out of the do while loop\n");
}


Above program shows the following output:





While loop In C


In previous post we learnt for loop, in this post we will learn working of  while loop.
While loop is  used when you want to execute a block of code repeatedly with a checked condition before making an iteration. In simple language while loop is a entry  controlled looping statement.


    Syntax of while loop -



Syntax of while loop is shown above.


    Example program of while loop: 


#include<stdio.h>
void main()
{
 int i=1;
 while(i<=10)
 {
 printf(" Hello ");
 i++;
 }
}

Here, until test counter condition is true the statements within while loop will execute, that means until i is less than or equal to 10 it will print Hello. So it will print hello 10 times. When condition becomes false control will be transferred to the next line after the loop.



Above program shows the following Output:



for loop in C


The for loop is a loop which is mostly used in any programming languages.
because for loop is easy to use and understand.
For loop is used to repeate a specific block of code a known number of times.

Let's understand with the syntax of for loop-






   Example: program to print series of  1  to 10 numbers using  for loop.


    
#include<stdio.h>
void main()
{
int i;
for(i=1;i<=10;i++)
 {
printf("%d ",i);
 }
}

Above program shows the following Output: