hello world example
#include
int main()
{
printf(“Hello world\n”);
return 0;
}
Hello world program in c
We may store “hello world” in a character array and then print it.
#include
int main()
{
char string[] = “Hello World”;
printf(“%s\n”, string);
return 0;
}
c program print integer
This c program first inputs an integer and then prints it. Input is done using scanf function and number is printed on screen
using printf.
C programming code
#include
main()
{
int a;
printf(“Enter an integer\n”);
scanf(“%d”, &a);
printf(“Integer that you have entered is %d\n”, a);
return 0;
}
Output:
input number
c program to add two numbers
C program to add two numbers: This c language program perform the basic arithmetic operation of addition on two numbers and
then prints the sum on the screen. For example if the user entered two numbers as 5, 6 then 11 ( 5 + 6 ) will be printed on
the screen.
C programming code
#include
main()
{
int a, b, c;
printf(“Enter two numbers to add\n”);
scanf(“%d%d”,&a,&b);
c = a + b;
printf(“Sum of entered numbers = %d\n”,c);
return 0;
}
program to check odd or even using modulus operator
#include
main()
{
int n;
printf(“Enter an integer\n”);
scanf(“%d”,&n);
if ( n%2 == 0 )
printf(“Even\n”);
else
printf(“Odd\n”);
return 0;
}
C program to check odd or even using bitwise operator
#include
main()
{
int n;
printf(“Enter an integer\n”);
scanf(“%d”,&n);
if ( n & 1 == 1 )
printf(“Odd\n”);
else
printf(“Even\n”);
return 0;
}
C program to check odd or even without using bitwise or modulus operator
#include
main()
{
int n;
printf(“Enter an integer\n”);
scanf(“%d”,&n);
if ( (n/2)*2 == n )
printf(“Even\n”);
else
printf(“Odd\n”);
return 0;
}
Find odd or even using conditional operator
#include
main()
{
int n;
printf(“Enter an integer\n”);
scanf(“%d”,&n);
n%2 == 0 ? printf(“Even number\n”) : printf(“Odd number\n”);
return 0;
}
Comments
1 comment posted
To check the given no. is even or odd without using % operator
#include
main()
{
char *a[]={“Odd”, “Even”};
int n;
printf(“Enter an integer\n”);
scanf(“%d”,&n);
printf(“%s\n”, a[(n/2)*2 == n]);
return 0;
}
C program to perform 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 programming code
#include
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);
return 0;
}
In c language when we divide two integers we get integer result for example 5/2 evaluates to 2. As a general rule
integer/integer = integer and float/integer = float or integer/float = float. So we convert denominator to float in our
program, you may also write float in numerator. This explicit conversion is known as typecasting.
Arithmetic operations.
Output of program:
c program to check whether input alphabet is a vowel or not
This code checks whether an input alphabet is a vowel or not. Both lower-case and upper-case are checked.
C programming code
#include
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);
return 0;
}
Output:
check character case output
Check vowel using switch statement
#include
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);
}
return 0;
}
Function to check vowel
int check_vowel(char a)
{
if (a >= ‘A’ && a <= 'Z')
a = a + 'a' – 'A'; /* Converting to lower case or use a = a + 32 */
if (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
return 1;
return 0;
}
c program to check leap year
c program to check leap year: c code to check leap year, year will be entered by the user. Please read the leap year article
before reading the code, it will help you to understand the program.
C programming code
#include
int main()
{
int year;
printf(“Enter a year to check if it is a leap year\n”);
scanf(“%d”, &year);
if ( year%400 == 0)
printf(“%d is a leap year.\n”, year);
else if ( year%100 == 0)
printf(“%d is not a leap year.\n”, year);
else if ( year%4 == 0 )
printf(“%d is a leap year.\n”, year);
else
printf(“%d is not a leap year.\n”, year);
return 0;
}
add digits of number in c
C program to add digits of a number: Here we are using modulus operator(%) to extract individual digits of number and adding
them.
C programming code
#include
main()
{
int n, sum = 0, remainder;
printf(“Enter an integer\n”);
scanf(“%d”,&n);
while(n != 0)
{
remainder = n % 10;
sum = sum + remainder;
n = n / 10;
}
printf(“Sum of digits of entered number = %d\n”,sum);
return 0;
}
Factorial program in c
Factorial program in c: c code to find and print factorial of a number, three methods are given, first one uses a for loop,
second uses a function to find factorial and third using recursion. Factorial is represented using !, so five factorial will
be written as 5!, n factorial as n!. Also
n! = n*(n-1)*(n-2)*(n-3)…3.2.1 and zero factorial is defined as one i.e. 0!=1.
Factorial program in c using for loop
: Here we find factorial using for loop.
#include
#include
main()
{
int c, n, fact = 1;
printf(“Enter a number to calculate it’s factorial\n”);
scanf(“%d”,&n);
for( c = 1 ; c <= n ; c++ )
fact = fact*c;
printf("Factorial of %d = %d\n",n,fact);
getch();
return 0;
}
c program to find hcf and lcm
C program to find hcf and lcm: The code below finds highest common factor and least common multiple of two integers. HCF is
also known as greatest common divisor(GCD) or greatest common factor(gcf).
C programming code
#include
int main() {
int a, b, x, y, t, gcd, lcm;
printf(“Enter two integers\n”);
scanf(“%d%d”, &x, &y);
a = x;
b = y;
while (b != 0) {
t = b;
b = a % b;
a = t;
}
gcd = a;
lcm = (x*y)/gcd;
printf(“Greatest common divisor of %d and %d = %d\n”, x, y, gcd);
printf(“Least common multiple of %d and %d = %d\n”, x, y, lcm);
return 0;
}
C program to find hcf and lcm using recursion
#include
long gcd(long, long);
int main() {
long x, y, hcf, lcm;
printf(“Enter two integers\n”);
scanf(“%ld%ld”, &x, &y);
hcf = gcd(x, y);
lcm = (x*y)/hcf;
printf(“Greatest common divisor of %ld and %ld = %ld\n”, x, y, hcf);
printf(“Least common multiple of %ld and %ld = %ld\n”, x, y, lcm);
return 0;
}
long gcd(long a, long b) {
if (b == 0) {
return a;
}
else {
return gcd(b, a % b);
}
}
C program to find hcf and lcm using function
#include
long gcd(long, long);
int main() {
long x, y, hcf, lcm;
printf(“Enter two integers\n”);
scanf(“%ld%ld”, &x, &y);
hcf = gcd(x, y);
lcm = (x*y)/hcf;
printf(“Greatest common divisor of %ld and %ld = %ld\n”, x, y, hcf);
printf(“Least common multiple of %ld and %ld = %ld\n”, x, y, lcm);
return 0;
}
long gcd(long x, long y) {
if (x == 0) {
return y;
}
while (y != 0) {
if (x > y) {
x = x – y;
}
else {
y = y – x;
}
}
return x;
}
Decimal to binary conversion
C program to convert decimal to binary: c language code to convert an integer from decimal number system(base-10) to binary
number system(base-2). Size of integer is assumed to be 32 bits. We use bitwise operators to perform the desired task. We
right shift the original number by 31, 30, 29, …, 1, 0 bits using a loop and bitwise AND the number obtained with 1(one),
if the result is 1 then that bit is 1 otherwise it is 0(zero).
C programming code
#include
int main()
{
int n, c, k;
printf(“Enter an integer in decimal number system\n”);
scanf(“%d”, &n);
printf(“%d in binary number system is:\n”, n);
for (c = 31; c >= 0; c–)
{
k = n >> c;
if (k & 1)
printf(“1”);
else
printf(“0”);
}
printf(“\n”);
return 0;
}
Above code only prints binary of integer, but we may wish to perform operations on binary so in the code below we are storing
the binary in a string. We create a function which returns a pointer to string which is the binary of the number passed as
argument to the function.
C code to store decimal to binary conversion in a string
#include
#include
char *decimal_to_binary(int);
main()
{
int n, c, k;
char *pointer;
printf(“Enter an integer in decimal number system\n”);
scanf(“%d”,&n);
pointer = decimal_to_binary(n);
printf(“Binary string of %d is: %s\n”, n, t);
free(pointer);
return 0;
}
char *decimal_to_binary(int n)
{
int c, d, count;
char *pointer;
count = 0;
pointer = (char*)malloc(32+1);
if ( pointer == NULL )
exit(EXIT_FAILURE);
for ( c = 31 ; c >= 0 ; c– )
{
d = n >> c;
if ( d & 1 )
*(pointer+count) = 1 + ‘0’;
else
*(pointer+count) = 0 + ‘0’;
count++;
}
*(pointer+count) = ”;
return pointer1;
}
c program to find ncr and npr
c program to find nCr and nPr: This code calculate nCr which is n!/(r!*(n-r)!) and nPr = n!/(n-r)!
C program to find nCr using function
#include
long factorial(int);
long find_ncr(int, int);
long find_npr(int, int);
main()
{
int n, r;
long ncr, npr;
printf(“Enter the value of n and r\n”);
scanf(“%d%d”,&n,&r);
ncr = find_ncr(n, r);
npr = find_npr(n, r);
printf(“%dC%d = %ld\n”, n, r, ncr);
printf(“%dP%d = %ld\n”, n, r, npr);
return 0;
}
long find_ncr(int n, int r)
{
long result;
result = factorial(n)/(factorial(r)*factorial(n-r));
return result;
}
long find_npr(int n, int r)
{
long result;
result = factorial(n)/factorial(n-r);
return result;
}
long factorial(int n)
{
int c;
long result = 1;
for( c = 1 ; c <= n ; c++ )
result = result*c;
return ( result );
}
c program to add n numbers
This c program add n numbers which will be entered by the user. Firstly user will enter a number indicating how many numbers
user wishes to add and then user will enter n numbers. In the first c program to add numbers we are not using an array, and
using array in the second code.
C programming code
#include
int main()
{
int n, sum = 0, c, value;
printf(“Enter the number of integers you want to add\n”);
scanf(“%d”, &n);
printf(“Enter %d integers\n”,n);
for (c = 1; c <= n; c++)
{
scanf("%d",&value);
sum = sum + value;
}
printf("Sum of entered integers = %d\n",sum);
return 0;
}
C programming code using array
#include
int main()
{
int n, sum = 0, c, array[100];
scanf(“%d”, &n);
for (c = 0; c < n; c++)
{
scanf("%d", &array[c]);
sum = sum + array[c];
}
printf("Sum = %d\n",sum);
return 0;
}
c program to swap two numbers
C program to swap two numbers with and without using third variable, swapping in c using pointers, functions (Call by
reference) and using bitwise XOR operator, swapping means interchanging. For example if in your c program you have taken two
variable a and b where a = 4 and b = 5, then before swapping a = 4, b = 5 after swapping a = 5, b = 4
In our c program to swap numbers we will use a temp variable to swap two numbers. Swapping is used in sorting that is when we
wish to arrange numbers in a particular order either in ascending order or in descending order.
Swapping of two numbers in c
#include
int main()
{
int x, y, temp;
printf(“Enter the value of x and y\n”);
scanf(“%d%d”, &x, &y);
printf(“Before Swapping\nx = %d\ny = %d\n”,x,y);
temp = x;
x = y;
y = temp;
printf(“After Swapping\nx = %d\ny = %d\n”,x,y);
return 0;
}
Swapping of two numbers without third variable
You can also swap two numbers without using temp or temporary or third variable. In that case c program will be as shown :-
#include
int main()
{
int a, b;
printf(“Enter two integers to swap\n”);
scanf(“%d%d”, &a, &b);
a = a + b;
b = a – b;
a = a – b;
printf(“a = %d\nb = %d\n”,a,b);
return 0;
}
To understand above logic simply choose a as 7 and b as 9 and then do what is written in program. You can choose any other
combination of numbers as well. Sometimes it’s a good way to understand a program.
Swap two numbers using pointers
#include
int main()
{
int x, y, *a, *b, temp;
printf(“Enter the value of x and y\n”);
scanf(“%d%d”, &x, &y);
printf(“Before Swapping\nx = %d\ny = %d\n”, x, y);
a = &x;
b = &y;
temp = *b;
*b = *a;
*a = temp;
printf(“After Swapping\nx = %d\ny = %d\n”, x, y);
return 0;
}
Swapping numbers using call by reference
In this method we will make a function to swap numbers.
#include
void swap(int*, int*);
int main()
{
int x, y;
printf(“Enter the value of x and y\n”);
scanf(“%d%d”,&x,&y);
printf(“Before Swapping\nx = %d\ny = %d\n”, x, y);
swap(&x, &y);
printf(“After Swapping\nx = %d\ny = %d\n”, x, y);
return 0;
}
void swap(int *a, int *b)
{
int temp;
temp = *b;
*b = *a;
*a = temp;
}
C programming code to swap using bitwise XOR
#include
int main()
{
int x, y;
scanf(“%d%d”, &x, &y);
printf(“x = %d\ny = %d\n”, x, y);
x = x ^ y;
y = x ^ y;
x = x ^ y;
printf(“x = %d\ny = %d\n”, x, y);
return 0;
}
c program to reverse a number
C Program to reverse a number :- This program reverse the number entered by the user and then prints the reversed number on
the screen. For example if user enter 123 as input then 321 is printed as output. In our program we use modulus(%) operator
to obtain the digits of a number. To invert number look at it and write it from opposite direction or the output of code is a
number obtained by writing original number from right to left. To reverse large numbers use long data type or long long data
type if your compiler supports it, if you still have large numbers then use strings or other data structure.
C programming code
#include
main()
{
int n, reverse = 0;
printf(“Enter a number to reverse\n”);
scanf(“%d”,&n);
while (n != 0)
{
reverse = reverse * 10;
reverse = reverse + n%10;
n = n/10;
}
printf(“Reverse of entered number is = %d\n”, reverse);
return 0;
}
Palindrome Numbers
Palindrome number in c: A palindrome number is a number such that if we reverse it, it will not change. For example some
palindrome numbers examples are 121, 212, 12321, -454. To check whether a number is palindrome or not first we reverse it and
then compare the number obtained with the original, if both are same then number is palindrome otherwise not. C program for
palindrome number is given below.
Palindrome number algorithm
1. Get the number from user.
2. Reverse it.
3. Compare it with the number entered by the user.
4. If both are same then print palindrome number
5. Else print not a palindrome number.
Palindrome number program c
#include
main()
{
int n, reverse = 0, temp;
printf(“Enter a number to check if it is a palindrome or not\n”);
scanf(“%d”,&n);
temp = n;
while( temp != 0 )
{
reverse = reverse * 10;
reverse = reverse + temp%10;
temp = temp/10;
}
if ( n == reverse )
printf(“%d is a palindrome number.\n”, n);
else
printf(“%d is not a palindrome number.\n”, n);
return 0;
}
c program to print patterns of numbers and stars
These program prints various different patterns of numbers and stars. These codes illustrate how to create various patterns
using c programming. Most of these c programs involve usage of nested loops and space. A pattern of numbers, star or
characters is a way of arranging these in some logical manner or they may form a sequence. Some of these patterns are
triangles which have special importance in mathematics. Some patterns are symmetrical while other are not. Please see the
complete page and look at comments for many different patterns.
*
***
*****
*******
*********
We have shown five rows above, in the program you will be asked to enter the numbers of rows you want to print in the pyramid
of stars.
C programming code
#include
main()
{
int row, c, n, temp;
printf(“Enter the number of rows in pyramid of stars you wish to see “);
scanf(“%d”,&n);
temp = n;
for ( row = 1 ; row <= n ; row++ )
{
for ( c = 1 ; c < temp ; c++ )
printf(" ");
temp–;
for ( c = 1 ; c <= 2*row – 1 ; c++ )
printf("*");
printf("\n");
}
return 0;
}
Output:
stars pyramid
Consider the pattern
*
**
***
****
*****
to print above pattern see the code below:
#include
main()
{
int n, c, k;
printf(“Enter number of rows\n”);
scanf(“%d”,&n);
for ( c = 1 ; c <= n ; c++ )
{
for( k = 1 ; k <= c ; k++ )
printf("*");
printf("\n");
}
return 0;
}
For more patterns or shapes on numbers and characters see comments below and also see codes on following pages:
Floyd triangle
Pascal triangle
Comments
48 comments posted
i need a code for this pattern.
12345
1234
123
12
1
Posted by Guest on Fri, 26/08/2011 – 12:35
number pattern c code
#include
main()
{
int n, c, k, space;
scanf(“%d”, &n);
space = 0;
for ( k = n ; k >= 1 ; k– )
{
for ( c = 1 ; c <= space ; c++ )
printf(" ");
space++;
for ( c = 1 ; c <= k ; c++)
printf("%d", c);
printf("\n");
}
return 0;
}
Posted by adminPs on Fri, 26/08/2011 – 17:06
thanks for the patterns it
thanks for the patterns it really helped me a lot.
Posted by Guest on Tue, 23/08/2011 – 22:04
i need code of this pattern
A B C D E F G H
A B C D E F G
A B C D E F
A B C D E
A B C D
A B C
A B
A
Posted by Guest on Sat, 20/08/2011 – 18:07
c program for character pattern
#include
main()
{
char ch = ‘A’;
int n, c, k, space = 0;
scanf(“%d”, &n);
for ( k = n ; k >= 1 ; k– )
{
for ( c = 1 ; c <= space ; c++)
printf(" ");
space++;
for ( c = 1 ; c <= k ; c++ )
{
printf("%c ", ch);
ch++;
}
printf("\n");
ch = 'A';
}
return 0;
}
Posted by adminPs on Sat, 20/08/2011 – 21:29
c language
diamond pattern of stars
Posted by Guest on Sat, 20/08/2011 – 14:51
diamond pattern
Diamond pattern
Posted by adminPs on Fri, 26/08/2011 – 09:48
please help give me source code
1
01
010
1010
10101
Posted by Guest on Fri, 19/08/2011 – 17:38
c program to print pattern
#include
main()
{
int n, c, k, num = 1;
scanf(“%d”, &n);
for ( c = 1 ; c <= n ; c++ )
{
for ( k = 1 ; k <= c ; k++ )
{
printf("%d", num);
if ( num == 0 )
num = 1;
else
num = 0;
}
printf("\n");
}
return 0;
}
Posted by adminPs on Sat, 20/08/2011 – 08:43
can you write code for this
p
pr
pro
prog
progr
progra
program
Posted by Guest on Thu, 18/08/2011 – 21:20
pattern for string
Just input the string and press enter, corresponding pattern will be printed.
#include
#include
main()
{
char string[100];
int c, k, length;
printf(“Enter a string\n”);
gets(string);
length = strlen(string);
for ( c = 0 ; c < length ; c++ )
{
for( k = 0 ; k <= c ; k++ )
{
printf("%c", string[k]);
}
printf("\n");
}
return 0;
}
Posted by adminPs on Thu, 18/08/2011 – 21:59
c programming code
please get me the code of the c program to get the output as follows:
1
121
12321
1234321
123454321
Posted by Guest on Thu, 18/08/2011 – 20:40
c program to print number pattern
#include
main()
{
int n, c, k, x = 1;
scanf(“%d”, &n);
for ( c = 1 ; c <= n ; c++ )
{
for ( k = 1 ; k <= c ; k++ )
{
printf("%d", x);
x++;
}
x–;
for ( k = 1 ; k <= c – 1 ; k++ )
{
x–;
printf("%d", x);
}
printf("\n");
x = 1;
}
return 0;
}
Posted by adminPs on Thu, 18/08/2011 – 22:10
c++ code to print pattern
1 1
12 21
12321
Posted by Guest on Wed, 17/08/2011 – 19:15
c++ code to print pattern
#include
using namespace std;
main()
{
int n, k, c, space, x, num = 1;
cin >> n;
x = n;
for ( k = 1 ; k <= n ; k++ )
{
for ( c = 1 ; c <= k ; c++ )
{
cout << num;
num++;
}
num–;
for ( c = 1 ; c <= 2*x – 3 ; c++ )
cout << " ";
x–;
if ( k != n )
{
for ( c = 1 ; c <= k ; c++ )
{
cout << num;
num–;
}
}
else
{
num–;
for ( c = 1 ; c <= k – 1 ; c++ )
{
cout << num;
num–;
}
}
printf("\n");
num = 1;
}
return 0;
}
Posted by adminPs on Thu, 18/08/2011 – 17:14
may i get its source code
1
232
34543
4567654
567898765
Posted by Guest on Tue, 16/08/2011 – 21:34
c code for number pattern
#include
main()
{
int n, c, d, num = 1, space;
scanf(“%d”,&n);
space = n – 1;
for ( d = 1 ; d <= n ; d++ )
{
num = d;
for ( c = 1 ; c <= space ; c++ )
printf(" ");
space–;
for ( c = 1 ; c <= d ; c++ )
{
printf("%d", num);
num++;
}
num–;
num–;
for ( c = 1 ; c < d ; c++)
{
printf("%d", num);
num–;
}
printf("\n");
}
return 0;
}
Posted by adminPs on Thu, 18/08/2011 – 17:17
how to print this pattern
*******
***S***
**SSS**
*SSSSS*
where S represents Space
*******
*** ***
** **
* *
Posted by Guest on Sat, 13/08/2011 – 17:53
c code for star pattern
#include
main()
{
int n, c, k, space, r;
printf(“Enter number of rows\n”);
scanf(“%d”,&n);
space = 1;
r = n-1;
for( c = 1 ; c <= 2*n – 1 ; c++ )
printf("*");
printf("\n");
for ( k = 2 ; k <= n ; k++ )
{
for( c = 1 ; c <= r ; c++ )
printf("*");
for ( c = 1 ; c <= space ; c++ )
printf(" ");
space = 2*k-1;
for( c = 1 ; c <= r ; c++ )
printf("*");
r–;
printf("\n");
}
return 0;
}
Posted by adminPs on Sat, 13/08/2011 – 19:17
Doubt
In the program, instead of the statement space=2*k-1; , can we go for the statement, space+=2; ?
Posted by Guest on Mon, 22/08/2011 – 09:47
sure
You can use but don't forget to initialize variable space to one.
Posted by adminPs on Mon, 22/08/2011 – 13:01
code
guys i need the code for this
*
**
***
****
*****
Posted by Guest on Sat, 13/08/2011 – 17:15
star pattern source code
#include
main()
{
int n, c, k, space;
printf(“Enter number of rows\n”);
scanf(“%d”,&n);
space = n;
for ( k = 1 ; k <= n ; k++ )
{
for ( c = 1 ; c < space ; c++ )
printf(" ");
space–;
for( c = 1 ; c <= k ; c++ )
printf("*");
printf("\n");
}
return 0;
}
Posted by adminPs on Sat, 13/08/2011 – 17:29
i need a program to draw the below patern
1
22
333
4444
55555
Posted by Guest on Fri, 12/08/2011 – 18:48
number pattern source code
#include
main()
{
int n, c, k;
printf(“Enter number of rows\n”);
scanf(“%d”,&n);
for ( c = 1 ; c <= n ; c++ )
{
for( k = 1 ; k <= c ; k++ )
printf("%d", c);
printf("\n");
}
return 0;
}
Posted by adminPs on Fri, 12/08/2011 – 20:13
need help
i need program to print the following pattern ::
1
2 3
4 5 6
7 8 9 10
Posted by Guest on Thu, 11/08/2011 – 20:14
Floyd's triangle
This pattern is Floyd's triangle see Floyd's Triangle code.
Posted by adminPs on Fri, 12/08/2011 – 07:31
need a code in c to print below pattern
i want a code to print stars like below
*
**
***
****
***
**
*
Posted by Guest on Tue, 09/08/2011 – 21:46
stars pattern using c programming
#include
main()
{
int n, c, k;
printf(“Enter number of rows\n”);
scanf(“%d”,&n);
for ( c = 1 ; c <= n ; c++)
{
for ( k = 1 ; k = 0 ; c– )
{
for ( k = c ; k >= 0 ; k– )
printf(“*”);
printf(“\n”);
}
return 0;
}
Posted by adminPs on Tue, 09/08/2011 – 22:38
GET ME THE CODE OF A PROGRAM
GET ME THE CODE OF A PROGRAM TO GET OUTPUT AS FOLLOWS:
*
*A*
*A*A*
*A*A*A*
Posted by Guest on Tue, 09/08/2011 – 17:21
code to print pattern of stars and numbers
#include
main()
{
int n, c, k, space, count = 1;
printf(“Enter number of rows\n”);
scanf(“%d”,&n);
space = n;
for ( c = 1 ; c <= n ; c++)
{
for( k = 1 ; k < space ; k++)
printf(" ");
for ( k = 1 ; k 1 && count < c)
{
printf("A");
count++;
}
}
printf("\n");
space–;
count = 1;
}
return 0;
}
Posted by adminPs on Tue, 09/08/2011 – 21:46
codes for pyramid of stars.
i need this pattern
* * * * *
* * *
* *
*
Posted by Guest on Mon, 08/08/2011 – 13:18
code for pattern
#include
main()
{
int n, c, k, temp;
printf(“Enter number of rows\n”);
scanf(“%d”,&n);
temp = n;
for ( c = 1 ; c <= n ; c++ )
{
for( k = 1 ; k <= temp ; k++ )
printf("* ");
temp–;
printf("\n");
}
return 0;
}
Posted by adminPs on Mon, 08/08/2011 – 15:32
Please post the code for this
*
* *
* * *
* * * *
Please post the code to print the following shape as soon as possible.
Posted by Guest on Mon, 08/08/2011 – 13:01
code
#include
main()
{
int n, c, k;
printf(“Enter number of rows\n”);
scanf(“%d”,&n);
for ( c = 1 ; c <= n ; c++ )
{
for( k = 1 ; k <= c ; k++ )
printf("* ");
printf("\n");
}
return 0;
}
Posted by adminPs on Mon, 08/08/2011 – 15:29
please help
i need c program to print no in this format
1
1 2
1 2 3
1 2 3 4
Posted by Guest on Sat, 06/08/2011 – 13:59
source code
#include
main()
{
int number = 1, n, c, k;
printf(“Enter number of rows\n”);
scanf(“%d”,&n);
for ( c = 1 ; c <= n ; c++ )
{
for( k = 1 ; k <= c ; k++ )
{
printf("%d ", number);
number++;
}
number = 1;
printf("\n");
}
return 0;
}
Posted by adminPs on Sun, 07/08/2011 – 11:32
Thanks giving and suggestion
Thanks for the pyramid program!! Please help me in the same problem.
Can't we directly print k directly instead of number(in 2nd inner for loop)?
I have tried it but output gives garbage values!!why does this happen? Please help me!
Posted by Guest on Mon, 08/08/2011 – 22:02
number pattern code
You are right there is no need to use variable number you can use k directly as in code below:
#include
main()
{
int n, c, k;
printf(“Enter number of rows\n”);
scanf(“%d”,&n);
for ( c = 1 ; c <= n ; c++ )
{
for( k = 1 ; k <= c ; k++ )
printf("%d ", k);
printf("\n");
}
return 0;
}
Posted by adminPs on Tue, 09/08/2011 – 11:19
i need a code in C to print pyramid of stars
i need a code in C to print stars like this
*
* *
* * *
* * * *
please help?
Posted by Guest on Fri, 05/08/2011 – 11:53
c code to print pattern
#include
main()
{
int n, c, k = 2, j;
printf(“Enter number of rows\n”);
scanf(“%d”,&n);
for ( j = 1 ; j <= n ; j++ )
{
for ( c = 1 ; c <= 2*n-k ; c++)
printf(" ");
k = k + 2;
for ( c = 1 ; c <= j ; c++)
printf("* ");
printf("\n");
}
getch();
return 0;
}
c program to print diamond pattern
Diamond pattern in c: This code print diamond pattern of stars. Diamond shape is as follows:
*
***
*****
***
*
C programming code
#include
int main()
{
int n, c, k, space = 1;
printf(“Enter number of rows\n”);
scanf(“%d”, &n);
space = n – 1;
for (k = 1; k <= n; k++)
{
for (c = 1; c <= space; c++)
printf(" ");
space–;
for (c = 1; c <= 2*k-1; c++)
printf("*");
printf("\n");
}
space = 1;
for (k = 1; k <= n – 1; k++)
{
for (c = 1; c <= space; c++)
printf(" ");
space++;
for (c = 1 ; c <= 2*(n-k)-1; c++)
printf("*");
printf("\n");
}
return 0;
}
c program for prime number
Prime number program in c: c program for prime number, this code prints prime numbers using c programming language. To check
whether a number is prime or not see another code below. Prime number logic: a number is prime if it is divisible only by one
and itself. Remember two is the only even and also the smallest prime number. First few prime numbers are 2, 3, 5, 7, 11, 13,
17….etc. Prime numbers have many applications in computer science and mathematics. A number greater than one can be
factorized into prime numbers, For example 540 = 22*33*51
Prime number program in c language
#include
main()
{
int n, i = 3, count, c;
printf(“Enter the number of prime numbers required\n”);
scanf(“%d”,&n);
if ( n >= 1 )
{
printf(“First %d prime numbers are :\n”,n);
printf(“2\n”);
}
for ( count = 2 ; count <= n ; )
{
for ( c = 2 ; c <= i – 1 ; c++ )
{
if ( i%c == 0 )
break;
}
if ( c == i )
{
printf("%d\n",i);
count++;
}
i++;
}
return 0;
}
There are many logic to check prime numbers, one given below is more efficient then above method.
for ( c = 2 ; c <= (int)sqrt(n) ; c++ )
//only checking from 2 to square root of number is sufficient.
There are many more efficient logic then written above.
C program for prime number or not
#include
main()
{
int n, c = 2;
printf(“Enter a number to check if it is prime\n”);
scanf(“%d”,&n);
for ( c = 2 ; c <= n – 1 ; c++ )
{
if ( n%c == 0 )
{
printf("%d is not prime.\n", n);
break;
}
}
if ( c == n )
printf("%d is prime.\n", n);
return 0;
}
C program for prime number using function
#include
int check_prime(int);
main()
{
int n, result;
printf(“Enter an integer to check whether it is prime or not.\n”);
scanf(“%d”,&n);
result = check_prime(n);
if ( result == 1 )
printf(“%d is prime.\n”, n);
else
printf(“%d is not prime.\n”, n);
return 0;
}
int check_prime(int a)
{
int c;
for ( c = 2 ; c <= a – 1 ; c++ )
{
if ( a%c == 0 )
return 0;
}
if ( c == a )
return 1;
}
armstrong number c program
armstrong number c program: c programming code to check whether a number is armstrong or not. A number is armstrong if the
sum of cubes of individual digits of a number is equal to the number itself. For example 371 is an armstrong number as 33 +
73 + 13 = 371. Some other armstrong numbers are: 0, 1, 153, 370, 407.
C programming code
#include
main()
{
int number, sum = 0, temp, remainder;
printf(“Enter a number\n”);
scanf(“%d”,&number);
temp = number;
while( temp != 0 )
{
remainder = temp%10;
sum = sum + remainder*remainder*remainder;
temp = temp/10;
}
if ( number == sum )
printf(“Entered number is an armstrong number.”);
else
printf(“Entered number is not an armstrong number.”);
return 0;
}
c program to generate and print armstrong numbers
armstrong number in c: This program prints armstrong number. In our program we ask the user to enter a number and then we use
a loop from one to the entered number and check if it is an armstrong number and if it is then the number is printed on the
screen. Remember a number is armstrong if the sum of cubes of individual digits of a number is equal to the number itself.
For example 371 is an armstrong number as 33 + 73 + 13 = 371. Some other armstrong numbers are 0, 1, 153, 370, 407.
C code
#include
#include
main()
{
int r;
long number = 0, c, sum = 0, temp;
printf(“Enter the maximum range upto which you want to find armstrong numbers “);
scanf(“%ld”,&number);
printf(“Following armstrong numbers are found from 1 to %ld\n”,number);
for( c = 1 ; c <= number ; c++ )
{
temp = c;
while( temp != 0 )
{
r = temp%10;
sum = sum + r*r*r;
temp = temp/10;
}
if ( c == sum )
printf("%ld\n", c);
sum = 0;
}
getch();
return 0;
}
Fibonacci series in c
Fibonacci series in c programming: c program for Fibonacci series without and with recursion. Using the code below you can
print as many number of terms of series as desired. Numbers of Fibonacci sequence are known as Fibonacci numbers. First few
numbers of series are 0, 1, 1, 2, 3, 5, 8 etc, Except first two terms in sequence every other term is the sum of two previous
terms, For example 8 = 3 + 5 (addition of 3, 5). This sequence has many applications in mathematics and Computer Science.
Fibonacci series in c using for loop
/* Fibonacci Series c language */
#include
main()
{
int n, first = 0, second = 1, next, c;
printf(“Enter the number of terms\n”);
scanf(“%d”,&n);
printf(“First %d terms of Fibonacci series are :-\n”,n);
for ( c = 0 ; c < n ; c++ )
{
if ( c <= 1 )
next = c;
else
{
next = first + second;
first = second;
second = next;
}
printf("%d\n",next);
}
return 0;
}
Output of program:
Fibonacci series program
Fibonacci series program in c using recursion
#include
int Fibonacci(int);
main()
{
int n, i = 0, c;
scanf(“%d”,&n);
printf(“Fibonacci series\n”);
for ( c = 1 ; c <= n ; c++ )
{
printf("%d\n", Fibonacci(i));
i++;
}
return 0;
}
int Fibonacci(int n)
{
if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else
return ( Fibonacci(n-1) + Fibonacci(n-2) );
}
linear search in c
Linear search in c programming: The following code implements linear search ( Searching algorithm ) which is used to find
whether a given number is present in an array and if it is present then at what location it occurs.It is also known as
sequential search. It is very simple and works as follows: We keep on comparing each element with the element to search until
the desired element is found or list ends. Linear search in c language for multiple occurrences and using function.
Linear search c program
#include
main()
{
int array[100], search, c, number;
printf(“Enter the number of elements in array\n”);
scanf(“%d”,&number);
printf(“Enter %d numbers\n”, number);
for ( c = 0 ; c < number ; c++ )
scanf("%d",&array[c]);
printf("Enter the number to search\n");
scanf("%d",&search);
for ( c = 0 ; c < number ; c++ )
{
if ( array[c] == search ) /* if required element found */
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if ( c == number )
printf("%d is not present in array.\n", search);
return 0;
}
C program for binary search
Output of code:
Linear Search program output
Linear search for multiple occurrences
In the code below we will print all the locations at which required element is found and also the number of times it occur in
the list.
#include
main()
{
int array[100], search, c, n, count = 0;
printf(“Enter the number of elements in array\n”);
scanf(“%d”,&n);
printf(“Enter %d numbers\n”, n);
for ( c = 0 ; c < n ; c++ )
scanf("%d",&array[c]);
printf("Enter the number to search\n");
scanf("%d",&search);
for ( c = 0 ; c < n ; c++ )
{
if ( array[c] == search )
{
printf("%d is present at location %d.\n", search, c+1);
count++;
}
}
if ( count == 0 )
printf("%d is not present in array.\n", search);
else
printf("%d is present %d times in array.\n", search, count);
return 0;
}
Output of code:
Linear Search program output for multiple occurence
C program for linear search using function
#include
int linear_search(int*, int, int);
main()
{
int array[100], search, c, n, position;
printf(“Enter the number of elements in array\n”);
scanf(“%d”,&n);
printf(“Enter %d numbers\n”, n);
for ( c = 0 ; c < n ; c++ )
scanf("%d",&array[c]);
printf("Enter the number to search\n");
scanf("%d",&search);
position = linear_search(array, n, search);
if ( position == -1 )
printf("%d is not present in array.\n", search);
else
printf("%d is present at location %d.\n", search, position+1);
return 0;
}
int linear_search(int *pointer, int n, int find)
{
int c;
for ( c = 0 ; c < n ; c++ )
{
if ( *(pointer+c) == find )
return c;
}
return -1;
}
C program for binary search
C program for binary search: This code implements binary search in c language. It can only be used for sorted arrays, but
it's fast as compared to linear search. If you wish to use binary search on an array which is not sorted then you must sort
it using some sorting technique say merge sort and then use binary search algorithm to find the desired element in the list.
If the element to be searched is found then its position is printed.
The code below assumes that the input numbers are in ascending order.
C programming code for binary search
#include
main()
{
int c, first, last, middle, n, search, array[100];
printf(“Enter number of elements\n”);
scanf(“%d”,&n);
printf(“Enter %d integers\n”, n);
for ( c = 0 ; c < n ; c++ )
scanf("%d",&array[c]);
printf("Enter value to find\n");
scanf("%d",&search);
first = 0;
last = n – 1;
middle = (first+last)/2;
while( first <= last )
{
if ( array[middle] last )
printf(“Not found! %d is not present in the list.\n”, search);
return 0;
}
c program for bubble sort
c program for bubble sort: c programming code for bubble sort to sort numbers or arrange them in ascending order. You can
easily modify it to print numbers in descending order.
Bubble sort algorithm in c
/* Bubble sort code */
#include
int main()
{
int array[100], n, c, d, swap;
printf(“Enter number of elements\n”);
scanf(“%d”, &n);
printf(“Enter %d integers\n”, n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0 ; c < ( n – 1 ); c++)
{
for (d = 0 ; d array[d+1]) /* For decreasing order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for ( c = 0 ; c < n ; c++ )
printf("%d\n", array[c]);
return 0;
}
Bubble sort in c language using function
#include
void bubble_sort(long [], long);
int main()
{
long array[100], n, c, d, swap;
printf(“Enter number of elements\n”);
scanf(“%ld”, &n);
printf(“Enter %ld longegers\n”, n);
for (c = 0; c < n; c++)
scanf("%ld", &array[c]);
bubble_sort(array, n);
printf("Sorted list in ascending order:\n");
for ( c = 0 ; c < n ; c++ )
printf("%ld\n", array[c]);
return 0;
}
void bubble_sort(long list[], long n)
{
long c, d, t;
for (c = 0 ; c < ( n – 1 ); c++)
{
for (d = 0 ; d list[d+1])
{
/* Swapping */
t = list[d];
list[d] = list[d+1];
list[d+1] = t;
}
}
}
}
Matrix multiplication in c
Matrix multiplication in c language: c program to multiply matrices (two dimensional array), this program multiplies two
matrices which will be entered by the user. Firstly user will enter the order of a matrix. If the entered orders of two
matrix is such that they can’t be multiplied then an error message is displayed on the screen. You have already studied the
logic to multiply them in Mathematics. Matrices are frequently used while doing programming and are used to represent graph
data structure, in solving system of linear equations and many more.
Matrix multiplication in c language
#include
int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
printf(“Enter the number of rows and columns of first matrix\n”);
scanf(“%d%d”, &m, &n);
printf(“Enter the elements of first matrix\n”);
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
scanf("%d", &first[c][d]);
printf("Enter the number of rows and columns of second matrix\n");
scanf("%d%d", &p, &q);
if ( n != p )
printf("Matrices with entered orders can't be multiplied with each other.\n");
else
{
printf("Enter the elements of second matrix\n");
for ( c = 0 ; c < p ; c++ )
for ( d = 0 ; d < q ; d++ )
scanf("%d", &second[c][d]);
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
{
for ( k = 0 ; k < p ; k++ )
{
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}
printf("Product of entered matrices:-\n");
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
printf("%d\t", multiply[c][d]);
printf("\n");
}
}
return 0;
}
String length
This program prints length of string, for example consider the string "c programming" it's length is 13. Null character is
not counted when calculating string length. To find string length we use strlen function of string.h.
C program to find string length
#include
#include
main()
{
char a[100];
int length;
printf(“Enter a string to calculate it’s length\n”);
gets(a);
length = strlen(a);
printf(“Length of entered string is = %d\n”,length);
return 0;
}
String length program executable.
Output of program:
string length
You can also find string length using pointer or without strlen function. Following program shows how to achieve this.
C program to find string length without strlen
C program to find length of a string using pointers.
#include
main()
{
char array[100], *pointer;
int length = 0;
printf(“Enter a string\n”);
gets(array);
pointer = array;
while(*(pointer+length))
length++;
printf(“Length of entered string = %d\n”,length);
return 0;
}
Function to find string length
int string_length(char *s)
{
int c = 0;
while(*(s+c))
c++;
return c;
}
c program to compare two strings
This c program compares two strings using strcmp, without strcmp and using pointers. For comparing strings without using
library function see another code below.
C program to compare two strings using strcmp
#include
#include
main()
{
char a[100], b[100];
printf(“Enter the first string\n”);
gets(a);
printf(“Enter the second string\n”);
gets(b);
if( strcmp(a,b) == 0 )
printf(“Entered strings are equal.\n”);
else
printf(“Entered strings are not equal.\n”);
return 0;
}
C program to compare two strings without using strcmp
Here we create our own function to compare strings.
int compare(char a[], char b[])
{
int c = 0;
while( a[c] == b[c] )
{
if( a[c] == ” || b[c] == ” )
break;
c++;
}
if( a[c] == ” && b[c] == ” )
return 0;
else
return -1;
}
C program to compare two strings using pointers
In this method we will make our own function to perform string comparison, we will use character pointers in our function to
manipulate string.
#include
int compare_string(char*, char*);
main()
{
char first[100], second[100], result;
printf(“Enter first string\n”);
gets(first);
printf(“Enter second string\n”);
gets(second);
result = compare_string(first, second);
if ( result == 0 )
printf(“Both strings are same.\n”);
else
printf(“Entered strings are not equal.\n”);
return 0;
}
int compare_string(char *first, char *second)
{
while(*first==*second)
{
if ( *first == ” || *second == ” )
break;
first++;
second++;
}
if( *first == ” && *second == ” )
return 0;
else
return -1;
}
c program to concatenate strings
This program concatenates strings, for example if the first string is “c ” and second string is “program” then on
concatenating these two strings we get the string “c program”. To concatenate two strings we use strcat function of string.h,
to concatenate without using library function see another code below which uses pointers.
C code
#include
#include
#include
main()
{
char a[100], b[100];
printf(“Enter the first string\n”);
gets(a);
printf(“Enter the second string\n”);
gets(b);
strcat(a,b);
printf(“String obtained on concatenation is %s\n”,a);
getch();
return 0;
}
string concatenation.
Output:
string concatenation
String concatenation without strcat
#include
void concatenate_string(char*, char*);
main()
{
char original[100], add[100];
printf(“Enter source string\n”);
gets(original);
printf(“Enter string to concatenate\n”);
gets(add);
concatenate_string(original, add);
printf(“String after concatenation is \”%s\”\n”, original);
return 0;
}
void concatenate_string(char *original, char *add)
{
while(*original)
original++;
while(*add)
{
*original = *add;
add++;
original++;
}
*original = ”;
}
C programming code using rand
#include
#include
int main() {
int c, n;
printf(“Ten random numbers in [1,100]\n”);
for (c = 1; c <= 10; c++) {
n = rand()%100 + 1;
printf("%d\n", n);
}
return 0;
}
C programming code using random function(Turbo C compiler only)
#include
#include
#include
main()
{
int n, max, num, c;
printf(“Enter the number of random numbers you want “);
scanf(“%d”,&n);
printf(“Enter the maximum value of random number “);
scanf(“%d”,&max);
printf(“%d random numbers from 0 to %d are :-\n”,n,max);
randomize();
for ( c = 1 ; c <= n ; c++ )
{
num = random(max);
printf("%d\n",num);
}
getch();
return 0;
}