Ex: write a C program to print number if it is less than or equal to 5 using do while loop. How to write a C program to print number if it is less than or equal to 5 using do while loop. C program to print number if it is less than or equal to 5 using do while loop.
Note:
do-while loop runs at least one even if the condition is false because the condition is evaluated, after the execution of the body of loop.
Step by step logic of the given program:
1. First declare intiger variable i=1.
2. After that inside the do while print value of i.
3. Increment value of i.
Give condition while(i<=5).
Program to print numbers till 5 using do-while loop:
#include<stdio.h>
int main()
{
int i=1;
do
{
printf("%d\n",i);
i++;
}while(i<=5);
return 0;
}
Above prgram shows the following output:
Ex: Write a C program to print Fibonacci series. How to write a C program to print Fibonacci series. C program for Fibonacci series.
Input from user:
Enter number for how many times generate series: 7
Expected output:
Fibonacci series is: 0 1 1 2 3 5 8 13 21
Step by step logic of the given program:
1. First declare intiger variable's no(number), a=0, b=1, c, i .
2. After that accept input (number) from user.
3. print value of a and b directly. (and decrease number by 2 in the for loop)
4. After that use one for loop which looks like.
for(i=0;i<no-2;i++)
5. Inside the for loop Fibonacci series calculate using..
c=a+b;
a=b;
b=c;
6. After that Print value of c inside the for loop.
C program to print Fibonacci series :
#include<stdio.h>
int main()
{
int no,a=0,b=1,c,i;
printf("Enter number for how many times generate series:");
scanf("%d",&no);
printf("FIBONACCI SERIES: ");
printf("%d %d ",a,b);
for(i=0;i<no-2;i++)
{
c=a+b;
a=b;
b=c;
printf("%d ",c);
}
return 0;
}
Above prgram shows the following output:
Ex: Write a C program to print product of digits of a number. How to write a C program to print product of digits of a number. C program to find product of digits of number.
Input from user:
Enter the number: 12345
Expected output:
Product of given number is: 120
Step by step logic of the given program:
1. Accept input (number) from user.
2. Use one while loop to check given number is greater than 0 or not.
3. Loop will iterate if number is greater than 0.
4. Inside the while loop product of digit calculate using...
z=no%10;
product*=z;
no=no/10;
5. Print product on the output screen.
C Program to find product of digits of a number:
#include<stdio.h>
int main()
{
int no,z;
long long int product=1ll;
printf("Enter the number\n");
scanf("%d",&no);
while(no!=0)
{
z=no%10;
product*=z;
no=no/10;
}
printf("product of given number is:%lld",product);
return 0;
}
Above prgram shows the following output:
Ex: write a C program to check given number is palindrome or not. How to write a C program to check given number is palindrome or not. C Program to check given number is palindrome or not.
Input from user:
Enter the number: 12321
Expected output:
Given number is palindrome.
Step by step logic of the given program:
1. Accept input (number) from user.
2. After that use one while loop to check given number is greater than zero or not.
3. If number is greater than 0 then while loop will iterate.
4. Use reverse number logic inside the loop to calculate reverse number.
reverse=reverse*10; reverse=reverse+(no % 10);
no=no/10;
5. After that using if statement check condition
if(reverse==num)
6. Then print given number is palindrome. Otherwise print given numbers is not palindrome.
C program to check given number is palindrome or not:
#include<stdio.h>
int main()
{
int no,num,reverse=0;
printf("Enter the number:");
scanf("%d",&no);
num=no;
while(no!=0)
{
reverse=reverse*10; reverse=reverse+(no % 10);
no=no/10;
}
if(reverse==num)
{
printf("Given number is palindrome");
}
else
{
printf("Given number is not palindrome");
}
return 0;
}
Above prgram shows the following output:
Ex: Write a C program for reverse a given number. How to write a C program to reverse a given number. C program for reverse a given number.
Input from user:
Enter the number: 12345
Expected output:
Given number in reverse order: 54321
Step by step logic of the given program:
1. Accept input (number) from user.
2. Use one while loop to check given number is greater than 0 or not.
3. After that inside the while loop add reverse number logic.
4. Inside the loop the reverse number computed using..
reverse=reverse*10;
reverse=reverse+no%10;
no=no/10;
5. Print reverse number on the output screen.
C program to reverse a given number using while loop:
#include<stdio.h>
int main()
{
int no,reverse=0;
printf("Enter the number\n");
scanf("%d",&no);
while(no>0)
{
reverse=reverse*10;
reverse=reverse+no%10;
no=no/10;
}
printf("Given number in reverse order:%d",reverse);
return 0;
}
Above program shows the following output:
Ex: write a C program to print digit sum. How to write a C program to print digit sum. C program to print digit sum.
Input from user:
Enter the number: 145
Expected output:
Sum of digit=10
Step by step logic of the given program:
1. Accept number from user.
2. Use while loop to check given number is greater than zero or not.
3. Enter given digit sum logic inside the while loop.
z=no%10;
sum=sum+z;
no=no/10;
4. Print sum outside the while loop.
C program to print digit sum using while loop:
#include<stdio.h>
void main()
{
int no,z,sum=0;
printf("Enter the number:");
scanf("%d",&no);
while(no>0)
{
z=no%10;
sum=sum+z;
no=no/10;
}
printf("sum of digit=%d",sum);
}
Above prgram shows the following output:
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:
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:
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:
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: