Like us on Facebook

September 26, 2015

Basics and Common C Programs Part 1

  9/26/2015 No comments


Friends today  I am posting some of the common C  Program that is useful
when we are learning basics of Java for the first time or can be useful for
interview related questions.


< 1./*How to Check whether a number a number is even or odd*/

#include<stdio.h>
#include<conio.h>
//Header files required are declared

void main()
{
//Beginning Of main()
int n; // Required variables are declared and initialized
clrscr();//Output Screen cleared

puts("Enter The Number To Check Even Or Odd : ");
scanf("%d",&n);//The Number Is accepted

if(n%2==0)//Checking for even condition
printf("\n%d Is An Even Number. \n",n);

else
printf("\n%d Is An Odd Number. \n",n);

getch();
}//End Of main()

2./*How to Check whether a year is leap year or not*/

#include<stdio.h>
#include<conio.h>
//Header files are declared

void main()
{//Beginnng of function main()
int year;
clrscr();

printf("\nEnter year to Check ");
scanf("%d",&year);//Year entered from user

if(year==0)
{//Check for invalid input
printf("\n* Invalid input *, Press any key to exit");
getch();
exit(0);
}

if(year<400)
{
if(year%4==0)
{
printf("\nLeap Year ");
}
else
printf("\nNot a Leap Year ");
}

else
{
if(year%4==0 && year%400==0)
{
printf("\nLeap Year ");
}

else if(year%4==0 && year%100!=0)
{
printf("\n Leap Year ");
}

else
{
printf("\nNot a Leap Year ");
}
}
getch();
}//End of function main()


3./*How to do Swapping of two numbers(Using or without using 
third variable)*/

#include<stdio.h>
#include<conio.h>
//Header files required are declared

void with(int a,int b)
{
int c;
c=a;
a=b;
b=c;
puts("Swapped Using Third Variable : ");
printf("a = %d\tb = %d\n",a,b);
}

void without(int a,int b)
{
a+=b;
b=a-b;
a=a-b;
puts("Swapped Without Using Third Variable : ");
printf("a = %d\tb = %d\n",a,b);
}

void main()
{
//Beginning Of main()
int i,a,b; // Required variables are declared and initialized
clrscr();//Output Screen cleared

puts("Enter The Two Numbers To Be Swapped :");
scanf("%d%d",&a,&b);//The Numbers Are accepted
printf("\nBefore Swapping a = %d\tb = %d\n",a,b);
with(a,b);
without(a,b);
getch();
}//End Of main()

4./*How to do Sum of n natural numbers*/

#include<stdio.h>
#include<conio.h>
//Header files required are declared

void main()
{
//Beginning Of main()
long i,n,s=0; // Required variables are declared and initialized
clrscr();//Output Screen cleared
printf("Enter The Limit Of The Loop : \n");
        scanf("%ld",&n);
for(i=1;i<=n;i++)
s+=i;
printf("\n%ld Natural Numbers Add Upto %ld. \n",n,s);
getch();
}//End Of main()

5./*How to To find the factorial of an user entered number*/

#include<stdio.h>
#include<conio.h>
//Header files are declared

long int fact(int n)
{//Beginning of function fact()
int i;
long int prod=1;//Required variables declared and initialized
if(n>1)
{
for(i=2;i<=n;i++)
prod *=i;
}
return(prod);//Returning the value of factorial to main()
}//End of function fact()

void main()
{//Beginning of function main()
int n;
    clrscr();

printf("Enter the number to find its factorial : \n");
scanf("%d",&n);

printf("\nThe factorial of %d is : %ld\n",n, fact(n));
getch();
}//End of function main()
.
Related Basics and Common C Programs Part 2

Author: Vikas Pandey

He is a software and web developer working for renowned European investment bank.He loves to share knowledge through blogging whatever he knows and encourages others as well to share the same to make world a better place to live.

0 comments:

.
© 2014-2015 Informational Digit : How-To & Tech Guides. The content is copyrighted to Vikas Pandey and may not be reproduced on other websites. WP themonic converted by Bloggertheme9.
TOP