Example : A program in C to find out the simple interest of given values using a while/do-while loop with a yes-no option.
#include<stdio.h>
#include<conio.h>
int main()
{
int p,r,t,si;
char ch,x=1;
while(x)
{
printf("\nEnter the value of P,r,t: \n");
scanf("%d%d%d",&p,&r,&t);
si=(p*r*t)/100;
printf("The SI is = %d",si);
printf("\n\nDo You want to enter another value,press Y/N = ");
ch=getche();
if(ch=='Y'||ch=='y')
{
x=1;
}
else
{
x=0;
}
}
return 0;
}
/*----------------------------------- OR ----------------------------------*/
#include<stdio.h>
#include<conio.h>
int main()
{
int p,r,t,si;
char ch,x=1;
do
{
printf("\nEnter the value of P,r,t: \n");
scanf("%d%d%d",&p,&r,&t);
si=(p*r*t)/100;
printf("The SI is = %d",si);
printf("\n\nDo You want to enter antoher value,press Y/N = ");
ch=getche();
if(ch=='Y'||ch=='y')
{
x=1;
}
else
{
x=0;
}
}while(x);
return 0;
}
Example : Write a program in C to display the Infinite loop concept.
#include <stdio.h>
int main()
{
while (1)
{
printf("This is an infinite loop example.\n");
}
return 0;
}
/*----------------------------------- OR ----------------------------------*/
#include <stdio.h>
int main()
{
for( ; ; )
{
printf("This is an infinite loop example.\n");
}
return 0;
}
/*----------------------------------- OR ----------------------------------*/
#include <stdio.h>
int main()
{
do
{
printf("This is an infinite loop example.\n");
} while(1);
return 0;
}
Example : Write a program in C to display the Infinite loop concept using the goto statement.
#include <stdio.h>
void main()
{
int i = 12;
InfiniteGoto(i);
}
void InfiniteGoto(int num)
{
if (num%2 == 0)
goto even;
else
goto odd;
even:
printf("The number is even.\t");
goto even;
odd:
printf("The number is odd.\t");
goto odd;
}
Example : Write a program in C to display the Infinite loop concept using a Macro statement.
#include <stdio.h>
#define InfiniteMacro for(;;)
int main()
{
InfiniteMacro
{
printf("Infinite loop concept using Macro");
}
return 0;
}
Example : Write a program in C to display all the even numbers in between two numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, num1, num2;
printf("Enter first number : ");
scanf("%d", &num1);
printf("Enter second number : ");
scanf("%d", &num2);
for(i=num1;i<=num2;i++)
{
if(i%2==0)
{
printf("%d\t", i);
}
}
getch();
}
Example : Write a program in C to display all the odd numbers in between two numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, num1, num2;
printf("Enter first number : ");
scanf("%d", &num1);
printf("Enter second number : ");
scanf("%d", &num2);
for(i=num1;i<=num2;i++)
{
if(i%2!=0)
{
printf("%d\t", i);
}
}
getch();
}
Example : Write a program in C to count the total no. of odd and even no. present in a given range of values.
#include<stdio.h>
#include<conio.h>
void main()
{
int min,max,i,counte=0,counto=0;
clrscr();
printf("Ente two no., first min value and second max value :-");
scanf("%d%d",&min,&max);
for(i=min;i<=max;i++)
{
if(i%2==0)
{
// counte=counte+1;
counte++;
}
if(i%2!=0)
{
// counto=counto+1;
counto++;
}
}
printf("The total even no is %d\t",counte);
printf("\nThe total odd no is %d\t",counto);
getch();
}
Example : Write a program in C to print the sum of natural no. up to a given value.
#include<stdio.h>
#include<conio.h>
void main()
{
int num, i, sum = 0;
clrscr();
printf("Enter a positive integer: ");
scanf("%d",&num);
for(i=1; i<=num; i++)
{
sum= sum+i;
}
printf("Sum of total given natural no. are = %d", sum);
getch();
}
NB: Natural numbers are positive integers started from 1. eg- 1,2,3,4,5...
Example : Write a program in C to print the Sum total between a given range of values.
#include<stdio.h>
#include<conio.h>
void main()
{
int num1,num2,i,sum=0;
clrscr();
printf("Enter first value :");
scanf("%d",&num1);
printf("Enter second value :");
scanf("%d",&num2);
for(i=num1+1;i<num2;i++)
{
sum=sum+i;
}
printf("Sum of between value is =%d",sum);
getch();
}
Example : Write a program in C to print the power/exponent result of a given base value without using a function.
#include <stdio.h>
#include <conio.h>
int main()
{
int base, exp;
double res=1;
printf("Enter a base number: ");
scanf("%d", &base);
printf("Enter a power/exponent value: ");
scanf("%d", &exp);
while(exp!=0)
{
res=res*base;
exp--;
}
printf("The result is = %lf", res);
return 0;
}
Example : Write a program in C to print a table of a given value.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, table;
printf("Enter a number to print table: ");
scanf("%d", &table);
for(i=1; i<=10; i++)
{
printf("%d ", table*i);
}
getch();
}
Output:
Enter a number to print table: 5
5 10 15 20 25 30 35 40 45 50
/*--------------------------- OR ------------------------- */
#include<stdio.h>
#include<conio.h>
void main()
{
int i, table;
printf("Enter a number to print table: ");
scanf("%d", &table);
for(i=table; i<=table*10; i=i+table)
{
printf("%d ", i);
}
getch();
}
Output:
Enter a number to print table: 5
5 10 15 20 25 30 35 40 45 50
/*--------------------------- OR ------------------------- */
#include<stdio.h>
#include<conio.h>
void main()
{
int i, table;
printf("Enter a number to print table: ");
scanf("%d", &table);
for(i=1; i<=10; i++)
{
printf("%d x %d = %d\n", table, i, (table*i));
}
getch();
}
Output:
Enter a number to print table: 5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
Example : Write a program in C to print all the tables (values) from a given range of values or between two values horizontally or vertically.
#include<stdio.h>
#include<conio.h>
void main()
{
int min,max,i,j;
//clrscr();
printf("Enter first (small) table value = ");
scanf("%d",&min);
printf("Enter second (large) table value = ");
scanf("%d",&max);
printf("\nThe horizontal form of tables are =\n\n");
for(i=min;i<=max;i++)
{
for(j=i;j<=i*10;j=j+i)
{
printf(" %d ",j);
}
printf("\n\n");
}
getch();
}
Output:
Enter first (small) table value = 2
Enter second (large) table value = 8
The horizontal form of tables are =
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
/*--------------------------- OR ------------------------- */
#include<stdio.h>
#include<conio.h>
void main()
{
int tab,i,j;
//clrscr();
printf("Enter a value upto which you want a table vertically from 1 = ");
scanf("%d",&tab);
printf("\nThe vertical form of tables are =\n\n");
for(i=1;i<=10;i++)
{
for(j=1;j<=tab;j=j+1)
{
printf("%d\t",i*j);
}
printf("\n");
}
getch();
}
Output:
Enter a value upto which you want a table vertically from 1 = 5
The vertical form of tables are =
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
6 12 18 24 30
7 14 21 28 35
8 16 24 32 40
9 18 27 36 45
10 20 30 40 50
/*--------------------------- OR ------------------------- */
#include<stdio.h>
#include<conio.h>
void main()
{
int min,max,i,j;
//clrscr();
printf("Enter first (small) table value = ");
scanf("%d",&min);
printf("Enter second (large) table value = ");
scanf("%d",&max);
printf("\nThe vertical form of tables are =\n\n");
for(i=1;i<=10;i++)
{
for(j=min;j<=max;j=j+1)
{
printf("%d\t",i*j);
}
printf("\n");
}
getch();
}
Output:
Enter first (small) table value = 2
Enter second (large) table value = 7
The vertical form of tables are =
2 3 4 5 6 7
4 6 8 10 12 14
6 9 12 15 18 21
8 12 16 20 24 28
10 15 20 25 30 35
12 18 24 30 36 42
14 21 28 35 42 49
16 24 32 40 48 56
18 27 36 45 54 63
20 30 40 50 60 70
Example : Write a program in C to print the Factorial of a given value.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,num;
long int fact=1;
clrscr();
printf("Enter a value for factorial:");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
fact=fact*i;
}
printf("Factorial of %d is %ld",num,fact);
getch();
}
Output:
Enter a value for factorial:5
Factorial of 5 is 120
/*--------------------------- OR ------------------------- */
#include<stdio.h>
#include<conio.h>
void main()
{
int i,num;
long int fact=1;
//clrscr();
printf("Enter a value for factorial:");
scanf("%d",&num);
for(i=num;i>=1;i--)
{
fact=fact*i;
}
printf("Factorial of %d is %ld",num,fact);
getch();
}
Output:
Enter a value for factorial:5
Factorial of 5 is 120
Example : Write a program in C to print and count Fibonacci Series values up to a given number using a for loop.
#include<stdio.h>
#include<conio.h>
void main()
{
int a=0,b=1,c=0,i,n,count=2;
//clrscr();
printf("Enter a number to display fibonacci series upto/below :- ");
scanf("%d",&n);
printf("The fibonacci series upto/below %d is = ",n);
printf("\n%d\t%d\t",a,b);
for(;c<=n;)
{
c=a+b;
if(c<=n)
{
printf("%d\t",c);
a=b;
b=c;
count++;
}
}
printf("\n\nTotal no. of fibonacci values upto/below %d is = %d",n,count);
getch();
}
Output:
Enter a number to display fibonacci series upto/below :- 35
The fibonacci series upto/below 35 is = 10
0 1 1 2 3 5 8 13 21 34
Total no. of fibonacci values upto/below 35 is = 10
Example : Write a program in C to print the Fibonacci Series up to a given number using a while loop.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,a=0,b=1,c=0,i;
clrscr();
printf("Enter a no up to which you want the series:- ");
scanf("%d",&n);
printf("%d\t%d",a,b);
while(c<=n)
{
c=a+b;
if(c<=n)
{
printf("%d\t",c);
a=b;
b=c;
}
}
getch();
}
/*--------------------------- OR ------------------------- */
#include<stdio.h>
#include<conio.h>
void main()
{
int a=0,b=1,c=0,n,count=2;
//clrscr();
repeat:
printf("Enter a number(>2) to display total n fibonacci series : ");
scanf("%d",&n);
if(n<=2)
goto repeat;
printf("The first %d fibonacci series values are = ",n);
printf("\n%d\t%d\t",a,b);
while(count<n)
{
c=a+b;
printf("%d\t",c);
a=b;
b=c;
count++;
}
getch();
}
Output:
Enter a number(>2) to display total n fibonacci series : 8
The first 8 fibonacci series values are =
0 1 1 2 3 5 8 13
Example : Write a program in C to check whether the user-given single value is a Prime number or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,count=0;
//clrscr();
printf("Enter a value to check whether it is prime or not:- ");
scanf("%d",&n);
for(i=2;i<n;i++)
{
if(n%i==0)
{
count++;
}
}
if(count==0)
{
printf("\nThe given no. %d is Prime no.",n);
}
else
{
printf("\nThe given no. %d is not a Prime no.",n);
}
getch();
}
------------------ OR -----------------
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,count=0;
//clrscr();
printf("Enter a value to check whether it is prime or not:- ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
{
count++;
}
}
if(count==2)
{
printf("\nThe given no. %d is Prime no.",n);
}
else
{
printf("\nThe given no. %d is not a Prime no.",n);
}
getch();
}
Output:
Enter a value to check whether it is prime or not:- 61
The given no. 61 is Prime no.
Example : Write a program in C to list and count the total Prime no. among two values.
#include<stdio.h>
#include<conio.h>
void main()
{
int m,n,i,j,count1=0;
//clrscr();
printf("Enter two values[first one smaller]to find out Prime no. among them : ");
scanf("%d%d",&m,&n);
printf("\nThe Prime no. among %d and %d are : \n",m,n);
for(i=m;i<=n;i++)
{
int count=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
{
count++;
}
}
if(count==2)
{
printf(" %d ",i);
count1++;
}
}
printf("\n\nThe total no. of Prime no. among %d and %d is = %d",m,n,count1);
getch();
}
Output:
Enter two values[first one smaller] to find out Prime no. among them : 4
25
The Prime no. among 4 and 25 are :
5 7 11 13 17 19 23
The total no. of Prime no. among 4 and 25 is = 7
Example : Write a program in C to check whether the user-given value is a Perfect Number or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,sum=0;
//clrscr();
printf("Enter a value:- ");
scanf("%d",&n);
for(i=1;i<n;i++)
{
if(n%i==0)
{
sum=sum+i;
}
}
if(n==sum)
{
printf("Given no. is prefect no.");
}
else
{
printf("Given no. is not perfect no.");
}
getch();
}
--------------- OR ----------------
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,sum;
for(i=2;i<=65000;i++)
{
sum=0;
for(j=1;j<i;j++)
{
if(i%j==0)
{
sum=sum+j;
}
}
if(i==sum)
{
printf("%d ",i);
}
}
getch();
}
Output:
6 28 496 8128
NB: A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself such as, the number 6 is a perfect no. and is divisible by 1, 2, and 3 completely and its sum is equal to 6.
Example : Write a program in c to check whether the user-given single value is a Palindrome Number or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,sum=0,temp;
clrscr();
printf("Enter a value:- ");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
{
printf("The given value is Palindrome no.");
}
else
{
printf("The given value is not a Palindrome no.");
}
getch();
}
Example : Write a program in C to display the reverse of the user given a single numeric value.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,rem,sum=0;
clrscr();
printf("Enter a no:- ");
scanf("%d",&n);
while(n>0)
{
rem=n%10;
n=n/10;
sum=(sum*10)+rem;
}
printf("%d",sum);
getch();
}
Example : Write a program in c to display all the Lowercase/Small letters from a to z.
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
printf("Small Letters from a to z are: \n");
for(i=97;i<=122;i++)
{
printf("%c\t", i);
}
getch();
}
NB: Using ASCII concept.
/* ----------------------- OR ------------------------- */
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
printf("Lowercase Letters from a to z are: \n");
for(ch='a'; ch<='z'; ch++)
{
printf("%c\t", ch);
}
getch();
}
Example : Write a program in c to display all the Uppercase/Capital letters from A to Z.
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
printf("Capital Letters from A to Z are: \n");
for(i=65;i<=90;i++)
{
printf("%c\t", i);
}
getch();
}
/*----------------------- OR ------------------------ */
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
printf("Capital Letters from A to Z are: \n");
for(ch='A'; ch<='Z'; ch++)
{
printf("%c\t", ch);
}
getch();
}
Example : Write a program in C to display the largest and smallest numbers from all the input numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int x,i,max, min, input;
printf("Enter value for total entry :");
scanf("%d",&x);
printf("Enter %d values to find max and min :",x);
scanf("%d",&input);
max=input;
min=input;
for(i=2;i<=x;i++)
{
scanf("%d",&input);
if(input>max)
{
max=input;
}
if(input<min)
{
min=input;
}
}
printf("The Largest no. is %d\t",max);
printf("\nThe Smallest no. is %d\t",min);
getch();
}
Example : Write a program in C to display the ASCII values of all characters.
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=0;i<=255;i++)
{
printf("ASCII value of %c = %d\n", i, i);
}
getch();
}
Example : Write a program in C to display the sum of each digit of a given number[such as 402=4+0+2=6].
#include<stdio.h>
#include<conio.h>
void main()
{
int num, sum;
printf("Enter a number: ");
scanf("%d",&num);
for(sum=0; num>0; num=num/10)
sum=sum+(num%10);
printf("Sum of each digit in the given no. is : %d", sum);
getch();
}
Example : Write a program in C to count the no. of digits in an accepted number with/without a loop [such as 4021=4].
#include<stdio.h>
#include<conio.h>
void main()
{
int num,count = 0;
printf("Enter a number: ");
scanf("%d", &num);
while(num>0)
{
count++;
num = num/10;
}
printf("The total digits in the given no is : %d ", count);
getch();
}
/* ---------------------------------- OR ------------------------------------ */
#include <stdio.h>
#include <stdio.h>
void main()
{
int num,num1;
int count=0;
clrscr();
printf("Enter an integer value: ");
scanf("%d",&num);
num1=num;
while(num!=0)
{
num=num/10;
count++;
}
printf("Total number of digits in %d is %d",num1,count);
getch();
}
/* ---------------------------------- OR ------------------------------------ */
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int num,count = 0;
printf("Enter a number: ");
scanf("%d", &num);
count = log10(num)+1;
printf("The total digits in the given no is : %d ", count);
getch();
}
NB: The log10() function computes the base 10 logarithm of an argument.
Example : Write a program in C to check and display whether the accepted number is Armstrong or not.
#include <stdio.h>
#include <conio.h>
int main()
{
int num,num1,rem,result=0;
printf("Enter an integer value: ");
scanf("%d",&num);
num1=num;
while(num1!=0)
{
rem=num1%10;
result=result+(rem*rem*rem);
num1=num1/10;
}
if(result==num)
printf("%d is an Armstrong number",num);
else
printf("%d is not an Armstrong number",num);
return 0;
}
---------------- OR ----------------
#include <stdio.h>
#include <conio.h>
int main()
{
int num,num1,rem,result;
for(num=1;num<=65000;num++)
{
num1=num;
result=0;
rem=1;
while(num1!=0)
{
rem=num1%10;
result=result+(rem*rem*rem);
num1=num1/10;
}
if(result==num)
printf("%d ",num);
}
return 0;
}
NB : Armstrong number is the number whose sum of cube of each digit is similar/equal to the original number. eg-0,1,153,370,371,407 [(such as 153=(1*1*1)+(5*5*5)+(3*3*3))= 153]
Example : Write a program in C to convert a Binary number into its equivalent Decimal number.
#include<stdio.h>
#include<conio.h>
void main()
{
long num, backup, remainder, result = 0, base = 1;
printf("Enter a binary number only : ");
scanf("%ld", &num);
backup = num;
while (num > 0)
{
remainder = num%10;
result = result+remainder*base;
num = num/10 ;
base = base*2;
}
printf("\n\nThe decimal value of given binary number %d is %d", backup,result);
getch();
}
Output:
Enter a binary number only : 111
The decimal value of given binary number 111 is 7
Example : Write a program in C to convert a Decimal number into its equivalent Binary number and also count no. of 1’s in it.
#include<stdio.h>
#include<conio.h>
void main()
{
long num, result, remainder, base=1, backup, count=0;
printf("Enter a decimal number : ");
scanf("%ld", &num);
backup = num;
while (num>0)
{
remainder=num%2;
if (remainder==1)
{
count++;
}
result = result + remainder * base;
num = num/2;
base = base*10;
}
printf("\nThe equivalent binary no. of given decimal no. %d is %ld\n\n",backup,result);
printf("The total no.of 1's in the equivalent binary number %d is = %d\n", result,count);
getch();
}
Output:
Enter a decimal number : 5
The equivalent binary no. of given decimal no. 5 is 101
The total no. of 1's in the equivalent binary number 101 is = 2
Example : Write a program in c to check whether the given value is Integer or Float/Real.
#include <stdio.h>
#include <stdlib.h>
void main()
{
char value[15];
int i = 0;
printf("Enter your value : ");
scanf("%s",value);
while(i < 15)
{
if(value[i] == '.')
{
printf("The given value is float\n");
exit(1);
}
i = i + 1;
}
printf("The given value is Integer\n");
getch();
}
/* ---------------------------- OR ------------------------------- */
#include<string.h>
#include<conio.h>
void main()
{
char value[15];
int flag=0;
int length;
printf("Enter a value \n");
scanf("%s",value);
length=strlen(value);
while(length--)
{
if(value[length]=='.')
{
flag=1;
break;
}
}
if(flag)
printf("The given value is Floating point number\n");
else
printf("The given value is Integer number\n");
getch();
}
/* --------------------------- OR ----------------------------- */
#include<stdio.h>
#include<math.h>
void main()
{
float value;
printf("Enter a number\n");
scanf("%f",&value);
if((value - (int)value)== 0)
printf("Entered number is Integer");
else
printf("Entered number is Float");
getch();
}
/* ------------------------ OR --------------------------- */
#include<stdio.h>
#include<math.h>
void main()
{
float value1;
int value2;
printf("Enter a number\n");
scanf("%f",&value1);
//value2 = ceil(value1); //ceil() converts float value to its next whole no. such as 26.32=27.
value2 = floor(value1); //floor() converts float value to its whole no. such as 26.82=26.
printf("%d",value2);
if((value1-value2)== 0)
printf("The given number is Integer\n");
else
printf("The given number is Float\n");
getch();
}
/* ----------------------- OR ----------------------- */
#include<stdio.h>
#include<math.h>
void main()
{
float value;
int num;
printf("Enter a number : ");
scanf("%f",&value);
num=value;
if(num==value)
printf("\nThe given value is Intiger");
else
printf("\nThe given value is Float");
getch();
}
0 Comments