c-programming
all c programming examples to this blog.
Friday, 4 May 2012
Saturday, 28 April 2012
Hello world program
value to list hello community. This program printing hello community, printf operate is used to display textual content on display, '\n' locations pointer on the beginning of next line. This may be your first c value while learning development.
#include<stdio.h>
void main()
{
printf("Hello world\n");
getch(0;
}
We may store "hello world" in a character array and then print it.
#include<stdio.h>
void main()
{
char string[ ] = "Hello World";
printf("%s\n", string);
getch();
}
output:-
Addition
c program to add two numbers:-
#include<stdio.h>
void main()
{
int a, b, c;
printf("Enter two number to add\n");
scanf("%d%d",&a,&b);
c = a + b;
printf("Sum of Enter number = %d\n",c);
getch();
}
output:-
Addition without using third variable:-
#include<stdio.h>
void main()
{
int a = 1, b = 2;
/* Storing result of addition in variable a */
a = a + b;
/* Not recommended because original value of a is lost
* and you may be using it some where in code considering it
* as it was entered by the user.*/
printf("Sum of a and b = %d\n", a);
getch();
}
C program to add two numbers repeatedly:-
#include<stdio.h>
void main()
void main()
{
int a, b, c;
char ch;
while(1)
{
printf("Enter values of a and b\n");
scanf("%d%d",&a,&b);
c = a + b;
printf("a + b = %d\n", c);
printf("Do you wish to add more numbers(y/n)\n");
scanf(" %c",&ch);
if ( ch == 'y' || ch == 'Y' )
continue;
else
break;
}
getch();
}
Adding numbers in c using function:-
#include<stdio.h>
#include<conio.h>
long addition(long, long);
void main()
{
long first, second, sum;
scanf("%ld%ld", &first, &second);
sum = addition(first, second);
printf("%ld\n", sum);
getch();
}
long addition(long a, lengthy b)
{
long result;
return = a + b;
return result;
}
Addition,subtraction,multiplication and division
C program to perform basic arithmetic operations i.e. addition , subtraction, multiplication and division of two numbers. Numbers are assumed to be integers and will be entered by the user.
C program code:-
#include <stdio.h>
#include<conio.h>
void main()
{
int first, second, add, subtract, multiply;
float divide;
printf("Enter two integers\n");
scanf("%d%d", &first, &second);
add = first + second;
subtract = first - second;
multiply = first * second;
divide = first / (float)second; //typecasting
printf("Sum = %d\n",add);
printf("Difference = %d\n",subtract);
printf("Multiplication = %d\n",multiply);
printf("Division = %.2f\n",divide);
getch();
}
output:-
Check vowel
This code checks whether an input alphabet is a vowel or not. Both lower-case and upper-case are checked.
C programming code:-
#include<stdio.h>
void main()
{
char ch;
printf("Enter a character\n");
scanf("%c",&ch);
if ( ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch =='o' || ch=='O' || ch == 'u' || ch == 'U')
printf("%c is a vowel.\n", ch);
else
printf("%c is not a vowel.\n", ch);
getch();
}
Check vowel using switch statement:-
#include<conio.h>
void main()
{
char ch;
printf("Enter a character\n");
scanf("%c",&ch);
switch(ch)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case'O':
case 'u':
case 'U':
printf("%c is a vowel.\n", ch);
break;
default:
printf("%c is not a vowel.\n", ch);
}
getch();
}
odd or even
#include<stdio.h>
void main()
{
int n;
printf("Enter an integer\n");
scanf("%d",&n);
if ( n%2 == 0 )
printf("Even\n");
else
printf("Odd\n");
getch();
}
C program to check odd or even using bitwise operator:-
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
printf("Enter an integer\n");
scanf("%d",&n);
if ( n & 1 == 1 )
printf("Odd\n");
else
printf("Even\n");
getch();
}
C program to check odd or even without using bitwise or modulus operator:-
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
printf("Enter an integer\n");
scanf("%d",&n);
if ( (n/2)*2 == n )
printf("Even\n");
else
printf("Odd\n");
getch();
}
Subscribe to:
Comments (Atom)



