Basic C Program

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

Basic c program

Hello World Program


• simple program printing Hello World in C language.

• printf() is a system defined function under the header file stdio.h,


used to print data onto the screen

• \n is used to move the control onto the next line

• \t is used to give a horizontal tab i.e. continuous five spaces


• include <stdio.h>

• int main()
•{
• printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
• int num;
• printf("\nHello world!\nWelcome to Studytonight: Best place to learn\n");
• printf("\n\n\t\t\tCoding is Fun !\n\n\n");
• return 0;
•}
• a program to explain how to take input from user for different datatypes available in C
language. The different datatypes are int(integer values), float(decimal values) and
char(character values).

• Here is the C language tutorial explaining various datatypes → Datatypes in C

• printf() is used to display text onto the screen

• & is used to assign the input value to the variable and store it at that particular location.

• scanf() is to take input from the user using format specifier discussed in upcoming tutorials

• %d and %i, both are used to take numbers as input from the user.

• %f is the format specifier to take float as input from the user

• %s is the format specifier to take character as input from the user


• #include<stdio.h>

• int main()
•{

• int num1, num2;


• float fraction;
• char character;

• printf("Enter two numbers number\n");



• // Taking integer as input from user
• scanf("%d%i", &num1, &num2);
• printf("\n\nThe two numbers You have entered are %d and %i\n\n", num1, num2);

• // Taking float or fraction as input from the user


• printf("\n\nEnter a Decimal number\n");
• scanf("%f", &fraction);
• printf("\n\nThe float or fraction that you have entered is %f", fraction);

• // Taking Character as input from the user


• printf("\n\nEnter a Character\n");
• scanf("%s",&character);
• printf("\n\nThe character that you have entered is %s", character);

• printf("\n\n\t\t\tCoding is Fun !\n\n\n");



• return 0;
• }
ASCII value of Character
• %c is the format specifier to take character as input
• #include<stdio.h>

• int main()
• {
• printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

• char c;
• printf("Enter a character : ");
• scanf("%c" , &c);
• printf("\n\nASCII value of %c = %d",c,c);

• printf("\n\n\t\t\tCoding is Fun !\n\n\n");


• return 0;
• }
How to use gets() function

• one of the important points about scanf() and gets() are:

• scanf() and gets() both are used to take input from the user.
• scanf() can only take input until it encounters a space. The words after
space are ignored by it.
• gets() is used to take a single input at a time but can be used to input a
complete sentence with spaces unlike scanf().
• Below is a program on use of gets().

• gets() takes only a single line at a time i.e all the words before hitting
\n(enter key).
• #include<stdio.h>

• int main()
• {
• printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

• char str[50]; // char array of size 50


• printf("Enter your complete name:\n\n\n");

• gets(str);
• printf("\n\nWelcome to Studytonight %s\n\n\n", str);
• printf("\n\n\t\t\tCoding is Fun !\n\n\n");
• return 0;
• }
Basic ifelse condition program
• #include<stdio.h>

• int main()
•{
• printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

• int number;
• printf("Please enter a number:\n");
• scanf("%d",&number);
• /*
• For single statements we can skip the curly brackets
• */
• if(number < 100)
• printf("Number is less than 100!\n");
• else if(number == 100)
• printf("Number is 100!\n");
• else
• printf("Number is greater than 100!\n");

• printf("\n\n\t\t\tCoding is Fun !\n\n\n");



• return 0;
• }
Switch Case with break
• #include<stdio.h>

• int main()
•{
• printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

• // Local Variable Definition


• char grade;
• printf("Enter your grade:\n");
• scanf("%c", &grade);
• switch(grade)
• {
• case 'A':
• printf("Excellent\n");
• break;
• case 'B':
• printf("Keep it up!\n\n");
• break;
• case 'C':
• printf("Well done\nbreak keyword takes execution to exit the switch case\n\n");
• break;
• case 'D':
• printf("You passed\n");
• break;
• case 'F':
• printf("Better luck next time\n");
• break;
• default:
• printf("Invalid grade\n");
• }
• printf("Your grade is %c\n",grade);
• printf("\n\n\t\t\tCoding is Fun !\n\n\n");
• return 0;
• }
Factorial
• #include<stdio.h>

• int main()
•{
• int n,c=1,f=1,a=1;

• printf("\n Enter The Number:");
• scanf("%d",&n);

• //LOOP TO CALCULATE FACTORIAL OF A NUMBER
• while(c<=n)
• {
• f=f*a;
• a= ++c;
• }

• printf("\n The Factorial of %d is %d",n,f);
• return 0;
• }

You might also like