Professional Documents
Culture Documents
Basic C Program
Basic C Program
Basic C Program
• 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).
• & 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.
• int main()
•{
• 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);
• 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");
• 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");
• int main()
•{
• printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
• 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;
• }