0% found this document useful (0 votes)
15 views49 pages

C Programming Basics: 25 Examples

The document contains a series of C programming examples demonstrating various basic programming concepts, including printing messages, performing arithmetic operations, using conditional statements, and implementing loops. Each example is structured with standard C syntax and includes comments on functionality. The programs cover a wide range of topics such as addition, swapping variables, checking even/odd numbers, calculating areas, and more.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views49 pages

C Programming Basics: 25 Examples

The document contains a series of C programming examples demonstrating various basic programming concepts, including printing messages, performing arithmetic operations, using conditional statements, and implementing loops. Each example is structured with standard C syntax and includes comments on functionality. The programs cover a wide range of topics such as addition, swapping variables, checking even/odd numbers, calculating areas, and more.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1. Program to print ‘hello’.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr( );
printf(“hello”);
getch();
}
2. Program to addition of two integer numbers.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a=2;
int b=4;
int add;
add=a+b;
printf(“the result is=%d”,add);
getch();
}
3. Program to division of two integer numbers.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a=6;
int b=3;
int div;
printf(“the result is=
%d”,div); getch();
}
4. Program to find the area of triangle.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int a;
int b;
int c;
int s;
int
area;
printf(“enter the value of a\n”);
scanf(“%d”,&a);
printf(“enter the value of b\n”);
scanf(“%d”,&b);
printf(“enter the value of c\n”);
scanf(“%d”,&c);
s=a+b+c/2;
area=sqrt[s*(s-a)*(s-b)*(s-c)];
printf(“the area is=%d”,area);
getch();
}
5. Program of swapping by 2 variables.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a;
int b;
printf(“enter the value of a\n”);
scanf(“%d”,&a);
printf(“enter the value of b\
n”); scanf(“%d”,&b);
printf(“before swapping\n”);
printf(“the value of a is %d\n”,a);
printf(“the value of b is %d\
n”,b); a=a+b;
b=a-b;
a=a-b;
printf(“after swapping”);
printf(“the value of a is %d\n”,a);
printf(“the value of b is %d\n”,b);
getch();
}
6. Program of swapping by 3 variables.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a;
int b;
int temp;
printf(“enter the value of a\n”);
scanf(“%d”,&a);
printf(“enter the value of b\
n”); scanf(“%d”,&b);
printf(“before swapping\n”);
printf(“the value of a is %d\n”,a);
printf(“the value of b is %d\n”,b);
a=b;
b=temp;
temp=a;
printf(“after swapping\n”);
printf(“the value of a is %d\n”,a);
printf(“the value of b is %d\n”,b);
getch();
}
7. Program to convert temprature fahrenheit
to celcius.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
float far;
float cel;
printf(“enter the temprature in fahrenheit\n”);
scanf(“%f”,&far);
cel=5*far-32/9;
printf(“celcius=%f”,cel);
getch();
}
8. Program to ckeck the number is even by
if statement.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a;
printf(“enter the number\
n”); scanf(“%d”,&a); if(a
%2==0)
{
printf(“number is even\n”);
}
getch();
}
9. Program to check the number is even or odd by
if- else statement.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a;
printf(“enter the value of a\n”);
scanf(“%d”,&a);
if(a%2==0)
{
printf(“a is even”);
}
else
{
printf(“a is odd”);
}
getch();
}
10. Program to find the largest number between
two numbers by if-else statement.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a;
int b;
printf(“enter the value of a\n”);
scanf(“%d”,&a);
printf(“enter the value of b\n”);
scanf(“%d”,&b);
if(a>b)
{
printf(“a is larger than b”);
}
else
{
printf(“b is greater than a”);
}
getch();
}
11. Program to find the largest between three
numbers by nested if statement.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a;
int b;
int c;
printf(“enter the value of a\n”);
scanf(“%d”,&a);
printf(“enter the value of b\n”);
scanf(“%d”,&b);
printf(“enter the value of c\n”);
scanf(“%d”,&c);
if(a>b)
{
if(a>c)
{
printf(“a is largest”);
}
else
{
printf(“c is largest”);
}
}
else if(b>c)
{
printf(“b is largest”);
}
else
{
printf(“c is largest”);
}
getch();
}
12. Program to make a calculator by switch statement.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
float a;
float b;
float c;
float s;
float r;
float pie=3.14;
float result;
int n;
int choice;
printf(“press 1 for addition\n”);
printf(“press 2 for subtraction\n”);
printf(“press 3 for multiplication\n”);
printf(“press 4 for division\n”);
printf(“press 5 for area of square\n”);
printf(“press 6 for area of rectangle\n”);
printf(“press 7 for area of triangle\n”);
printf(“press 8 for area of circle\n”);
printf(“press 9 for value check odd or even\n”);
printf(“press 10 for value check positive or negative\n”);
printf(“enter your choice”);
scanf(“%d”,&choice);
switch(choice)
{
case 1:
printf(“enter the value of a\n”);
scanf(“%f”,&a);
printf(“enter the value of b\n”);
scanf(“%f”,&b);
result=a+b;
printf(“the result is=%f”,result);
break;
case 2:
printf(“enter the value of a\n”);
scanf(“%f”,&a);
printf(“enter the value of b\n”);
scanf(“%f”,&b);
result=a-b;
printf(“the result is =%f”,result);
break;
case 3:
printf(“enter the value of a\n”);
scanf(“%f”,&a);
printf(“enter the value of b\n”);
scanf(“%f”,&b);
result=a*b;
printf(“the result is =%f”,result);
break;
case 4:
printf(“enter the value of a\n”);
scanf(“%f”,&a);
printf(“enter the value of b\n”);
scanf(“%f”,&b);
result=a/b;
printf(“the result is =%f”,result);
break;
case 5:
printf(“enter the value of side\n”);
scanf(“%f”,&a);
result=side*side;
printf(“the area is=%f”,result);
break;
case 6:
printf(“enter the value of length\n”);
scanf(“%f”,&a);
printf(“enter the value of breath\n”);
scanf(“%f”,&b);
result=a*b;
printf(“the area is =%f”,result);
break;
case 7:
printf(“enter the value of a\n”);
scanf(“%f”,&a);
printf(“enter the value of b\n”);
scanf(“%f”,&b);
printf(“enter the value of c\n”);
scanf(“%f”,&c);
s=a+b+c/2;
result=sqrt[s*(s-a)*(s-b)*(s-c)];
printf(“the area is=%f”,result);
break;
case 8:
printf(“enter the radius\n”);
scanf(“%f”,&r”);
result=pie*r*r;
printf(“the area is=%f”,result)
break;
case 9:
printf(“enter the value of a\n”);
scanf(“%d”,&n);
if(a%2==0)
{
printf(“a is even”);
}
else
{
printf(“a is odd”);
}
break;
case 10:
printf(“enter the value of a\n”);
scanf(“%d”,&n);
if(a>0)
{
printf(“a is positive”);
}
else
{
printf(“a is negative”);
}
break;
default:
printf(“your choice is invalid\n”);
}
getch();
}
13. Program to print ‘hello’ n times by while loop.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i;
int n;
printf(“enter the value of n\n”);
scanf(“%d”,&n);
i=1;
while(i<=n)
{
printf(“hello”);
i++;
}
getch();
}
14. Program to calculate the sum of first n
integer numbers by while loop.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i;
int n;
sum=0;
printf(“enter the value of n\n”);
scanf(“%d”,&n);
i=1;
while(i<=n)
{
sum=sum+i; i+
+;
}
printf(“sum=%d”,sum);
getch();
}
15. Program to print a table by while loop.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i;
int n;
int m;
printf(“enter the number\n”);
scanf(“%d”,&n);
i=1;
while(i<=10)
{
m=n*i; printf(“%d*%d=
%d”,n,i,m);
i++;
}
getch();
}
16. Program of checking the prime number by
while loop.

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
int n;
printf(“enter the number\n”);
scanf(“%d”,&n);
i=2;
while(i<=n-1)
{
if(n%1==0)
{
printf(“number is not prime\n”);
break;
}
i++;
}
if(n==i)
{
printf(“number is prime\n”);
}
getch();
}
17. Program to reverse the number by while loop.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n;
int digit;
int rev=0;
printf(“enter the number\n”);
scanf(“%d”,&n);
while(n>0)
{
digit=n%10;
rev=rev*10+digit;
n=n/10;
}
printf(“%d”,rev);
getch();
}
18. Program to checking the pelendrom number
by while loop.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n;
int digit;
int rev=0;
int
original;
printf(“enter the number\n”);
scanf(“%d”,&n);
original=n;
while(n>o)
{
digit=n%10;
rev=rev*10+digit;
n=n/10;
}
printf(“original number=%d\
n”,original); printf(“reverse of number=
%d”,rev); if(rev==original)
{
printf(“number is palindrom\n”);
}
else
{
printf(“number is not palindrom\n”);
}
getch(); }
19. Program to print a table by do-while loop.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i;
int n;
int m;
printf(“enter the number\n”);
scanf(“%d”,&n);
i=1;
do
{
m=n*i; printf(“%d*%d=
%d”,n,i,m); i++;
}
while(i<=10);
getch();
}
20. Program of checking the prime number by for loop.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i;
int n;
printf(“enter the number\n”);
scanf(“%d”,&n);
for(i=2; i<=n-1; i++)
{
if(n%1==0)
{
printf(“number is not prime\n”);
break;
}
}
if(n==i)
{
printf(“number is prime\n”);
}
getch();
}
21. Program to print fabonacci series by for loop.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n;
int i;
int fib 1=0;
int fib 2=1;
int fib 3;
printf(“how many numbers in series\n”);
scanf(“%d”,&n);
printf(“fabonacci series\
n”); printf(“%d”,fib 1);
printf(“%d”,fib 2);
for(i=3; i<=n; i++)
{
fib 3=fib 1+fib 2;
printf(“%d”,fib 3);
fib 1=fib 2;
fib 2=fib 3;
}
getch();
}
22. Program to find the factorial by for loop.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
long int i;
long int fact=1;
long int n;
printf(“enter the number\n”);
scanf(“%ld”,&n);
if(n==0)
{
printf(“factorial=%ld”,fact);
}
else
{
for(i=1; i<=n; i++)
{
fact=fact*1;
}
printf(“factorial=%ld”,fact);
}
getch();
}
23. Program to print right angled pascal triangle
by nested-for loop.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i;
int j;
int n;
printf(“enter the maximum number\n”);
scanf(“%d”,&n);
for(i=1; i<=n; i++)
{
for(j=1; j<=i; j++)
{
printf(“* ”);
}
printf(“\n”);
}
getch();
}
24. Program to print an invert right angled
pascal triangle by nested-for loop.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n;
int i;
int j;
printf(“enter the maximum number\n”);
scanf(“%d”,&n);
for(i=n; i>=1; i--)
{
for(j=i; j>=1; j--)
{
printf(“ *”);
}
printf(“\n”);
}
getch();
}
25. Program to print left angled pascal triangle
by nested-for loop.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n;
int i;
int j;
int k;
printf(“enter the maximum number\n”);
scanf(“%d”,&n);
for(i=1; i<=n; i++)
{
for(j=i; j<=n; j++)
{
printf(“ ”);
}
for(k=1; k<=i; k++)
{
printf(“ *”);
}
printf(“\n”);
}
getch();
}
26. Program to print an inverted left angled
pascal triangle by nested-for loop.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n;
int i;
int j;
int k;
printf(“enter the maximum value\n”);
scanf(“%d”,&n);
for(i=n; i>=1; i--)
{
for(j=n; j>=i; j--)
{
printf(“ ”);
}
for(k=1; k<=i; k++)
{
printf(“ *”);
}
printf(“\n”);
}
getch();
}
27. Program to print side pyramid of number
by nested-for loop.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n;
int i;
int j;
int k;
printf(“enter the maximum number\n”);
scanf(“%d”,&n);
for(i=1; i<=n; i++)
{
for(j=i; j<=n; j++)
{
printf(“ ”);
}
for(k=1; k<=i; k++)
{
printf(“%d”,k);
}
printf(“\n”);
}
for(i=n; i>=1; i--)
{
for(j=n; j>=i; j--)
{
printf(“ ”);
}
for(k=1; k<=i; k++)
{
printf(“%d”,k);
}
printf(“\n”);
}
getch();
}
28. Program to print side pyramid of lower-
case alphabet by nested-for loop.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n;
int i;
int j;
int k;
printf(“enter the maximum number\n”);
scanf(“%d”,&n);
for(i=1; i<=n; i++)
{
for(j=i; j<=n; j++)
{
printf(“ ”);
}
for(k=1; k<=i; k++)
{
printf(“%c”,’a’+k);
}
printf(“\n”);
}
for(i=n; i>=1; i--)
{
for(j=n; j>=i; j--)
{
printf(“ ”);
}
for(k=1; k<=i; k++)
{
printf(“%c”,’a’+k);
}
printf(“\n”);
}
getch();
}
29. Program of using break and continue statement.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i;
for(i=1; i<=10; i++)
{
if(i==3)
{
continue;
}
if(i==6)
{
break;
}
printf(“%d”,i);
}
getch();
}
30. Program of using break statement.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i;
for(i=1; i<=10; i++)
{
if(i==4)
{
break;
}
printf(“%d”,i);
}
getch();
}
31. Program of using continue statement.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i;
for(i=1; i<=5; i++)
{
if(i==3)
{
continue;
}
printf(“%d”,i);
}
getch();
}
32. Program to find the sum of two numbers by NON-
ANSI Style.

