C Language Introduction
C Programming Language is a professional high-level programming language. C Language was initially developed by Dennis Ritchie in the year 1972 at the Bell Telephone Laboratories to begin the UNIX operating system.
In this post 10, Basic C Programs are being discussed below which every beginner should know before learning difficult C Programming. C Programs are discussed in easy steps following with pictures and Output results.
In this post 10, Basic C Programs are being discussed below which every beginner should know before learning difficult C Programming. C Programs are discussed in easy steps following with pictures and Output results.
1. Simple C Program Structure
The basic structure of simple c programming is described below including important syntax and useful data types like - int (integer).
#include <stdio.h>#include <stdlib.h>int main (){printf ("Hello World ! \n");return 0;}
A Simple C Program Structure. |
Output :
- Hello World
2. Simple C Program for Addition
The syntax and commands used for writing a simple c program for addition are described below with proper output. The arithmetic operator of addition is also used here.
#include <stdio.h>#include <stdlib.h>int main (){int a,b,c;printf ("Enter Number1: ");scanf("%d", &a);printf ("Enter Number2: ");scanf("%d", &b);c=a+b;printf ("The Addition Is : %d",c);return 0/getch();}
Simple C Program For Addition. |
Output :
- Enter Number1: 2 (accepting)
- Enter Number2 : 3 (accepting)
- The Addition Is: 5 (result)
3. Basic C Program for Subtraction
The syntax and C code used to write a basic c program for subtraction are described below with proper output. The arithmetic operator of subtraction is also used here.
#include <stdio.h>#include <stdlib.h>int main (){int a,b,c;printf ("Enter Number1: ");scanf("%d", &a);printf ("Enter Number2: ");scanf("%d", &b);c=a-b;printf ("The Subtraction Is : %d",c);return 0/getch();}
Basic C Program for Subtraction. |
Output :
- Enter Number1: 5 (accepting)
- Enter Number2 : 3 (accepting)
- The Subtraction Is: 2 (result)
4. Basic C Program for Multiplication
The syntax and C code used to write a basic c program for multiplication are described below with proper output. The arithmetic operator of multiplication is also used here.
#include <stdio.h>#include <stdlib.h>int main (){int a,b,c;printf ("Enter Number1: ");scanf("%d", &a);printf ("Enter Number2: ");scanf("%d", &b);c=a*b;printf ("The Multiplication Is : %d",c);return 0/getch();}
Output :
- Enter Number1: 5 (accepting)
- Enter Number2 : 3 (accepting)
- The Multiplication Is: 15 (result)
5. Basic C Program for Percentage (%)
#include <stdio.h>
#include <stdlib.h>
main()
{
int roll_number,m1,m2,m3,OM;
float percentage;
printf("Enter Roll Number: ");
scanf("%d",&roll_number);
printf("Enter Marks Of Three Subjects: ");
scanf("%d%d%d",&m1,&m2,&m3);
OM=m1+m2+m3;
percentage=OM/3;
printf("Roll Number: %d \nTotal Marks : %d \nPercentage: %6.2f",roll_number,OM,percentage);
getch();
}
Basic C Program for Percentage. |
Output :
- Enter Roll Number : 21 (accepting roll number)
- Enter Marks of Three Subjects : 60 70 80 (accepting marks)
- Roll Number : 21 (printing)
- Total Marks : 210 (printing)
- Percentage : 70 (result)
6. Simple C Program to Swap Two Numbers
#include <stdio.h>
#include <stdlib.h>
main()
{
int a,b,t;
printf("Enter Numbers To Swap: ");
scanf("%d%d",&a,&b);
printf("Number Before Swapping Are : a=%d,b=%d",a,b);
t=a;
a=b;
b=t;
printf("Number After Swapping Are : a=%d,b=%d",a,b);
getch();
}
Simple C Program to Swap Two Program. |
Output :
- Enter Numbers To Swap : 2 3 (accepting numbers)
- Number Before Swapping Are : a=2 b=3 (printing numbers)
- Number After Swapping Are : a=3 b=2 (swapped numbers)
7. Basic C Program for Square and Cube Root
#include <stdio.h>
#include <stdlib.h>
main()
{
int n,SQR,CUBE;
printf("Enter A Numner : ");
scanf("%d",&n);
SQR=n*n;
CUBE=n*n*n;
printf("Square Is: %d Cube Is: %d",SQR,CUBE);
getch();
}
Basic C Program for Square and Cube Root. |
Output :
- Enter A Number: 2
- Square Is: 4
- Cube Is: 8
8. C++ Program for Armstrong Number
#include <iostream>using namespace std;
int main()
{
int r,n,s=0,m;
cout << "Enter Number n :"<<endl;
cin>>n;
m=n;
while (n>0)
{
r=n%10;
n=n/10;
s=s + (r*r*r);
}
if (s==m)
cout<<"No. Is ArmStrong"<<endl;
else
cout<<"Not ArmStrong"<<endl;
cout<<"Sum="<<s;
return 0;
}
Output :
- Enter Number n : 153
- No. Is Armstrong
- Sum = 153
9. Basic C Program for Area of Circle
#include <stdio.h>
#include <stdlib.h>
main()
{
float AREA,R;
printf("Enter Radius: ");
scanf("%f",&R);
AREA=3.14*R*R;
printf("Area Of Circle Is: %f",AREA);
getch();
}
Output :
- Enter Radius: 7
- Area Of Circle Is: 153.86
10. C Program Area of Triangle for Beginners
#include <stdio.h>
#include <stdlib.h>
main()
{
float AREA,B,H;
printf("Enter Base & Height: ");
scanf("%f%f",&B,&H);
AREA=(B*H)/2;
printf("Area Of Triangle Is: %6.2f",AREA);
getch();
}
Output :
- Enter Base & Height : 2, 4
- Area Of Triangle Is : 4