Showing posts with label C Programming beginners. Show all posts
Showing posts with label C Programming beginners. Show all posts

Monday, 9 September 2013

C Program to find out leap year

Write a program to find entered year is a leap year or not a leap year.

#include<conio.h>
#include<stdio.h>
void main()
{
int year;
clrscr();
printf("Enter Year:");
scanf("%d",&year);
if(year%4==0 || year%400==0)
{
printf("%d is leap year...!!!",year);
}
else
{
printf("%d is not a leap year...!!!",year);
}
getch();
}

O/P:


Thursday, 29 August 2013

C Programming beginners, for loop

Syntax : for(initalization;termination condition; increament)
FOR loop statement is defined by 3 parameters
1) Initialization of variable, which says that from which value loop will start
2) Termination condition, at what point loop should stop
3) Increament of  variable,

for(i=2;i<=5;i++)
{
//Some code ....
}
above loop will run four times
loop start at i=2 and every time it increase value of "i" by 1
when "i" values becomes 6, lop will stop.

//Write a program to print 1 to N number, where 0<N<=100
#include<conio.h>
#include<stdio.h>
void main()
{
   int n,i;
   clrscr();
   printf("Enter Value for N(0 - 100):");
   scanf("%d",&n);
   if(n>0 && n<=100)
   {
      for(i=1;i<=n;i++)
      {
         printf("%d ",i);
      }
   }
   else
   {
     printf("Invalid input.\n Enter Value from 1 to 100\n");
   }
   getch();
}

O/P:
Enter Value for N(0 - 100):105
Invalid input.\n Enter Value from 1 to 100

Enter Value for N(0 - 100):15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

-----------------------------------------------------------------
//Find odd/even number from 0 - 100 and count frequency for the same
#include<conio.h>
#include<stdio.h>
void main()
{
   int n=100,i,Total_Even=0,Total_Odd=0;
   clrscr();
   //Find Even Numbers
   printf("Even Numbers:");
   for(i=0;i<=100;i++)
   {
      if(i%2==0)
      {
         printf("%d ",i);
         Total_Even+=1;
      }
   }
   //Find Odd Numbers
   printf("\n\nOdd Numbers:");
   for(i=0;i<=100;i++)
   {
      if(i%2!=0)
      {
         printf("%d ",i);
         Total_Odd+=1;
      }
   }
   prinft("\n\nTotal Even No:%d\n Total Odd No:%d",Total_Even,Total_Odd);
   getch();
}

Tuesday, 27 August 2013

C Programming beginners, if...else programe

//SIMPLE IF..ELSE PROG. to FIND NUMBER IS ODD or EVEN NUMBER
#include<conio.h>
#include<stdio.h>
void main()
{
int n;
clrscr();
printf("Enter Number:");
scanf("%d",&n);
if(n%2==0)
{
printf("%d is EVEN Number.",n);
}
else
{
printf("%d is ODD number",n);
}
getch();
}
O/P:
Enter Number: 10
10 is EVEN Number

Enter Number: 13
13 is ODD number


-------------------------------------------------------------------------
//SIMPLE IF..ELSE PROG. to FIND NUMBER IS ZERO/POSITIVE/NEGATIVE
#include<conio.h>
#include<stdio.h>
void main()
{
int n;
clrscr();
printf("Enter Number:");
scanf("%d",&n);
if(n==0)
{
printf("You have entered ZERO");
}
else if(n>0)
{
printf("%d is POSITIVE number",n);
}
else
{
printf("%d is NEGATIVE number",n);
}
getch();
}

O/P:
Enter Number: 0
You have entered ZERO

Enter Number: 12
12 is POSITIVE number

Enter Number: -25
-25 is NEGATIVE number


-------------------------------------------------------------------------
//SIMPLE IF..ELSE PROG. to FIND NUMBER RANGE

#include<conio.h>
#include<stdio.h>
void main()
{
int n;
clrscr();
printf("Enter POSITIVE NUmber:");
scanf("%d",&n);
if(n>=0)
{
if(n>=0 && n<=10)
{
printf("%d is in between 0 to 10",n);
}
else if(n>=11 && n<=20)
{
printf("%d is in between 11 to 20",n);
}
else if(n>=21 && n<=50)
{
printf("%d is in between 21 to 50",n);
}

else if(n>50 && n<=100)
{
printf("%d is in between 51 to 100",n);
}
else
{
printf("%d is grater than 100",n);
}
}
else
{
printf("%d is NEGATIVE number",n);
}
getch();
}

O/P:
Enter Number: 25
25 is in between 21 to 50

Enter Number: 7
7 is in between 0 to 10

Enter Number: -9
-9 is NEGATIVE NUMBER

Enter Number: 15
15 is in between 11 to 20

Enter Number: 85
85 is in between 51 to 100

Enter Number: 105
105 is greater than 100

-------------------------------------------------------------------------
//SIMPLE IF..ELSE PROG. to FIND Minimum & Maximum Number
#include<conio.h>
#include<stdio.h>
void main()
{
int n,m,Calc;
clrscr();
printf("Enter Value for N:");
scanf("%d",&n);
printf("Enter Value for M:");
scanf("%d",&m);

if(n==m)
{
printf("N & M both are equal\n");
}
else
{
 if(m>n)
{
printf("M:%d is Greater Than N:%d\n",m,n);
}
else
{
printf("N:%d is Greater Than M:%d\n",n,m);
}
}

printf("N+M = %d",(n+m));
printf("N-M = %d",(n-m));
printf("N*M = %d",(n+m));
printf("N/M = %f",((float)n/(float)m));
getch();
}

O/P:
check yourself

Friday, 23 August 2013

C Programming beginners, Read Value from user in string

Reading value in string from user and display it in format

1)Read Single Character
#include<conio.h>
#include<stdio.h>
void main()
{
char str1;
clrscr();
printf("Enter Your Name:");
scanf("%c",&str1);
printf("\n\nYou have entered:%c",str1);
getch();
}
O/P:


2) Read Single Word
#include<conio.h>
#include<stdio.h>
void main()
{
char str1;
clrscr();
printf("Enter Your Name:");
scanf("%s",str1);//Read single word
printf("\n\nYou have entered:%c, ",&str1);//It will give some random Char
printf("\n\nYou have entered:%s",str1);//This will display Single Word
getch();
}
O/P:



3)Read Sentence
#include<conio.h>
#include<stdio.h>
void main()
{
char str1;//Store 2 byte data
clrscr();
printf("Enter Your Name:");
scanf("%[^\n]",str1);//Read single word
printf("\n\nYou have entered:%s",str1);
getch();
}
O/P:
This will read only 1 byte data. and more than 1 byte will result into garbage.


4)To overcome problem of above program string pointer is used.
in this program instead of char str1 we'll use pointer to read complete string
and it is declared  as char *str1, now it hold no. of bytes. and to read complete sentence we'll use
%[^\n].

#include<conio.h>
#include<stdio.h>
void main()
{
char *str1;
clrscr();
printf("Enter Your Name:");
scanf("%[^\n]",str1);//Read single word
printf("\n\nYou have entered:%s",str1);
getch();
}
O/P:

Thursday, 22 August 2013

C Programming beginners, Read Value from user

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

printf("Enter Value for A:");
scanf("%d",&a);// "&" points to address of variable where value will be stored
printf("Enter Value for B:");
scanf("%d",&b);
printf("Enter Value for C:");
scanf("%d",&c);
printf("You have entered...\nA:%d\nB:%d\nC:%d",a,b,c);

printf("\n\nEnter float value( value that contains decimal place, six digit after decimal point) ");
printf("\nEnter Value for X:");
scanf("%f",&x);
printf("Enter Value for Y:");
scanf("%f",&y);
printf("\nYou have entered...\nX:%d\nY:%d",x,y);
printf("\nYou have entered...\nX:%f\nY:%f",x,y);
printf("\none decimal place\nX:%.1f\nY:%.1f",x,y);
printf("\ntwo decimal places\nX:%.2f\nY:%.2f",x,y);


getch();
}

O/P:

Monday, 12 August 2013

C Programming beginners, Local Variable declaration

getch() is the function which scans single key stroke.
so it'll be used to check output at the end of execution of program.

conio.h : Console input output
stdio.h  : Standar input output

printf() is declared in stdio.h file while getch() is declared in conio.h file.

//Use of getch()
#include<conio.h>
#include<stdio.h>
void main()
{
   printf("Hello World...!!!");
   getch();
}



//Simple variable declaration

#include<conio.h>
#include<stdio.h>
void main()
{
   int a;
   clrscr();// clear screen - clears previous screen out put

   a=10;
   printf("Value of a:%d",a);
   getch();
}

output :
Value of a:10

//Multiple variable declaration

#include<conio.h>
#include<stdio.h>
void main()
{
   int a,b;
   clrscr();// clear screen - clears previous screen out put

   a=10;
   b=22;
   printf("Value of a:%d",a);
   printf("Value of b:%d",b);
   printf("\n");//\n to print next statement in new line
   printf("Value of a:%d and b:%d\n",a,b);
   printf("Value of a:%d and b:%d\n",b,a);
   getch();
}

output :
Value of a:10Value of b:22
Value of a:10 and b:22
Value of a:22 and b:10



Contact Form

Name

Email *

Message *