#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int a;
int b;
int
sum;
printf(“enter the value of a\n”);
scanf(“%d”,&a);
printf(“enter the value of b\
n”); scanf(“%d”,&b);
sum=add_nums(a,b);
printf(“the sum is =%d”,sum);
getch();
}

int add_nums(x,y)
int x;
int y;
{
int result;
result=x+y;
return(result);
}
33. Program to find the product of two numbers by
NON-ANSI Style.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a;
int b;
int multi;
printf(“enter the value of a\n”);
scanf(“%d”,&a);
printf(“enter the value of b\n”);
scanf(“%d”,&b);
multi=multiplication(x,y);
printf(“the product is =%d”,multi);
getch();
}

int multiplication(x,y)
int x;
int y;
{
int result;
result=x*y;
return(result);
}
34. Program to find the sum of two numbers by ANSI
Style.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a;
int b;
int
sum;
printf(“enter the value of a\n”);
scanf(“%d”,&a);
printf(“enter the value of b\
n”); scanf(“%d”,&b);
sum=add_nums(a,b);
printf(“the sum is=%d”,sum);
getch();
}

int add_nums(int x, int y)


{
int result;
result=x+y;
return(result);
}
35. Program of calling functions into called function.

#include<stdio.h>
#include<conio.h>
void add()
{
int a;
int b;
int
sum;
printf(“enter the value of a\n”);
scanf(“%d”,&a);
printf(“enter the value of b\n”);
scanf(“%d”,&b);
sum=a+b;
printf(“the sum is =%d”,sum);
}

