Tuesday, January 24, 2017
Monday, January 23, 2017
Program to Print an Integer
Program to Print an Integer
#include <stdio.h>
int main()
{
int number;
// printf() dislpays the formatted output
printf("Enter an integer: ");
// scanf() reads the formatted input and stores them
scanf("%d", &number);
// printf() displays the formatted output
printf("You entered: %d", number);
return 0;
}
OutputEnter a integer: 25 You entered: 25 In this program, an integer variable number is declared.
The
printf() function displays Enter an integer: on the screen. Then, the scanf() function reads an integer data from the user and stores in variable number.Finally, the value stored in the variable number is displayed on the screen using
printf() function.
C "Hello, World!" Program
Program to Display "Hello, World!"
#include <stdio.h>
int main()
{
// printf() displays the string inside quotation
printf("Hello, World!");
return 0;
}
OutputHello, World! How "Hello, World!" program works?
- The
#include <stdio.h>is a preprocessor command. This command tells compiler to include the contents ofstdio.h(standard input and output) file in the program.
Thestdio.hfile contains functions such asscanf()andprint()to take input and display output respectively.
If you useprintf()function without writing#include <stdio.h>, the program will not be compiled. - The execution of a C program starts from the
main()function. - The
printf()is a library function to send formatted output to the screen. In this program, theprintf()displays Hello, World! text on the screen. - The
return 0;statement is the "Exit status" of the program. In simple terms, program ends with this statement.
Sunday, January 22, 2017
Tuesday, January 17, 2017
Tuesday, January 10, 2017
Print Floyd's Triangle.
1 2 3 4 5 6 7 8 9 10
#include <stdio.h>
int main()
{
int rows, i, j, number= 1;
printf("Enter the number of rows: ");
scanf("%d",&rows);
for(i=1; i <= rows; i++)
{
for(j=1; j <= i; ++j)
{
printf("%d ", number);
++number;
}
printf("\n");
}
return 0;
}
Print Pascal's triangle
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
#include <stdio.h>
int main()
{
int rows, coef = 1, space, i, j;
printf("Enter number of rows: ");
scanf("%d",&rows);
for(i=0; i<rows; i++)
{
for(space=1; space <= rows-i; space++)
printf(" ");
for(j=0; j <= i; j++)
{
if (j==0 || i==0)
coef = 1;
else
coef = coef*(i-j+1)/j;
printf("%4d", coef);
}
printf("\n");
}
return 0;
}
Inverted half pyramid using numbers
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
#include <stdio.h>
int main()
{
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d",&rows);
for(i=rows; i>=1; --i)
{
for(j=1; j<=i; ++j)
{
printf("%d ",j);
}
printf("\n");
}
return 0;
}
Inverted half pyramid using *
* * * * *
* * * *
* * *
* *
*
#include <stdio.h>
int main()
{
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d",&rows);
for(i=rows; i>=1; --i)
{
for(j=1; j<=i; ++j)
{
printf("* ");
}
printf("\n");
}
return 0;
}
Program to print half pyramid using alphabets
A
B B
C C C
D D D D
E E E E E
#include <stdio.h>
int main()
{
int i, j;
char input, alphabet = 'A';
printf("Enter the uppercase character you want to print in last row: ");
scanf("%c",&input);
for(i=1; i <= (input-'A'+1); ++i)
{
for(j=1;j<=i;++j)
{
printf("%c", alphabet);
}
++alphabet;
printf("\n");
}
return 0;
}
Program to print half pyramid a using numbers
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
#include <stdio.h>
int main()
{
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d",&rows);
for(i=1; i<=rows; ++i)
{
for(j=1; j<=i; ++j)
{
printf("%d ",j);
}
printf("\n");
}
return 0;
}
Program to print half pyramid using *
*
* *
* * *
* * * *
* * * * *
#include <stdio.h>
int main()
{
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d",&rows);
for(i=1; i<=rows; ++i)
{
for(j=1; j<=i; ++j)
{
printf("* ");
}
printf("\n");
}
return 0;
}
Tuesday, January 3, 2017
Subscribe to:
Comments (Atom)