#include<stdio.h>
void main()
{
int n,i,j,flag;
printf("\n\nEnter the limit\t:");
scanf("%d",&n);
printf("\nThe prime numbers within the given limit:\n");
for(i=1;i<=n;i++)
{
flag=0;
for(j=2;j<=i/2;j++)
{
if(i%j==0)
{
flag=1;
break;
}
}
if(flag==0)
printf("%d\t",i);
}
}
Tuesday, June 30, 2015
Sunday, June 28, 2015
Solve Quadratic Equation
#include <stdio.h>
#include <math.h>
void main()
{
float a,b,c,d,temp,r1,r2;
printf("\nEnter the value of a\t:");
scanf("%f",&a);
printf("Enter the value of b\t:");
scanf("%f",&b);
printf("Enter the value of c\t:");
scanf("%f",&c);
d=((b*b)-(4*c*a));
if(d==0)
{
printf("\nRoots are real and equal\n");
r1=-b/(2*a);
printf("\t X1=%f \n \t X2=%f ", r1, r1 );
}
else if(d>0)
{
printf("\nRoots are real and distinct\n");
temp=sqrt(d);
r1=(-b+temp)/(2*a);
r2=(-b-temp)/(2*a);
printf("\n\tX1=%f\n\tX2=%f",r1,r2);
}
else
{
printf("\nRoots are Imaginary\n");
d*=-1;
temp=sqrt(d);
r1=-b/(2*a);
r2=temp/(2*a);
printf("\n\tX1=(%f + %fi)\n\tX2=(%f - %fi)",r1,r2,r1,r2);
}
}
Output
Enter the value of a :1
Enter the value of b :-11
Enter the value of c :30
Roots are real and distinct
X1=6.000000
X2=5.000000
Find the greatest of two numbers using Conditional Operator
Sumit Kar June 28, 2015 Basic, Conditional Operator No comments
Generate the Fibonacci Series
#include<stdio.h>
void main()
{
int t,i,m=0,n=1,f=1;
printf("\n\nEnter the number of terms:");
scanf("%d",&t);
printf("\n The Fibonacci Series of %d terms:\n",t);
for(i=1;i<=t;i++)
{
printf("%d\t",f);
f=m+n;
m=n;
n=f;
}
}
Output
Enter the number of terms:10
The Fibonacci Series of 10 terms:
1 1 2 3 5 8 13 21 34 55
Check whether an alphabet is Vowel or not
#include <stdio.h>
void main()
{
char in;
printf("Enter Character: ");
scanf("%c", &in);
if(in>=65&&in<=90 || in>=97&& in<=122)
{
if(in<97)
in=in+32;
if(in=='a'||in=='e'||in=='i'||in=='o'||in=='u')
printf("vowel");
else
printf("consonent");
}
else
printf("Not an valid Alphabet");
}
Output
Enter Character: A
vowel
Print the ASCII values of all the English Alphabets
#include<stdio.h>
void main()
{
char chl='a';
char chu='A';
int i;
printf("\n===========================");
for(i=1;i<=26;i++,chu++,chl++)
printf("\n|%c | %d \t|| %c| %d |",chl,chl, chu, chu);
printf("\n===========================");
}
Output
===========================
|a | 97 || A| 65 |
|b | 98 || B| 66 |
|c | 99 || C| 67 |
|d | 100 || D| 68 |
|e | 101 || E| 69 |
|f | 102 || F| 70 |
|g | 103 || G| 71 |
|h | 104 || H| 72 |
|i | 105 || I| 73 |
|j | 106 || J| 74 |
|k | 107 || K| 75 |
|l | 108 || L| 76 |
|m | 109 || M| 77 |
|n | 110 || N| 78 |
|o | 111 || O| 79 |
|p | 112 || P| 80 |
|q | 113 || Q| 81 |
|r | 114 || R| 82 |
|s | 115 || S| 83 |
|t | 116 || T| 84 |
|u | 117 || U| 85 |
|v | 118 || V| 86 |
|w | 119 || W| 87 |
|x | 120 || X| 88 |
|y | 121 || Y| 89 |
|z | 122 || Z| 90 |
===========================
Friday, June 26, 2015
Find Area and Circumference of a Circle
#include<stdio.h>
#define PI 3.142
void main()
{
float r,a,c;
printf("\nEnter the radius of circle:\t");
scanf("%f",&r);
a=PI*r*r;
printf("\nThe area of the circle:\t%f",a);
c=2*PI*r;
printf("\nThe circumference of the circle: %f",c);
}
Output
Enter the radius of circle: 2
The area of the circle: 12.568000
The circumference of the circle: 12.568000
Enter the radius of circle: 1
The area of the circle: 3.142000
The circumference of the circle: 6.284000
Friday, May 22, 2015
Print the Truth Table
Sumit Kar May 22, 2015 Basic, If Else, Switch Case, User Defined Function No comments
#include<stdio.h>
int OR(int,int);
int AND(int,int);
int XOR(int,int);
int NOT(int);
void main()
{
int a,b,ch,o;
printf("\n-:MENU:-\n1.AND\n2.OR\n3.NOT\n4.XOR\n0.Exit\n");
do{
printf("\nEnter Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf ("\nTruth Table for AND\nA B \tO");
for(a=0;a<=1;a++)
{
for(b=0;b<=1;b++)
{
o=AND(a,b);
printf("\n%d %d \t%d",a,b,o);
}
}
break;
case 2:
printf ("\nTruth Table for OR\nA B \tO");
for(a=0;a<=1;a++)
{
for(b=0;b<=1;b++)
{
o=OR(a,b);
printf("\n%d %d \t%d",a,b,o);
}
}
break;
case 3:
printf ("\nTruth Table for NOT\nA \tO");
for(a=0;a<=1;a++)
{
o=NOT(a);
printf("\n%d \t%d",a,o);
}
break;
case 4:
printf ("\nTruth Table for XOR\nA B \tO");
for(a=0;a<=1;a++)
{
for(b=0;b<=1;b++)
{
o=XOR(a,b);
printf("\n%d %d \t%d",a,b,o);
}
}
break;
case 0: exit();
break;
default: printf("\nError");
}
}while(ch!=0);
}
int OR(int a, int b)
{
if(a==0&&b==0)
return 0;
else
return 1;
}
int AND(int a, int b)
{
if (a==1&&b==1)
return 1;
else
return 0;
}
int NOT(int a)
{
if (a==0)
return 1;
else
return 0;
}
int XOR(int a,int b)
{
if (a==0&&b==1||a==1&&b==0)
return 1;
else
return 0;
}
Output
-:MENU:-
1.AND
2.OR
3.NOT
4.XOR
0.Exit
Enter Choice: 3
Truth Table for NOT
A O
1 0
0 1
Thursday, May 21, 2015
Simple Interest
#include<stdio.h>
void main()
{
float p;
float ir,t;
float in;
printf("Principle amount : ");
scanf("%f",&p);
printf("\nInterest : ");
scanf("%f",&ir);
printf("\nTime (in year) : ");
scanf("%f",&t);
in=p*ir*t/100;
printf("\nYou will get interest : Rs. %.2f",in);
}
Output
Principle amount : 5000
Interest : 10
Time (in year) : 3
You will get interest :Rs. 1500.00
Print the Multiplication Table
#include<stdio.h>
void main()
{
int x=1,num,res;
printf("Enter a Number : ");
scanf("%d",&num);
while(x<=10)
{
res=num*x;
printf("\n%d x %d = %d",num,x,res);
x++;
}
}
Output
Enter a Number : 6
6 x 1 = 6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
6 x 10 = 60
Find the Greatest of Three Numbers
#include<stdio.h>
void main()
{
int x,y,z;
printf("Enter values of x, y and z : ");
scanf("%d,%d,%d",&x,&y,&z);
if(x>=y && x>=z)
printf("\n%d is greatest",x);
else if(y>=z)
printf("\n%d is greatest",y);
else
printf("\n%d is greatest",z);
}
Output
Enter values of x, y and z : 4, 9, 2
9 is greatest
Find Greatest number using Conditional Operator
Sumit Kar May 21, 2015 Basic, Conditional Operator, Tricky No comments
Print "Hello, World!" without semicolon
Solution: 1
#include<stdio.h>Solution: 2
void main(){
if(printf("Hello world")){
}
}
#include<stdio.h>Solution: 3
void main(){
while(!printf("Hello world")){
}
}
#include<stdio.h>
void main(){
switch(printf("Hello world")){
}
}
Output
Hello, World!