Guess the number game Program in C:
Quick links:
In these program we used two extra header files one is <stdlib.h> and second is <time.h>. <stdlib.h> header file used because,
Quick links:
In these program we used two extra header files one is <stdlib.h> and second is <time.h>. <stdlib.h> header file used because,
In these program we used two new header files one is <stdlib.h> and second is <time.h>. <stdlib.h> header file used because
The C programming language supports many library functions which includes a number of input/output functions. These functions are used to transfer the information between the computer and device(user).
scanf() function is used to take input from user(keyboard).
printf() function is used to display output on the screen.
#include<stdio.h>
void main()
{
int num;// variable definition
/*Display message and ask user to enter some number... using printf()*/
printf("Enter the number:");
/*Read the number entered by user... using scanf()*/
scanf("%d",&num);
/*Display the number with message...using printf() */
printf("You entered number is: %d",num);
}
In above program %d is used to scan the intiger value.
If value is char then we use %c.
If value is float then we use %f and so on...
Above program shows the following output:
getchar() function reads a single character from the terminal and returns it as an intiger.
putchar() function is used to display these single character.
Example program:
#include<stdio.h>
void main()
{
int a;
printf("Enter a character:");
/*Here, getchar function is used to store the accepted character in variable a*/
a = getchar();
/*Display the character stored in variable a using putchar function*/
putchar(a);
}
Above program shows the following output:
In simple words:
gets() function is used to accept string from user.
puts() function is used to display the string.
#include<stdio.h>
void main()
{
//Declare character array to store string
char str[100];
printf("Enter a string: ");
/*store string in array str*/
gets(str);
/*Display string on the output screen*/
puts(str);
}
Above program shows the following output:
In this tutorial you will find lot of pattern programming exercises and examples. Pattern programming exercises manly used to enhance the logic of programmer, by solving this programs you can easily increase your logical thinking ability or also it will increase your loops and if-else statements knowledge.
Thank you for visiting us....... keep visiting. Learn our more C programming exercises with their solutions.....😊
In this tutorial you will learn number pattern programs with there solutions. You can practice it to enhance your programming logic. I hope this exercises will help you to understand working of looping statements, if-else statements and more.
Quick links:
Ex: Write a C program to print number heart pattern, which shows the steps to print pattern. How to write a C program to print number heart pattern, which shows steps to print pattern . C program to print number heart pattern which shows steps to print pattern.
Input from user:
Enter the number: 5
Expected output:
11 22
1111 2222
333333333
3333333
33333
333
3
This pattern is same as previous exercises star heart pattern.
Only difference is:
In this pattern we only need to replace the * with numbers 1, 2 and 3.
This number's also shows the steps to print this pattern.
#include<stdio.h>
void main()
{
int no,z;
int i,j,k;
printf("Enter the number:\n");
scanf("%d",&no);
/*To print upper part*/
z=no*2;
for(i=no/2;i<=no;i=i+2)
{
/*To print left peak */
for(j=1;j<no-i;j=j+2)
{
printf(" ");
}
for(k=1;k<=i;k++)
{
printf("1");
}
/*To print right peak*/
for(j=1;j<=no-i;j++)
{
printf(" ");
}
for(k=1;k<=i;k++)
{
printf("2");
}
printf("\n");
}
/*To print lower part means inverted triangle*/
for(i=1;i<=no;i++)
{
for(j=1;j<i;j++)
{
printf(" ");
}
for(k=1;k<z;k++)
{
printf("3");
}
z-=2;
printf("\n");
}
}
Above program shows the following output:
Input from user:
Enter the number: 5
Expected output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
For better understanding i have devided this code in two parts.
To print upper half part:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1. Accept input number from user.
2. Run outer for loop from 1 to no which shows rows in pattern:
for(i=1;i<=no;i++)
3. Run inner for loop from i to no-1 to print spaces:
for(j=i;j<no;j++)
{
printf(" ");
}
4. Run another inner for loop from 1 to i which shows columns in pattern:
for(k=1;k<=i;k++)
5. Inside the second inner for loop print value of k with one space:
printf("%d ",k);
6. After that outside the block of inner for loop print next line \n for print new row.
1 2 3 4
1 2 3
1 2
1
1. Run outer loop from 1 to no-1 which shows rows in pattern:
for(i=1;i<no;i++)
2. Run inner for loop from 1 to i to print spaces:
for(j=1;j<=i;j++)
{
printf(" ");
}
3. Run another one inner for loop from 1 to no-i and inside the loop print value of k :
for(k=1;k<=no-i;k++)
{
printf("%d ",k);
}
4. Last print \n for new row.
#include<stdio.h>
void main()
{
int no,i,j,k;
printf("Enter the number:");
scanf("%d",&no);
/*To print upper part*/
for(i=1;i<=no;i++)
{
for(j=i;j<no;j++)
{
printf(" ");
}
for(k=1;k<=i;k++)
{
printf("%d ",k);
}
/*For next row*/ printf("\n");
}
/*To print lower part*/
for(i=1;i<no;i++)
{
for(j=1;j<=i;j++)
{
printf(" ");
}
for(k=1;k<=no-i;k++)
{
printf("%d ",k);
}
/*For next row*/
printf("\n");
}
}
Above program shows the following output:
Input from user:
Enter the number: 5
Expected output:
1
123
12345
1234567
123456789
Input from user:
Enter the number:
5
Expected output:
12345
1234
123
12
1
1. Accept input number from user declare variable say no.
2. Run outer for loop from 1 to no which shows rows in pattern:
for(i=1;i<=no;i++)
3. Run inner for loop from 1 to (no+1)-i which shows columns in pattern:
for(j=1;j<=(no+1)-i;j++)
4. Inside the inner loop print value of j :
printf("%d",j);
5. For next row print \n.
#include<stdio.h>
void main()
{
int i,j,no;
printf("Enter the number:\n");
scanf("%d",&no);
/*Run outer loop from 1 to no*/
for(i=1;i<=no;i++)
{
/*Run inner loop from 1 to (no+1)-i*/
for(j=1;j<=(no+1)-i;j++)
{
/*Print value of j*/ printf("%d",j);
}
/*For next row*/ printf("\n");
}
}
Above program shows the following output: