C Language for beginners
Arrays in C
Collection of single type of elements in a Single variable is Called Array
Array - [ ] this brackets indicates array
Array Indes [ ] always start from zero
Advantages
1) Memory will be created automatically
2) We can store values in single variable
Disadvantages
1) Array size always static, We can declare initially
2) Ones we declare array size we can not Increase or Decrease
Syntax
datatype<variable>[size];
IF defination
If condition is true then only excutes statments and shows result other wise out does not display result
Syntax
IF(conditon)
{
Statement 1 ;
Statement 2 ;
:
:
:...etc.;
}
If condition is true then only excutes statments and shows result other wise out does not display result So we have Use else
Do While Defination
In Do-While frist it is enters into the loop excutes all the statements and cheks the conditon if the conditon is true again enters into the loop excutes all statements these phrases repeat untill conditon became false
Syntax
Do {
Statement 1 ;
Statement 2 ;
:
:
:...etc.;
}While(conditon);
Do while is compalsary Excutes all statements one time without depends on the condion
Else If In C Language
If condition is true then only excutes statments and shows result other wise
Cheks the Else conditon if conditon is true excutes the statements and display result according to else conditon
Syntax
If (conditon)
{
Statement 1 ;
}
Else (conditon)
{
Statement 2 ;
}
C For Loop definition
For LoopAs for loop first checks installation, again checks if the condition is true it enter into the loop executes all the statements after increment update and check again the condition do the same phrases until condition is false
Syntax
for(intilation;condition;update)
{
Statement 1 ;
Statement 2 ;
:
:
:...etc.;
}
Nested loop or Inner loop
Writting the loop side the loop is called Inner loop
Syntax :
While(condition)
{
Statement 1 ;
:
While(condition)
{
Statement 1 ;
:
}
Explanation
It cheks the while conditons if the both loops are true enter the inner loop where the outer loop runs n times and consists of another loop inside it. The inner loop runs m times. Then, the total number of times the inner loop runs during the program execution is n*m.
While defination in c
In while loop it cheks the conditon frist if the conditon is ture it enters the loop excutes the all the statements and after that it cheks condition again,exutes statements utill conditon false. If the condition is false it come out from the loop.Syntax
While(conditon)
{
Statement 1 ;
Statement 2 ;
Statement 3 ;
:
}
In while loop we can also use if,else,else-if,...etc
Best 10 Simple C Programs for Beginners
C programme for print numbers ascetics
#include<stdio.h>
#include<conio.h>
void main(){
int a[5],i;
clrscr();
printf("Enter any 5 values:");
for(i=0;i<5;i++){
scanf("%d",&a[i]);
}
for(i=0;i<5;i++){
printf("%d\n",a[i]);
}
getch();
}
#include<conio.h>
void main(){
int a[5],i;
clrscr();
printf("Enter any 5 values:");
for(i=0;i<5;i++){
scanf("%d",&a[i]);
}
for(i=0;i<5;i++){
printf("%d\n",a[i]);
}
getch();
}
C programme for sum of diagonal matrix
#include<stdio.h>
#include<conio.h>
void main(){
int a[10][10],r1,c1,i,j,sum=0;
clrscr();
printf("Enter the rows and colums for matrix:\n");
scanf("%d%d",&r1,&c1);
if(r1<=10&&c1<=10){
printf("Enter the values: ");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
printf("\n Diagonal matrix is ");
printf("\n");
for(i=0;i<r1;i++){
for(j=0;j<c1;j++){
if(i==j)
sum=sum+a[i][j];
}
}
printf("%d",sum);
}
getch();
}
#include<conio.h>
void main(){
int a[10][10],r1,c1,i,j,sum=0;
clrscr();
printf("Enter the rows and colums for matrix:\n");
scanf("%d%d",&r1,&c1);
if(r1<=10&&c1<=10){
printf("Enter the values: ");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
printf("\n Diagonal matrix is ");
printf("\n");
for(i=0;i<r1;i++){
for(j=0;j<c1;j++){
if(i==j)
sum=sum+a[i][j];
}
}
printf("%d",sum);
}
getch();
}
C programme for print hello world
#include<stdio.h>
#include<conio.h>
void main(){
int a;
clrscr();
a=1;
while(a<=5){
printf("hello world",&a);
a++;
}
getch();
}
#include<conio.h>
void main(){
int a;
clrscr();
a=1;
while(a<=5){
printf("hello world",&a);
a++;
}
getch();
}
C programme for big number
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter the values:");
scanf("%d%d%d",&a,&b,&c);
(a>b&&a>c)?printf("a is big"):(b>a&&b>c)?printf("b is big"):printf("c is big");
getch();
}
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter the values:");
scanf("%d%d%d",&a,&b,&c);
(a>b&&a>c)?printf("a is big"):(b>a&&b>c)?printf("b is big"):printf("c is big");
getch();
}
C programme for sum of even numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1,sum=0;
while(i<=100){
if(i%2==0){
sum=sum+i;}
i++;
printf("%d",sum);
}
getch();
}
#include<conio.h>
void main()
{
int i=1,sum=0;
while(i<=100){
if(i%2==0){
sum=sum+i;}
i++;
printf("%d",sum);
}
getch();
}
C programme for leap year
#include<stdio.h>
#include<conio.h>
void main(){
int a;
printf("Enter the year");
scanf("&d",&a);
if(a%400==0){
printf("it is leaf year",a);
}
else if(a%100==0){
printf("it is not leaf year",a);
}
else if(a%4==0){
printf("it is leaf year");
}
else{
printf("it is not leaf year");
}
else{
printf("it is leaf year");
}
getch();
}
#include<conio.h>
void main(){
int a;
printf("Enter the year");
scanf("&d",&a);
if(a%400==0){
printf("it is leaf year",a);
}
else if(a%100==0){
printf("it is not leaf year",a);
}
else if(a%4==0){
printf("it is leaf year");
}
else{
printf("it is not leaf year");
}
else{
printf("it is leaf year");
}
getch();
}
C programme for password
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("Enter the pass word");
scanf("%d",&a);
(a>=1000&&a<9999)?printf("pass word is valid"):printf("pass word is not valid");
getch();
}
#include<conio.h>
void main()
{
int a;
clrscr();
printf("Enter the pass word");
scanf("%d",&a);
(a>=1000&&a<9999)?printf("pass word is valid"):printf("pass word is not valid");
getch();
}
C programme for single table
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
i=1;
j=7;
while(i<=10){
printf("%d*%d=%d\n",i,j,j*i);
i++;
}
getch();
}
#include<conio.h>
void main()
{
int i,j;
clrscr();
i=1;
j=7;
while(i<=10){
printf("%d*%d=%d\n",i,j,j*i);
i++;
}
getch();
}
C programme for even numbers upto n
#include<stdio.h>
#include<conio.h>
void main()
{
int a, n;
clrscr();
Printf ("Enter the value");
Scanf ("% d", &n);
a=2;
while(a>=n){
printf("%d",&a);}
a+2;
getch();
}
#include<conio.h>
void main()
{
int a, n;
clrscr();
Printf ("Enter the value");
Scanf ("% d", &n);
a=2;
while(a>=n){
printf("%d",&a);}
a+2;
getch();
}
Comments
Post a Comment