0% found this document useful (0 votes)
102 views32 pages

C Programs

The document contains 21 code snippets demonstrating various programming concepts in C/C++ like checking if two numbers are equal, finding the greatest of three numbers, generating prime numbers within a range, checking if a number is Armstrong or finding factorial of a number. Each code snippet is independently testing and applying a different programming concept through functions, loops, conditional statements, operators etc.

Uploaded by

Bhavani Bhavan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views32 pages

C Programs

The document contains 21 code snippets demonstrating various programming concepts in C/C++ like checking if two numbers are equal, finding the greatest of three numbers, generating prime numbers within a range, checking if a number is Armstrong or finding factorial of a number. Each code snippet is independently testing and applying a different programming concept through functions, loops, conditional statements, operators etc.

Uploaded by

Bhavani Bhavan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

1. Check two number are equal or not.

#include<stdio.h>
int main()
{
int n1,n2;
printf("Enter two numbers\n");
scanf("%d%d",&n1,&n2);
if(n1==n2)
printf("The numbers are the same");
if(n1!=n2)
printf("The numbers are not same");
return 0;
}

2. Check number is odd or even.


#include<stdio.h>
int main()
{
int n1;
printf("Enter anumber\n");
scanf("%d",&n1);
if ((n1%2)==0)
printf("Even");
else
printf("Odd");
return 0;
}

3.Quadrant
#include<stdio.h>
#include<conio.h>
void main()
{
int x1,x2;
clrscr();
printf("Enter the two roots\n");
scanf("%d%d",&x1,&x2);
if(x1>0 && x2>0)
{
printf("Quadrant I");
}
else if(x1<0 && x2>0)
{
printf("Quadrant II");
}
else if(x1<0 && x2<0)
{
printf("Quadrant III");
}
else if(x1==0 && x2==0)
{
printf("Origin");
}
else if(x1>0 && x2<0)
{
printf("Quadrant IV");
}
getch();
}

4.Maximum of three given values.

#include<stdio.h>
#include<stdio.h>
int main()
{
int n1,n2,n3;
printf("Enter three numbers\n");
scanf("%d%d%d",&n1,&n2,&n3);
if ((n1>n2)&&(n1>n3))
printf("The greatest number is: %d",n1);
else if (n2>n3)
printf("The greatest number is: %d",n2);
else
printf("The greatest number is: %d",n3);
return 0;
}
5. Find the grade of subject received by student.
#include<stdio.h>
int main()
{
int mark;
printf("\nEnter the mark");
scanf("%d",&mk);
if(mk >=90)
printf("\nThe grade is A+");
else if((mk >=80)&&( mk <90))
printf("\nThe grade is A");
else if((mk >=70)&&( mk <80))
printf("\nThe grade is B+");
else if((mk >=60)&&( mk <70))
printf("\nThe grade is B");
else if((mk >=50)&&( mk <60))
printf("\nThe grade is C+");
else if((mk >=40)&&( mk <50))
printf("\nThe grade is c");
else
printf("\nFailed");
return 0;
}
6.Find the area of various geometric figures.

#include<stdio.h>
#define pi 3.14
int main()
{
int ch,length,breadth,cubel;
float radius;
printf(" 1.Rectangle \n 2.Circle \n 3.Cube\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter length and breadth \n ");
scanf("%d %d",&length,&breadth);
printf("\nArea=%d",length*breadth);
break;
case 2:

printf("Enter Radius of circle\n");


scanf("%f",&radius);

printf("\nArea=%5.2f",pi*radius*radius);
break;
case 3:
printf("Enter Length\n");
scanf("%d",&cubel);
printf("Surface Area=%d",6*cubel*cubel);
break;
default:
printf("Enter A Valid Number");
break;

}
return 0;

7.Find number of vowels in a given text.

#include<stdio.h>
int main()
{
char str[100];
int v=0,i;
printf("Enter A Line Of Text \n");
gets(str);
for(i=0;str[i]!='\0';i++)
{
if((str[i]=='a' || str[i]=='e'|| str[i]=='i'|| str[i]=='o'|| str[i]=='u'|| str[i]=='A'|| str[i]=='E'||
str[i]=='I'|| str[i]=='O'|| str[i]=='U'))

v+=1;

}
}
printf("\nNumber Of Vowels=%d",v);
return 0;
}