void sub()
{
int a;
int b;
int sub;
printf(“enter the value of a\n”);
scanf(“%d”,&a);
printf(“enter the value of b\n”);
scanf(“%d”,&b);
sub=a-b;
printf(“the subtraction is=%d”,sub);
}
void multi()
{
int a;
int b;
int
mul;
printf(“enter the value of a\n”);
scanf(“%d”,&a);
printf(“enter the value of b\n”);
scanf(“%d”,&b);
mul=a*b;
printf(“the product is =%d”,mul);
}

void main()
{
clrscr();
add();
add();
add();
sub();
sub();
sub();
multi();
multi();
multi();
getch();
}
36. Program of addition with parameter passing
method with ‘call by value’.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a;
int b;
int
sum;
printf(“enter the value of a\n”);
scanf(“%d”,&a);
printf(“enter the value of b\
n”); scanf(“%d”,&b);
sum=add(a,b);
printf(“the sum is =%d”,sum);
getch();
}

int add(int x, int y)


{
int c;
c=x+y;
return(c);
}
37. Program of addition with parameter passing
method with ‘call by reference’.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a;
int b;
int
sum;
printf(“enter the value of a\n”);
scanf(“%d”,&a);
printf(“enter the value of b\n”);
scanf(“%d”,&b);
sum=add(&a, &b);
printf(“the sum is =%d”,sum);
getch();
}

