Skip to main content

Posts

Showing posts from 2018

Largest of three numbers

#include <stdio.h> int main() { int a, b, c, largest; printf ("Enter three numbers : "); scanf ("%d%d%d", &a, &b, &c); largest = a; // Let 'a' be the largest number if(b > largest) largest = b; if(c > largest) largest = c; printf ("\n%d is the largest number.", largest); // To find the smallest number, replace '>' with '<' return 0; } Link : https://repl.it/@nithin_gudla/largest3numbers Enter three numbers : 52 98 64 98 is the largest number.

Compare two numbers

#include <stdio.h> int main() {    int a,b;    printf("Enter two numbers : ");    scanf("%d%d", &a, &b);    if(a > b)       printf("\n%d is greater than %d.", a, b);    else       printf("\n%d is greater than %d.", b, a);    return 0; } Enter two numbers : 5 7 7 is greater than 5.

Leap year or not

#include <stdio.h> int main() { int year; printf("Enter year : "); scanf("%d", &year); if (((year % 4 == 0) && (year % 100!= 0)) || (year % 400 == 0)) printf("\n%d is a leap year", year); else printf("\n%d is not a leap year", year); return 0; } Link :  https://repl.it/@nithin_gudla/leapyear Enter year : 2020 2020 is a leap year.

Whether a character is Vowel or Consonant

#include <stdio.h> int main() {     char ch;     printf("Enter an alphabet\t:\t");     scanf("%c", &ch);     if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'||        ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')         printf("\n'%c' is a vowel", ch);     else         printf("\n'%c' is a consonant", ch);     return 0; } Enter an alphabet : U 'U' is a vowel

Swapping (Exchanging) of two numbers without using a third variable

#include <stdio.h> int main() {   int a,b;   printf("Enter two numbers ");   scanf("%d%d", &a, &b );   printf("\nBefore swapping, a = %d and b = %d", a, b);   a = a + b;   b = a - b;   a = a - b;   printf("\nAfter swapping, a=%d and b=%d", a, b);   return 0; } Enter two numbers 7 9 Before swapping, a = 7 and b = 9 After swapping, a=9 and b=7

Swapping (Exchanging) of two numbers using third variable

#include <stdio.h> int main() {   int a,b,c;   printf("Enter two numbers ");   scanf("%d%d", &a, &b );   printf("\nBefore swapping, a = %d and b = %d", a, b);   c = a;   a = b;   b = c;   printf("\nAfter swapping, a=%d and b=%d", a, b);   return 0; } Enter two numbers 3 4 Before swapping, a = 3 and b = 4 After swapping, a=4 and b=3

Given number is +ve or -ve

#include <stdio.h> int main() {   int n;   printf("Enter a number\t:\t");   scanf("%d", &n );   if(n == 0)     printf("\n%d is a neutral number", n);   else   if(n > 0)     printf("\n%d is a positive number", n);   else     printf("\n%d is a negative number", n);   return 0; } Enter a number : -6 -6 is a negative number

Area and Perimeter of a circle

#include <stdio.h> #define pi 3.14   // defining pi=3.14 int main() {   float radius,area,perimeter;   printf("Radius\t:\t");   scanf("%f", &radius);   area = pi*radius*radius;   perimeter = 2*pi*radius;   printf("\nArea\t\t:\t%.2f", area);   printf("\nPerimeter\t:\t%.2f", perimeter);   return 0; } Radius : 5 Area : 78.50 Perimeter : 31.40

Area and Perimeter of a Rectangle

#include <stdio.h> int main() {   float length, breadth, area, perimeter ;   printf("Length\t:\t");   scanf("%f", &length );   printf("Breadth\t:\t");   scanf("%f", &breadth );   area=length*breadth;   perimeter=2*( length + breadth );   printf("\nArea\t:\t%f", area);   printf("\nPerimeter\t:\t%f", perimeter);   return 0; } Length : 5.64 Breadth : 4.86 Area : 27.41 Perimeter : 21.00

Division of two numbers

#include <stdio.h> int main() {   int a,b,rem,quo;   float finalresult;   printf("Enter two numbers\t:\t");   scanf("%d%d", &a , &b );   quo=a / b;   rem=a % b;   finalresult=(float) a / b;   printf("\nQuotient\t:\t%d", quo);   printf("\nRemainder\t:\t%d", rem);   printf("\nFinal Result\t:\t%f", finalresult);   return 0; } Enter two numbers : 24 9 Quotient : 2 Remainder : 6 Final Result : 2.666667

Absolute difference between two numbers

#include <stdio.h> #include<stdlib.h>  // since we are using abs() function int main() {   int a,b,c;   printf("Enter two numbers\t:\t");   scanf("%d%d", &a , &b );   c= abs (a - b);   printf("The absolute difference between %d and %d is %d", a, b, c);   return 0; } Enter two numbers : 5 6 The average of 5 and 6 is 1

Average of two numbers

#include <stdio.h> int main() {   int a, b;   float avg;   printf("Enter two numbers\t:\t");   scanf("%d%d", &a , &b );   avg = (float) (a + b) / 2;   printf("The average of %d and %d is %f", a, b, avg);   return 0; } Enter two numbers : 5 6 The average of 5 and 6 is 5.500000

'int main()' and 'void main()'

In C programming both the 'int main()' and 'void main()' are accepted. 'void main()' means that the main function doesn't return anything. void main() {     // statements   } 'int main()' says that the main function will return an integer to the operating system. For that we will return an integer to the operating system. int main() {     // statements     return 0; // since 0 is an integer } Some compilers are designed in such a way that they can't accept main function without returning anything, for those compilers we have to use 'int main()'.

Read input from the keyboard

To read input from the keyboard, we have to use a pre-defined function "scanf()". The syntax for "scanf()" function is scanf("format string", arguments list); A format string can be : %d (integer), %s (string) , %f (floating point number), %c (character) , %lf etc. Ex : scanf("%d", &input); // for single input       Here '%d' represents an integer and 'input' is the name assigned to that integer. scanf("%d%c", &input1 , &input2 ); // for multiple inputs To use 'scanf()' function also, we have to include a header file "stdio.h", i,e #include<stdio.h> or #include"stdio.h"

How to display text on monitor

We can display the text on monitor/ screen using a standard library function "printf()". The syntax for the "printf()" statement is printf(" "); printf(" Hi "); The above statement displays "Hi' on monitor. printf("format string", arguments list ); A format string can be  : %d (integer), %s (string) , %f (floating point number), %c (character) , %lf etc. Ex : printf ( " %d " , output); // for single output     Here '%d' represents an integer and 'output' is the name assigned to that integer. printf("%d%c", output1 , output2 ); // for multiple outputs To use printf statement in our program, we have to include a header file "stdio.h" i.e   #include<stdio.h> or #include"stdio.h"