C Program To Check Odd or Even
C Program To Check Odd or Even
#include<stdio.h>
#include<conio.h>
void main()
{
int no;
clrscr();
printf("Enter any number: ");
scanf("%d",&no);
if(no%2==0)
{
printf("Even num");
}
else
{
printf("Odd num");
}
getch();
}
void main()
{
int a,b,c;
clrscr();
printf("Enter value of a: ");
scanf("%d",&a);
printf("Enter value of b: ");
scanf("%d",&b);
c=a;
a=b;
b=c;
printf("After swapping a=: %d b=: %d",a,b);
getch();
}
void main()
{
int a,b;
clrscr();
printf("Enter value of a: ");
scanf("%d",&a);
printf("Enter value of b: ");
scanf("%d",&b);
a=a+b;
b=a-b;
a=a-b;
printf("After swapping \na=: %d\nb=: %d",a,b);
getch();
}
A Prime number is a natural number greater than 1 that has no positive divisors other than 1 and
itself. It is only divisible by 1 and itself, and it start from 2. The smallest prime number is 2.
void main()
{
int i,no;
clrscr();
printf("Enter any num: ");
scanf("%d",&no);
if(no==1)
{
printf("Smallest Prime no. is 2");
}
for(i=2;i<no;i++)
{
if(no%i==0)
{
printf("Not Prime no.");
break;
}
}
if(no==i)
{
printf("Prime no.");
}
getch();
}
void main()
{
int i,j=2,num;
clrscr();
printf("Enter any no.: ");
scanf("%d",&num);
printf("Next Prime no.: ");
for(i=num+1; i<3000; i++)
{
for(j=2; j<i; j++)
{
if(i%j==0)
{
break;
} // if
} // for
if(i==j || i==1)
{
printf("%d\t",i);
break;
} // if
} // outer for
getch();
}
void main()
{
char choice;
int a,b,res=0;
clrscr();
printf("Enter First value: ");
scanf("%d",&a);
printf("\n Enter Operator: ");
choice=getch();
printf("\n Enter Second value: ");
scanf("%d",&b);
switch(choice)
{
case '+':
res=a+b;
printf("Sum: %d",res);
break;
case '-':
res=a-b;
printf("Difference: %d",res);
break;
case '*':
res=a*b;
printf("Product: %d",res);
break;
case '/':
res=a/b;
printf("Quotient: %d",res);
break;
default:
printf("Enter Valid Operator!!");
}
getch();
}
Factorial of any number is the product of an integer and all the integers below it.For example
factorial of 4 is
4! = 4 * 3 * 2 * 1 = 24
void main()
{
int i, no, fact=1;
clrscr();
printf("Enter the any no. : ");
scanf("%d",&no);
for(i=1;i<=no;i++)
{
fact=fact*i;
}
printf("Factorial =%d ",fact);
getch();
}
void main()
{
int i,no,table=1;
clrscr();
printf("Enter any number : ");
scanf("%d",&no);
printf("Table of %d \n",no);
for(i=1;i<=10;i++)
{
table=no*i;
printf("%d",table);
printf("\n");
}
getch();
}
Reverse of number means reverse the position of all digits of any number. For example reverse
of 839 is 938
void main()
{
int no,rev=0,r,a;
clrscr();
printf("Enter any num: ");
scanf("%d",&no);
a=no;
while(no>0)
{
r=no%10;
rev=rev*10+r;
no=no/10;
}
printf("Reverse of %d is: %d",a,rev);
getch();
}
void main()
{
int no,a=0;
clrscr();
printf("Enter any number: ");
scanf("%d",&no);
while(no>0)
{
no=no/10;
a++;
}
printf("Number of digits in given number is: %d",a);
getch();
}
void main()
{
int i,no, first=0, second=1, next;
clrscr();
first=0;
second=1;
printf("Enter nubmer of terms for Series: ");
scanf("%d",&no);
printf("Fibonacci series are:\n");
for(i=0; i<no; i++)
{
printf("%d\n",first);
next = first + second;
first = second;
second = next;
}
getch();
}
For example:
Three Digits Armstrong number is 153, 1 ^ 3 + 5 ^ 3 + 3 ^ 3 = 153
Four Digits Armstrong number is 1634, 1 ^ 4 + 6 ^ 4 + 3 ^ 4 + 4 ^ 4 + = 1634
void main()
{
int temp, arm=0,a,b,c,d,num;
clrscr();
printf("Enter any number: ");
scanf("%d",&num);
temp=num;
while(temp>0)
{
a=temp%10;
temp=temp/10;
arm=arm+a*a*a;
}
if(arm==num)
{
printf("%d is armstrong number",num);
}
else
{
printf("%d is not armstrong number", num);
}
getch();
}
void main()
{
int a,b,c;
clrscr();
printf("Enter any three number: ");
scanf("%d%d%d",&a,&b,&c);
void main()
{
int a,b,c,largest;
clrscr();
printf("Enter any three numbers: ");
scanf("%d%d%d",&a,&b,&c);
largest=(a>b)?(a>c?a:c):(b>c?b:c);
printf("Largest number: %d",largest);
getch();
}
A palindrome number is a number that remains the same when its digits are reversed. Like
16461, for example: we take 121 and reverse it, after revers it is same as original number.
void main()
{
int a,no,b,temp=0;
clrscr();
printf("Enter any num: ");
scanf("%d",&no);
b=no;
for(;no>0;)
{
a=no%10;
no=no/10;
temp=temp*10+a;
}
if(temp==b)
{
printf("Palindrome number");
}
else
{
printf("Not Palindrome number");
}
getch();
}
void gcd(int,int);
void main()
{
int a,b;
clrscr();
printf("Enter two numbers: ");
scanf("%d %d",&a,&b);
gcd(a,b);
getch();
// return 0;
}
m=a;
n=b;
while(m!=n)
{
if(m>n)
m=m-n;
else
n=n-m;
}
Example
Suppose find LCM of 3 and 4
Multiple of 3 : 3, 6, 9, 12, 15, 18, 21, 24,.......
Multiple of 4 : 4, 8, 12, 16, 20, 24, 28,.....
void lcm(int,int);
void main()
{
int a,b;
clrscr();
printf("Enter two numbers: ");
scanf("%d %d",&a,&b);
lcm(a,b);
getch();
// return 0;
}
m=a;
n=b;
while(m!=n)
{
if(m < n)
{
m=m+a;
}
else
{
n=n+b;
}
}
void main()
{
int num,ans;
clrscr();
printf("Enter any number: ");
scanf("%d",&num);
ans=pow(num,0.5);
printf("\n Square root of %d is: %d",num,ans);
getch();
}
void main()
{
int num, ans;
clrscr();
printf("Enter any number: ");
scanf("%d",&num);
ans=pow(num, 1.0/3.0);
ans++;
printf("\n\Cube of %d is: %d",num,ans);
getch();
}
int main(void)
{
int number, result;
clrscr();
printf("Enter any Character/Symbol/Digits: ");
number = getch();
result = toascii(number);
printf("\nAscii value of %c is %d\n", number, result);
getch();
}
void main()
{
int a,no,sum=0;
clrscr();
printf("Enter any number: ");
scanf("%d",&no);
while(no>0)
{
a=no%10;
no=no/10;
sum=sum+a;
}
printf("\nSum of Digits: %d",sum);
getch();
}
void main()
{
char ch;
clrscr();
printf("Enter any charecter: ");
scanf("%c",&ch);
if(islower(ch))
{
printf("\n Lower case small letter");
}
else if(isupper(ch))
{
printf("Upper case capital letter");
}
else if(isdigit(ch))
{
printf("This number or digit");
}
else
{
printf("This is speciial symbol");
}
getch();
}
void main()
{
int y;
printf("Enter any year: ");
scanf("%d",&y);
if(y%4==0)
{
printf("Leap year");
}
else
{
printf("Not a leap year");
}
getch();
}
Formula
area of circle = 3.14*(radius*radius);
void main()
{
int radius;
float area_circle=0;
clrscr();
printf("Enter radius of circle: ");
scanf("%d",&radius);
area_circle=3.14*(radius*radius);
printf("\nArea of Circle: %f",area_circle);
getch();
}
Formula
Area of Rectangle = length*breath;
Parameter of Rectangle = 2*(length+breath);
void main()
{
int length,breath,area_rec=0,parameter=0;
clrscr();
printf("Enter Length and Breath of Rectangle: ");
scanf("%d%d",&length,&breath);
area_rec=length*breath;
parameter=2*(length+breath);
printf("\nArea of Ractangle: %d",area_rec);
printf("\nParameters of Rectangle: %d",parameter);
getch();
}
void main()
{
int no, i;
float marks[10], per=0, total=0;
clrscr();
printf("Enter number of subject: ");
scanf("%d",&no);
printf("Enter marks of %d subject: ",no);
for(i=0; i<no; i++)
{
scanf("%f",&marks[i]);
}
for(i=0; i<no; i++)
{
total=total+marks[i];
}
per=total/no;
printf("Percentage: %f %",per);
getch();
}
If percentage > 85 print A grade, If percentage < 85 && percentage >= 75 print B grade, If
percentage < 75 && percentage >= 50 print C grade, If percentage > 30 && percentage <= 50
print D grade, If percentage <30 print fail
void main()
{
int i,count=0;
char ch[20];
clrscr();
printf("Enter Any string: ");
gets(ch);
for(i=0;ch[i]!='\0';i++)
{
count++;
}
printf("Length of String: %d",count);
getch();
}
To compare any two string first we need to find length of each string and then compare both
strings. If both string have same length then comapre character by character of each string.
void main()
{
char str1[20],str2[20],i,j,flag=0;
clrscr();
printf("Enter first string: ");
gets(str1);
printf("Enter Second string: ");
gets(str2);
i=0;
j=0;
while(str1[i]!='\0')
{
i++;
}
while(str2[j]!='\0')
{
j++;
}
if(i!=j)
{
flag=0;
}
else
{
for(i=0,j=0;str1[i]!='\0',str2[j]!='\0';i++,j++)
{
if(str1[i]==str2[j])
{
flag=1;
}
}
}
if(flag==0)
{
printf("Strings are not equal");
}
else
{
printf("Strings are equal");
}
getch();
}
void main()
{
char str[100],temp;
int i,j=0;
clrscr();
printf("Enter any the string :");
gets(str); // gets function for input string
i=0;
j=strlen(str)-1;
while(i<j)
{
temp=str[i];
str[i]=str[j];
str[j]=temp;
i++;
j--;
}
printf("Reverse string is :%s",str);
getch();
}
void main()
{
char s1[100], s2[100], i;
clrscr();
printf("Enter string s1: ");
scanf("%s",s1);
for(i=0; s1[i]!='\0'; ++i)
{
s2[i]=s1[i];
}
s2[i]='\0';
printf("String s2: %s",s2);
getch();
}
for(i=0;i<n;i++)
{
sum=sum+arr[i];
}
for(i=0;i<n;i++)
{
}
printf("%d ",sum);
getch();
}
void main()
{
int arr[20],even[20],odd[20],i,j=0,k=0,no;
clrscr();
printf("Enter size of array: ");
scanf("%d",&no);
printf("Enter any %d elements in Array: ",no);
for(i=0; i<no;i++)
{
scanf("%d",&arr[i]);
}
for(i=0; i<no;i++)
{
if(arr[i]%2==0)
{
even[j]=arr[i];
j++;
}
else
{
odd[k]=arr[i];
k++;
}
}
printf("\nEven Elements: ");
for(i=0; i<j ;i++)
{
printf("%d ",even[i]);
}
printf("\nOdd Elements: ");
for(i=0; i<k; i++)
{
printf("%d ",odd[i]);
}
getch();
}
void main()
{
int i,j, k=1;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<i;j++)
{
printf("%d",k);
k++
}
printf("\n");
}
getch();
}
Output
1
23
456
78910
void main()
{
int i, j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",i);
}
printf("\n");
}
getch();
}
Output
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
void main()
{
int i,j,k;
k=1;
for(i=1;i<=5;i+=2)
{
for(j=5;j>=1;j--)
{
if(j>i)
printf(" ");
else
printf("%d ",k++);
}
printf("\n");
}
getch();
}
Output
1
2 3 4
5 6 7 8 9
void main()
{
int i, j;
clrscr();
for(i=1;i<=7;i+=2)
{
for(j=1;j<=i;j++)
printf("%d",j);
printf("\n");
}
for(i=5;i>=1;i-=2)
{
for(j=1;j<=i;j++)
printf("%d",j);
printf("\n");
}
getch();
}
Output
1
123
12345
1234567
12345
123
1
Example
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
printf("%d",i);
printf("\n");
}
getch();
}
Output
11111
22222
33333
44444
55555
Example
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j;
clrscr();
for(i=5;i>=1;i--)
{
for(j=5;j>=1;j--)
{
if(j>i)
printf(" ");
else
printf("%d",j);
}
printf("\n");
}
getch();
}
Output
54321
4321
321
21
1
void main()
{
int n, c, d, num = 1, space;
clrscr();
printf("Enter any number (1-10): ");
scanf("%d",&n);
space=n-1;
for(d=1; d<=n; d++)
{
num=d;
for(c=1; c<=space; c++)
printf(" ");
space--;
for(c=1; c<=d; c++)
{
printf("%d", num);
num++;
}
num--;
num--;
for(c=1; c<d; c++)
{
printf("%d", num);
num--;
}
printf("\n");
}
getch();
}
Output
Enter any number (1-10): 4
1
232
34543
4567654
void main()
{
int i,j,k;
clrscr();
for(i=1; i<=5; i++)
{
for(j=4; j>=i; j--)
{
printf(" ");
}
for(k=1; k<=(2*i-1); k++)
{
printf("*");
}
printf("\n");
}
getch();
}
Output
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
Print Star Pattern in C
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, k;
for(i=5;i>=1;i--)
{
for(j=5;j>i;j--)
{
printf(" ");
}
for(k=1;k<(i*2);k++)
{
printf("*");
}
printf("\n");
}
getch();
}
Output
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Print * Triangle in C
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1; i<=6; i++)
{
for(j=1; j<i; j++)
{
printf("*");
}
printf("\n");
}
getch();
}
Output
*
* *
* * *
* * * *
* * * * *
void main()
{
int i, j;
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
}
getch();
}
Output
* * * * *
* * * *
* * *
* *
*
print * pattern
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, k;
for(i=5;i>=1;i--)
{
for(j=1;j<i;j++)
{
printf(" ");
}
for(k=5;k>=i;k--)
{
printf("*");
}
printf("\n");
}
getch();
}
Output
*
* *
* * *
* * * *
* * * * *
void main()
{
int i, j, k;
for(i=5;i>=1;i--)
{
for(j=5;j>i;j--)
{
printf(" ");
}
for(k=1;k<=i;k++)
{
printf("*");
}
printf("\n");
}
getch();
}
Output
* * * * *
* * * *
* * *
* *
*
void main()
{
int i, j, k;
for(i=1;i<=5;i++)
{
for(j=i;j<5;j++)
{
printf(" ");
}
for(k=1;k<(i*2);k++)
{
printf("*");
}
printf("\n");
}
for(i=4;i>=1;i--)
{
for(j=5;j>i;j--)
{
printf(" ");
}
for(k=1;k<(i*2);k++)
{
printf("*");
}
printf("\n");
}
getch();
}
Output
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
void main()
{
int i, j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%c",'A' + j-1);
}
printf("\n");
}
getch();
}
Output
A
AB
ABC
ABCD
ABCDE
void main()
{
int i, j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%c",'A'-1 + i);
}
printf("\n");
}
getch();
}
Output
A
BB
CCC
DDDD
EEEEE
void main()
{
int i, j;
for(i=1;i<=5;i++)
{
for(j=5;j>=i;j--)
{
printf("%c",'A'-1 + i);
}
printf("\n");
}
getch();
}
Output
AAAAA
BBBB
CCC
DD
E
void main()
{
char s[]="india";
int i,j;
clrscr();
for(i=0;s[i];i++)
{
for(j=0;j<=i;j++)
printf("%c",s[j]);
printf("\n");
}
getch();
}
Output
I
IN
IND
INDI
INDIA
void main()
{
int i, j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
if(j%2==0) printf("A");
else printf("*");
}
printf("\n");
}
getch();
}
Output
*
*A*
*A*A*
*A*A*A*
void main()
{
int i,n,sum=0;
clrscr();
n=10;
for(i=1;i<=n;i++)
{
sum+=i;
}
printf("Sum: %d",sum);
getch();
}
Output
Sum: 55
Output
Sum: 385
void main()
{
int i,j,n,sum=0;
clrscr();
n=10;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
sum+=j;
}
}
printf("Sum: %d",sum);
getch();
}
Output
Sum:385
void main()
{
int bin,p,q,r,x;
clrscr();
bin=1;
q=0;
printf("\t\t\tDisplay pascal Triangle");
printf("\n\n\t\t Created By:- Hitesh Kumar");
printf("\n\n\nHow Many Row Do you want to input:");
scanf("%d",&r);
printf("\nPascal's Triangle:\n");
while(q<r)
{
for(p=40-3*q;p>0;--p)
printf(" ");
for(x=0;x<=q;++x)
{
if((x==0)||(q==0))
bin=1;
else
bin=(bin*(q-x+1))/x;
printf(" ");
printf("%d",bin);
}
printf("\n\n\n");
++q;
}
getch();
}