int add (int *x, int *y)


{
int *c;
*c=*x+*y;
return(*c);
}
38. Program of addition by register storage class.

#include<stdio.h>
#include<conio.h>
viod main()
{
clrscr();
register a;
register b;
register sum;
printf(“enter the value of a\n”);
scanf(“%d”,&a);
printf(“enter the value of b\n”);
scanf(“%d”,&b);
sum=a+b;
printf(“the sum is =%d”,sum);
getch();
}
39. Program of addition by static storage class.

#include<stdio.h>
#include<conio.h>
viod main()
{
clrscr();
static a;
static b;
static sum;
printf(“enter the value of a\n”);
scanf(“%d”,&a);
printf(“enter the value of b\n”);
scanf(“%d”,&b);
sum=a+b;
printf(“the sum is =%d”,sum);
getch();
}
40. Program of addition by external storage class.

#include<stdio.h>
#include<conio.h>
viod main()
{
clrscr();
extern a;
extern b;
extern sum;
printf(“enter the value of a\n”);
scanf(“%d”,&a);
printf(“enter the value of b\n”);
scanf(“%d”,&b);
sum=a+b;
printf(“the sum is =%d”,sum);
getch();
}
41. Program to find the factorial by recursion function.

#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
{
clrscr();
int n;
int ans;
printf(“enter the number\
n”); scanf(“%d”,&n);
ans=fact(n);
printf(“the factorial is =%d”,ans);
getch();
}

int fact(int x)
{
int f;
if(x==0)
{
return(1);
}
else
{
f=x*fact(x-1);
return(f);
}
}

You might also like