8.Check given letter is uppercase or lowercase & if lowercase, convert into uppercase.

#include<stdio.h>
#include<conio.h>

void main()
{
char ch, res;
clrscr();
printf("Enter the character\n");
scanf("%c",&ch);

(ch>=65 && ch<=90) ? (printf("%c is uppercase.\n",ch), res = ch + 32, printf("%c changes to %c.", ch,
res)) : (printf("%c is lowercase.", ch));
getch();
}

9. Convert lowercase to uppercase using ternary operator.

#include<stdio.h>
void main()
{
char ch, rs;
printf("Enter the character\n");
scanf("%c",&ch);

(ch>=65 && ch<=90) ? (printf("%c is uppercase.\n",ch), rs = ch + 32, printf("%c changes to %c.",


ch, rs)) : (printf("%c is lowercase.", ch));
}

9 Find biggest of three number using ternary operator.


#include<stdio.h>
#include<conio.h>
void main()
{
char ch, rs;
printf("Enter the character\n");
scanf("%c",&ch);

(ch>=97 && ch<=122) ? (printf("%c is lowercase.\n",ch), rs = ch - 32, printf("%c changes to %c.",


ch, rs)) : (printf("%c is uppercase.", ch));
}

11.Find biggest of three number using ternary operator.

#include<stdio.h>
void main()
{
int a,b,c;
printf("Enter the three numbers\n");
scanf("%d%d%d",&a,&b,&c);

(a>=b && a>=c) ? printf("Greatest number -> %d",a) : (b>=a && b>=c) ? printf("Greatest number
-> %d",b) : printf("Greatest number -> %d",c);
}
12. Generate odd and even number upto 50.

#include<stdio.h>

#include<conio.h>

void main()
{
int i;
clrscr();
for(i=0;i<=50;i++)
{
if(i%2==0)
{
printf("%d - Even\t", i);
}
else
{
printf("%d - Odd\t", i);
}
}
getch();
}
13. Sum of values from 100 to 200.

#include<stdio.h>
#include<conio.h>

void main()
{
int sum=0, i;
clrscr();

for(i=100;i<=200;i++)
{
sum=sum+i;
}

printf("Sum - %d", sum);


getch();
}

14. Reverse the given number.

#include<stdio.h>
#include<conio.h>

void main()
{
int num, temp, rev=0, rem;
clrscr();
printf("Enter a number: \t");
scanf("%d",&num);

temp = num;

while(num!=0)
{
rem = num % 10;
rev = rev * 10 + rem;
num = num/10;
}

printf("Reversed number of %d is: %d", temp, rev);


getch();
}

15. Check the given number is prime or not.

#include<conio.h>
#include<stdio.h>
void main()
{
float a,b,y,x;
clrscr();
printf("Enter values of a and b : \nd");
scanf("%f%f",&a,&b);
for(x=-5;x<=5;x+=0.5)
{
y=a*x+b;
printf("x = %6.2f , y = %6.2f\n",x,y);
}
getch();
}
#include<conio.h>
#include<stdio.h>
void main()
{
float a,b,y,x;
clrscr();
printf("Enter values of a and b : \nd");
scanf("%f%f",&a,&b);
for(x=-5;x<=5;x+=0.5)
{
y=a*x+b;
printf("x = %6.2f , y = %6.2f\n",x,y);
}
getch();
}
#include<conio.h>
#include<stdio.h>
void main()
{
float a,b,y,x;
clrscr();
printf("Enter values of a and b : \nd");
scanf("%f%f",&a,&b);
for(x=-5;x<=5;x+=0.5)
{
y=a*x+b;
printf("x = %6.2f , y = %6.2f\n",x,y);
}
getch();
}

16. Generate all prime number within a given range.

#include<stdio.h>
#include<conio.h>

void main()
{
int i, j, start, end, flag;
clrscr();

printf("Enter the starting range: ");


scanf("%d",&start);
printf("Enter the ending range: ");
scanf("%d",&end);

for(i=start;i<=end;i++)
{
flag=0;
for(j=2;j<=i/2;j++)
{
if(i%j==0)
{
flag=1;
break;
}
}
if(flag==0)
{
printf("%d - Prime\t", i);
}
else
{
printf("%d - Not Prime\t", i);
}
}
getch();
}
17. Generate all prime number within a given range.
#include<stdio.h>
void main()
{
int i, j, start, end, flag;
printf("Enter range: ");
scanf("%d%d",&start,&end);
for(i=start;i<=end;i++)
{
flag=0;
for(j=2;j<=i/2;j++)
{
if(i%j==0)
{
flag=1;
break;
}
}

if(flag==0)
{
printf("%d - Prime", i);
}
else
{
printf("%d - Not Prime", i);
}
}
}
18. Check given number is Armstrong or not.

#include<stdio.h>
#include<conio.h>

Int main()
{
int i, num, res=0, temp, rem;
clrscr();

printf("Enter the number:\t");


scanf("%d",&num);

temp=num;

while(num!=0)
{
rem = num % 10;
res = res + rem * rem * rem;
num = num/10;
}

if(temp==res)
{
printf("%d is an Armstrong number", temp);
}
else
{
printf("%d is not an Armstrong number",temp);
}

getch();
}

19. Generate fibbonacci series.

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int main(){

int i=0, num1=0, num2=1, res, limit;


clrscr();

printf("Enter the limit:- \t");


scanf("%d",&limit);
printf("%d\t%d\t",num1,num2);

while(i<limit)
{
res = num1+num2;
printf("%d\t",res);
num1=num2;
num2=res;
i++;
}
getch();
}

20. Check given year is leap year or not.

#include<stdio.h>
#include<conio.h>

int main()
{
int year;
clrscr();
printf("Enter a Year\t");
scanf("%d",&year);

if(year%4==0)
{
if(year%100==0)
{
if(year%400==0)
printf("%d is a leap year.", year);

else
printf("%d is not a leap year.",year);
}
else
printf("%d is a leap year.");
}
else
printf("%d is not leap year.",year);

getch();
}

21. Define value of nCr & nPr.


#include<stdio.h>
#include<conio.h>

void main()
{
int n, r, i, factn=1, factr=1, factnr=1, ncr, npr;
clrscr();

printf("Enter the value of n and r:\n");


scanf("%d%d",&n,&r);

for(i=1;i<=n;i++)
{
factn = factn * i;
}

for(i=1;i<=r;i++)
{
factr = factr * i;
}

for(i=1;i<=n-r;i++)
{
factnr = factnr * i;
}

ncr = factn/(factr*factnr);
npr = factn/factnr;

printf("%dC%d = %d\n",n, r, ncr);


printf("%dP%d = %d\n",n, r, npr);

getch();
}
22. Find mean and standard deviation of given value.

#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
float mean, res = 0, sd, sum = 0;
int i, n, arr[20];
clrscr();
printf("Enter the number of data you want to enter: \n");
scanf("%d",&n);

printf("Enter the %d data(s): \n",n);


for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}

