0% found this document useful (0 votes)
58 views28 pages

C Record

The document contains 19 code snippets showing C programs that demonstrate basic programming concepts like input/output, arithmetic operations, conditional statements, loops, functions, and more. Each code snippet is accompanied by a brief description of the program and showcases a different basic programming concept in C language.

Uploaded by

Dragu Stelian
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)
58 views28 pages

C Record

The document contains 19 code snippets showing C programs that demonstrate basic programming concepts like input/output, arithmetic operations, conditional statements, loops, functions, and more. Each code snippet is accompanied by a brief description of the program and showcases a different basic programming concept in C language.

Uploaded by

Dragu Stelian
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/ 28

www.btechsmartclass.

com

1. Write a C program to print welcome message.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("Hi! Welcome to C!!!");
getch();
}

1
www.btechsmartclass.com

2. Write a C program to perform addition of two integer numbers.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c; // Variable declaration
clrscr();

printf("Enter any two integer values: ");


scanf("%d%d",&a,&b);
c = a + b;
printf("SUM of given two numbers is %d",c);
getch();
}

2
www.btechsmartclass.com

3. Write a C program to convert distance from cm to mts.

#include<stdio.h>
#include<conio.h>
void main()
{
float cm, mts;
clrscr();

printf("Enter the distance in Centimeters: ");


scanf("%f",&cm);

mts = cm / 100;
printf("Given distance in Meters: %.2f mts",mts);

getch();
}

3
www.btechsmartclass.com

4. Write a C program to illustrate increment and decrement operators.

#include<stdio.h>
#include<conio.h>
void main()
{
int a = 5, b = 10, c, d;
clrscr();

c = a++;
d = ++b;
printf("Post increment result: %d\n",c);
printf("Pre increment result: %d\n",d);

c = a-- + --b;
printf("Finally C = %d",c);

getch();
}

4
www.btechsmartclass.com

5. Write a C program to test whether given number is EVEN or ODD.

#include<stdio.h>
#include<conio.h>
void main()
{
int num;
clrscr();

printf("Enter any integer value: ");


scanf("%d",&num);

if(num % 2 == 0)
printf("\n%d is EVEN number");
else
printf("\n%d is ODD number");

getch();
}

5
www.btechsmartclass.com

6. Write a C program to find largest of three numbers.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();

printf("Enter any three integer values: ");


scanf("%d%d%d",&a,&b,&c);

if(a>b && a>c)


printf("\n%d is Largest!!!",a);
else
if(b>a && b>c)
printf("\n%d is Largest!!!",b);
else
printf("\n%d is Largest!!!",c);

getch();
}

6
www.btechsmartclass.com

7. Write a C program to swap (exchange) given two numbers.

#include<stdio.h>
#include<conio.h>
void main()
{
int num1, num2, temp;
clrscr();

printf("Enter any two integer values: ");


scanf("%d%d",&num1,&num2);

printf("\nBefore SWAP:\nnum1 = %d\tnum2 = %d",num1,num2);

temp = num1;
num1 = num2;
num2 = temp;

printf("\n\nAfter SWAP:\nnum1 = %d\tnum2 = %d",num1,num2);

getch();
}

7
www.btechsmartclass.com

8. Write a C program to print ASCII value of given character.

#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();

printf("Enter any Charcter: ");


scanf("%c",&ch);

printf("\nASCII value of given character %c is %d",ch,ch);

getch();
}

8
www.btechsmartclass.com

9. Write a C program to find roots of a quadratic equation.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c, root1,root2,temp, real, imag;
clrscr();

printf("Enter any a, b and c values: ");


scanf("%f%f%f",&a,&b,&c);

temp = (b*b) - (4*a*c);


if(temp>0)
{
printf("\nRoots are real:\n");
root1 = (-b + sqrt(temp))/(2*a);
root2 = (-b - sqrt(temp))/(2*a);

printf("\nRoot1 = %.2f",root1);
printf("\nRoot2 = %.2f",root2);
}
else{
if(temp == 0)
{
printf("\nRoots are real:\n");
root1 = root2 = -b/(2*a);

printf("\nRoot1 = %.2f",root1);
printf("\nRoot2 = %.2f",root2);
}
else
{
real = -b / (2*a);
imag = sqrt(-temp) / (2*a);
printf("\nRoots are imaginary:\n");
printf("Root1 = %.2f + %.2fi",real,imag);
printf("\nRoot2 = %.2f - %.2fi",real,imag);
}
}

getch();
}

9
www.btechsmartclass.com

10
www.btechsmartclass.com

10. Write a C program to find Area and Circumference of a Circle.

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

#define PI 3.14

void main()
{
float radius,area,circumference;
clrscr();

printf("Enter the radius of circle: ");


scanf("%f",&radius);

area = PI*(radius*radius);
circumference = 2 * PI * radius;

printf("\nArea = %.2f",area);
printf("\nCircumference = %.2f",circumference);

getch();
}

11
www.btechsmartclass.com

11. Write a C program to find Area of a Triangle.

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

void main()
{
float base,height,area;
clrscr();

printf("Enter the base and height of Triangle: ");


scanf("%f%f",&base,&height);

area = (0.5) * base * height;

printf("\nArea of triangle = %.2f",area);

getch();
}

12
www.btechsmartclass.com

12. Write a C program to find Factorial of a given integer number.

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

void main()
{
int num,fact=1,i;
clrscr();

printf("Enter any number: ");


scanf("%d",&num);

for(i = num; i >= 1; i--)


{
fact = fact * i;
}

printf("\nFactorial of %d is %d",num,fact);

getch();
}

13
www.btechsmartclass.com

13. Write a C program to test whether given number is PRIME or NOT.

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

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

printf("Enter any number: ");


scanf("%d",&num);

for(i = 2; i < num; i++)


{
if(num%i == 0)
count++;
}
if(count == 0)
printf("\nGiven number is PRIME!!!");
else
printf("\nGiven number is NOT PRIME!!!");

getch();
}

14
www.btechsmartclass.com

14. Write a C program to print all PRIME numbers up to n.

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

void main()
{
int num,count,i,value;
clrscr();

printf("Enter any number: ");


scanf("%d",&num);

printf("\nAll PRIME numbers from 1 to %d are\n\n",num);


value = 1;
while(value<=num){
count = 0;
for(i = 2; i < value; i++){
if(value%i == 0)
count++;
}
if(count == 0)
printf("%d\t",value);
value++;
}
getch();
}

15
www.btechsmartclass.com

15. Write a C program to print all EVEN numbers from 1 to 50.

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

void main()
{
int count = 1;
clrscr();

printf("\nEven numbers from 1 to 50:\n");


while(count<=50)
{
if(count%2 == 0)
printf("%3d",count);
count++;
}

getch();
}

16
www.btechsmartclass.com

16. Write a C program to print Fibonacci series of first n elements.

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

void main()
{
int a=0,b=1,c,n,count = 3;
clrscr();

printf("Enter the number of elements to be display: ");


scanf("%d",&n);

printf("\nFibonacci series of first %d elements:\n",n);


printf("\n%d\t%d",a,b);
while(count<=n)
{
c = a + b;
printf("\t%d",c);
a = b;
b = c;
count++;
}

getch();
}

17
www.btechsmartclass.com

17. Write a C program to print REVERSE of given integer number.

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

void main()
{
int num,reverse=0,remainder;
clrscr();

printf("\nEnter any integer number: ");


scanf("%d",&num);

while(num>0)
{
remainder = num % 10;
reverse = reverse * 10 + remainder;
num = num / 10;
}
printf("\nReverse of given number is %d",reverse);

getch();
}

18
www.btechsmartclass.com

18. Write a C program to test whether given number is POLINDROME or NOT.

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

void main()
{
int num,reverse=0,remainder,temp;
clrscr();

printf("\nEnter any integer number: ");


scanf("%d",&num);
temp = num;

while(num>0)
{
remainder = num % 10;
reverse = reverse * 10 + remainder;
num = num / 10;
}
if(temp == reverse)
printf("\nGiven number is PALINDROME!!!");
else
printf("\nGiven number is NOT POLINDROME!!!");

getch();
}

19
www.btechsmartclass.com

19. Write a C program to test whether given number is ARMSTRONG number or NOT.

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

void main()
{
int num,sum=0,remainder,temp;
clrscr();

printf("\nEnter any integer number: ");


scanf("%d",&num);
temp = num;

while(num>0){
remainder = num % 10;
sum = sum + pow(remainder,3);
num = num / 10;
}
if(temp == sum)
printf("\nGiven number is ARMSTRONG!!!");
else
printf("\nGiven number is NOT ARMSTRONG!!!");

getch();
}

20
www.btechsmartclass.com

20. Write a C program to perform Addition of two numbers using functions.

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