for(i=0;i<n;i++)
{
sum = sum + arr[i];
}

mean = sum / n;

for(i=0;i<n;i++)
res = res + pow(arr[i] - mean, 2);
sd = sqrt(res/n);

printf("Mean = %.3f\n",mean);
printf("Standard Deviation = %.3f",sd);
getch();
}

23. Check whether given program is perfect square or not.

#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
float fnum;
int inum, num;
clrscr();

printf("Enter the number: ");


scanf("%d",&num);

fnum = sqrt(num);
inum = fnum;

if(inum == fnum)
printf("%d is a perfect square.",num);
else
printf("%d is not perfect square.",num);

getch();
}

24. Check given number is divisible by 7.


#include<stdio.h>
#include<math.h>
void main()
{

float f;
int num, num1;
printf("Enter the number: ");
scanf("%d",&num);

f = sqrt(num);

num1 = f;

if(num1 == f)
printf("%d is a perfect square.",num);
else
printf("%d is not perfect square.",num);

25. Find sum of series of 1 + 2! + 3! + 4! —-- n! .

#include<stdio.h>
#include<conio.h>

void main()
{
int i, j, limit, sum = 0, p;
clrscr();
printf("Enter the limit till when you want to sum\n");
scanf("%d",&limit);

for(i=1; i<=limit; i++)


{
p=1;
for(j=1;j<=i;j++)
{
p = p * j;
}
sum = sum + p;
}
printf("Sum of %d factorial is %d", limit, sum);
getch();
}

26. HCF & LCM

#include<stdio.h>
#include<conio.h>

int main()
{
int num1, num2, a, b, temp, hcf, lcm;
clrscr();
printf("Enter two number\n");
scanf("%d%d", &a, &b);

num1 = a;
num2 = b;

while(num2!=0)
{
temp = num2;
num2 = num1 % num2;
num1 = temp;
}

hcf = num1;
lcm = (a*b) / hcf;
printf("HCF of %d and %d = %d\n", a, b, hcf);
printf("LCM of %d and %d = %d\n", a, b, lcm);
getch();
}

27. Check palindrome or not.

#include<stdio.h>
#include<conio.h>

void main()
{
int num, i = 0 , rem, rev = 0, temp;
clrscr();
printf("Enter the number\n");
scanf("%d", &num);

temp = num;;

while(num != 0)
{
rem = num % 10;
rev = rev * 10 + rem;
num = num / 10;
i++;
}

if(temp == rev)
printf("%d is Palindrome.", temp);
else
printf("%d is not Palindrome", temp);

getch();
}

28. Find middle number of 3 digit is equal to either sum or product of either two number.

#include<stdio.h>
#include<conio.h>

void main()
{
int num, i, rem, sum = 0, pro = 1, temp;
clrscr();
printf("Enter three digit number\t");
scanf("%d",&num);

for(i=0;num!=0;i++)
{
rem = num % 10;
if(i==1)
{
temp = rem;
}
else
{
sum = sum + rem;
pro = pro * rem;
}
num = num / 10;
}

printf("Middle number = %d\nSum = %d\nProduct = %d\n",temp, sum, pro);

if(sum == temp || pro == temp)


printf("The middle number is equal to sum or the product of two numbers.");
else
printf("The middle number is not equal to sum or the product of two number.");

getch();
}
29. Generate multiplication table of n number.

#include<stdio.h>
#include<conio.h>

void main()
{
int num, i, pro;
clrscr();
printf("Enter number\t");
scanf("%d",&num);

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

30. Generate multiplication table of n number.

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

int num, i, pro;


printf("Enter number\t");
scanf("%d",&num);
for(i=1;i<=10;i++)
{
pro = num * i;
printf("%d * %d = %d\n", num, i, pro);
}

}
31. Factorial of any number.

#include<stdio.h>
#include<conio.h>

int main()
{
int num, i, fact = 1;
clrscr();
printf("Enter number\t");
scanf("%d",&num);

for(i=1;i<=num;i++)
{
fact = fact * i;
}

printf("Factorial of %d = %d", num, fact);


getch();
}
32. Find prime numbers fibbonacci series of given range.

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int main(){

int i, j, flag, num1=0, num2=1, res, limit;


clrscr();

printf("Enter the limit:- \t");


scanf("%d",&limit);

printf("\nThe prime number in the Fibbonacci series are:-\n");

for(i=0;i<limit;i++)
{
res = num1+num2;
num1=num2;
num2=res;
flag=0;

for(j=2;j<=res/2;j++)
{
if(res%j==0)
{
flag=1;
break;
}
}

if(flag==0)
{
printf("%d\t",res);
}
}
getch();
}

#include<conio.h>
#include<stdio.h>
void main()
{
float a,b,y,x;
clrscr();
printf("Enter values of a and b : \nd");
scanf("%f%f",&a,&b);
for(x=-5;x<=5;x+=0.5)
{
y=a*x+b;
printf("x = %6.2f , y = %6.2f\n",x,y);
}
getch();
}

You might also like