void main()
{
int num1,num2,result;
int addition(int,int);
clrscr();

printf("\nEnter any two integer numbers: ");


scanf("%d%d",&num1,&num2);

result = addition(num1,num2);

printf("\nSUM of given numbers is %d",result);

getch();
}

int addition(int a, int b)


{
return(a+b);
}

21
www.btechsmartclass.com

21. Write a C program to perform all Arithmetic operations using functions.

#include<stdio.h>
#include<conio.h>
void main()
{
int num1,num2,result;
int addition(int,int);
int subtraction(int,int);
int multiplication(int,int);
int division(int,int);
int modulo(int,int);
clrscr();

printf("\nEnter any two integer numbers: ");


scanf("%d%d",&num1,&num2);

printf("\n%d + %d = %d",num1,num2,addition(num1,num2));
printf("\n%d - %d = %d",num1,num2,subtraction(num1,num2));
printf("\n%d * %d = %d",num1,num2,multiplication(num1,num2));
printf("\n%d / %d = %d (float value is type casted!)",num1,num2,division(num1,num2));
printf("\n%d %% %d = %d",num1,num2,modulo(num1,num2));
getch();
}

int addition(int a, int b){


return(a+b);
}
int subtraction(int a, int b){
return(a-b);
}
int multiplication(int a, int b){
return(a*b);
}
int division(int a, int b){
if(b == 0){
printf("\nDivision is not posible!!!");
return;
}
else
return(a/b);
}
int modulo(int a, int b){
return(a%b);
}

22
www.btechsmartclass.com

23
www.btechsmartclass.com

22. Write a C program to find SUM of individual digits of given integer number.

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

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

printf("\nEnter any integer number: ");


scanf("%d",&num);

while(num>0)
{
remainder = num % 10;
sum = sum + remainder;
num = num / 10;
}
printf("\nSUM of individual digits of given number is %d",sum);

getch();
}

24
www.btechsmartclass.com

23. Write a C program to perform all Arithmetic operations using switch statement.

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

void main()
{
int num1,num2;
char choice;
clrscr();

printf("\nEnter any two integer numbers: ");


scanf("%d%d",&num1,&num2);

printf("\nEnter operation symbol (+,-,*,/,%): ");


choice = getch();

switch(choice)
{
case '+': printf("\n%d + %d = %d",num1,num2,num1+num2); break;
case '-': printf("\n%d - %d = %d",num1,num2,num1-num2); break;
case '*': printf("\n%d * %d = %d",num1,num2,num1*num2); break;
case '/': if(num2==0)
printf("\nDivision not posible!!!");
else
printf("\n%d / %d = %d",num1, num2, num1/num2);
break;
case '%': printf("\n%d %% %d = %d",num1,num2,num1%num2); break;
default: printf("\nWrong input!!!");
}
getch();
}

25
www.btechsmartclass.com

Write a C program to find GCD or HCF of given two integer numbers.

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

void main()
{
int num1,num2,temp=1,gcd;
clrscr();

printf("\nEnter any two integer numbers: ");


scanf("%d%d",&num1,&num2);

while(temp <= num1 || temp <= num2)


{
if(num1%temp == 0 && num2%temp == 0)
gcd = temp;
temp++;
}
printf("\nGCD of %d and %d is %d",num1,num2,gcd);

getch();
}

26
www.btechsmartclass.com

24. Write a C program to calculate the following.


Sum = 1-x2/2!+x4/4!-x6/6!+x8/8!-x10/10!

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

void main()
{
int x,i=0,n=0;
float sum=0;
long fact(int);
clrscr();

printf("\nEnter the value of 'x': ");


scanf("%d",&x);

while(i<=10)
{
sum = sum+(pow(-1,n)*pow(x,i)/fact(i));
i=i+2;
n++;
}
printf("\nSUM = %ld",sum);

getch();
}
long fact(int a)
{
long f=1;
while(a!=0){
f = f*a;
a--;
}
return f;
}

27
www.btechsmartclass.com

25. Write a C program to calculate the following.


s = ut + at2

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

void main()
{
float u,t,s,a;
clrscr();

printf("\nEnter the initial speed: ");


scanf("%f",&u);
printf("\nEnter the time taken: ");
scanf("%f",&t);
printf("\nEnter the acceleration: ");
scanf("%f",&a);

s = (u*t) + (0.5)*a*pow(t,2);
printf("\nResult = %.2f",s);

getch();
}

28

You might also like