#include <time.h>
#include <stdio.h>
int main(void)
{
time_t lt;
lt = time(NULL);
printf(ctime(<));
return 0;
}
By Dinesh Thakur
#include <time.h>
#include <stdio.h>
int main(void)
{
time_t lt;
lt = time(NULL);
printf(ctime(<));
return 0;
}
By Dinesh Thakur
This program user ask to define the getch() function for receiving user selection about character that press on key board. User declares the variables that use to contain the value. In printing criteria user put a query about “do u want to continue (y/n)”. Here if user press ‘Y’ then the character pressed will be selection word as output else the condition will terminate.
Problem Statement:
This is C program user need to define the selection of user in getch() function.
Here is C source code to define the selection of user in getch() function. The output of this program shown below.
#include <conio.h>
#include <stdio.h>
int main(void) {
char ch;
printf("Do you wish to continue? (Y/N : )");
ch = getche();
if(ch=='Y') {
/* continue with something */
}
return 0;
}
By Dinesh Thakur
The function getchar () read and write (display) single character. The function getchar () reads and returns the next character from standard input stream. The standard input is from keyboard. If the function encounters end-of-file character, it returns EOF. Also, if an error occurs in reading, then also the function returns EOF. The prototype of the function is
int getchar (void); /*function type is int and no arguments (void).*/
It may be used as shown below.
char ch ;
ch= getchar();
The function reads single character at a time, however, it may be used to read a string with the help of a loop, i.e., read the string character by character. Since the function has access to single character it is useful in manipulating the characters of a string, i.e., altering a character in a string or for counting all characters or for counting the occurrence of particular character in a string. Illustrates use of getchar ().
#include <stdio.h>
int main(void)
{
int a, b;
char ch;
printf("Choice:\n");
printf("Add, Subtract, Multiply, or Divide?\n");
printf("Enter first letter: ");
ch = getchar();
printf("\n");
printf("Enter a: ");
scanf("%d", &a);
printf("Enter b: ");
scanf("%d", &b);
if(ch=='A') printf("%d", a+b);
if(ch=='S') printf("%d", a-b);
if(ch=='M') printf("%d", a*b);
if(ch=='D' && b!=0) printf("%d", a/b);
return 0;
}
By Dinesh Thakur
Program illustrates the application of operator modulus(%) to random numbers generated in order to limit the maximum size of random numbers. This is helpful in sampling problems as well as in playing of dice. In dice play, the maximum value that can be obtained is 6. The code for such a case is illustrated below.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int number_to_guess = rand() % 100 + 1;
clrscr();
printf("Random Number Between 1 and 100 is :%d ",number_to_guess);
return 0;
}
By Dinesh Thakur
This is C program that asks user to find out the square root of a number. for this operation user declare a variable for fetching the output on it user also declare a math.h header file this file contains all the math functions for required operation. Then user asks a number to find out the square root put on the square root function the last move to fetches out the result as output on the screen.
Problem Statement:
This is a C program to find out the square root of a number.
Here is C source code for finding out the square root. Output of this program shown below.
#include <stdio.h>
#include <math.h> /* needed by sqrt() */
int main(void)
{
double answer;
clrscr();
answer = sqrt(20.0);
printf("Square Root of a Give Value : %f", answer);
getch();
return 0;
}
By Dinesh Thakur
This is C program where user asks to calculate the exponential value of Number. Exponential is known to a variables raised to the power of something. For example, 10 to the power of 2 is 100 (10×10). For this process user declare the required variables for this after assign the value to variable. User put out the method to find out exponential value. And fetch out the result as output on display.
Problem Statement:
This is C program that asks user to calculate the exponential power of a Number.
Here is C source code for Calculate the exponential value for number. Output of this program shown below.
#include <math.h>
int main ()
{
double p, result;
clrscr();
p = 5;
result = exp (p);
printf ("Exponential of %lf = %lf\n", p, result );
getch();
return 0;
}
By Dinesh Thakur
In this program user ask to calculate the Absolute value of integer Parameter. User declares some required variables for the storing value in it. Here user use math function for calculate the absolute value. In abs function the negative value is been denied. Then user uses the absolute function for displaying the result on the screen.
Problem Statement:
This is C program where user asks to calculate the Absolute value for the integer parameter.
Here is C source code for calculate the absolute value for integer parameter. Output of this program shown below.
#include <stdio.h>
#include <math.h>
int main ()
{
int n, m;
clrscr();
n = abs( 3 );
m = abs( -1 );
printf (" n = %d \n", n);
printf (" m = %d\n", m);
getch();
return 0;
}
Output :
n = 3
m = 1
By Dinesh Thakur
In this program user ask to find out the Power of numeric value as cubic, trigonometric etc. to print the table of powers user need to declare all the power functions. In first instance user declare math.h header file this file contains all the power () functions. User declares integer type and float type variables for process to print the power tables. User asks to enter the value for printing the power table. After declare the value user put the method to find the powers of value like cubic, quadric etc. user in the end print the value through the printing method.
Problem Statement:
This Is C program that asks user to print a table for Power functions.
Here is C source code for printing the table of powers. Output of this program shown below.
#include <stdio.h>
/* this header file includes the pow() function */
#include <math.h>
int main()
{
int num;
float rn;
clrscr();
printf("Type in a Number : ");
scanf ("%d",&num);
printf("\nHere is a table for the square\n");
printf("cube, quartic and quintic for %d to %d:\n\n", num, num+4);
printf("Integer\tSquare\tCube\tQuartic\tQuintic\n"); /* if read in as int must be converted to float for pow() */
rn=num;
/* each specifier includes the same width as header, no decimal places with following tab*/
printf("%7.0f\t%6.0f\t%4.0f\t%7.0f\t%7.0f\n", rn,pow(rn,2),pow(rn,3),pow(rn,4),pow(rn,5)); rn++;
printf("%7.0f\t%6.0f\t%4.0f\t%7.0f\t%7.0f\n", rn,pow(rn,2),pow(rn,3),pow(rn,4),pow(rn,5)); rn++;
printf("%7.0f\t%6.0f\t%4.0f\t%7.0f\t%7.0f\n", rn,pow(rn,2),pow(rn,3),pow(rn,4),pow(rn,5)); rn++;
printf("%7.0f\t%6.0f\t%4.0f\t%7.0f\t%7.0f\n", rn,pow(rn,2),pow(rn,3),pow(rn,4),pow(rn,5)); rn++;
printf("%7.0f\t%6.0f\t%4.0f\t%7.0f\t%7.0f\n", rn,pow(rn,2),pow(rn,3),pow(rn,4),pow(rn,5));
getch();
return 0;
}
By Dinesh Thakur
#include <stdio.h>
void main()
{
double PI = 3.141595358979;
float A= 7.5;
int K = 456;
clrscr();
printf("Display some values in separate lines.\n");
printf("PI = %lf\nA = %f\n", PI,A);
printf("K (in decimal) = %d\n", K);
printf("K (in octal) = %0\n", K);
printf("K (in hexadecimal) = %x\n", K);
printf("K (in hexadecimal cap. letters) = %X\n", K);
printf("Display some values in same line.\n");
printf("PI = %G,\t A = %%%f,\t K = %d\n", PI, A, K);
}
The expected output is as given below.
Explanation of the output: The second and third lines of the output are due to the print statement.
printf(“PI = %lf\n A = %f\n”, PI,A);
Here, “PI = ” is a text segment which is displayed as it is written in the above statement. The value of PI is displayed where % l f appears. Then comes “\ n” which takes the cursor to the next line; so, “A= ” is displayed in the next line. After this, the value of A is displayed where %f appears. Again we have used”\n” so that other outputs are shifted to the next line. The fourth line of the output is the usual output statement for an integer. The statement between parentheses “in decimal” indicates number with base 10. Value of Kin decimal system is 456 as declared in the program. The next two lines of the output display the same value 456 converted into octal (base 8) and hexadecimal (base 16) system. This is achieved by simply changing the conversion character after% sign. For octal, the sign used is “o”, while for hexadecimal “x” may be used if character digits of hexadecimal system are desired in lowercase and “X” for the character digits to be displayed in uppercase (capital letters). The last line of the output displays several values in the same line separated by spaces and commas. For displaying the percent sign% with 7.5, we have to put an extra symbol”%%” in the formatting string, i.e., the code becomes “A= %%%f”. becomes “A= %%%f”.
By Dinesh Thakur
#include <stdio.h>
int a,b,num;
main()
{
clrscr();
printf("\nEnter number:");
scanf("%d",&num);
a=num/10;
b=num-a*10;
printf("\n\nThe reverse is %d%d\n",b,a);
getch();
}
By Dinesh Thakur
main()
{
int number=5;
printf("---%*d----\n", 6, number);
}
By Dinesh Thakur
Computer instructions have the provision of directly adding or subtracting unity to a variable. So, if we use this facility, the operation is quicker than the operation for the statement A = A+ 1; . Therefore, for increasing and decreasing the values of integral objects by unity, it is better to make use of increment operator (++) and decrement operator (- -) , respectively. Another provision with these operators is that the operators may be placed before or after the identifier of the variable. When the operators + + and — are placed before the variable name, these are called pre-increment and pre-decrement operators, respectively. For instance,
Int A= 10, B = 5;
++A* ++B; // This is equivalent to(10+ 1) * (5+ 1) and A= 11,B=6.
When the operators are placed after the names they are called post-increment and post-decrement operators. In this case, the increment is carried out after the current use. For example,
A++* B++; // This is equivalent to 10 x 5 and A = 11 and B = 6.
In case of pre-increment/pre-decrement operators the present value of the variable is first increased/ decreased by unity and then this changed value is used in the application. On the other hand, in case of post-increment/post-decrement operators, the present value of the variable is used in the application and then its value is incremented/ decremented. Table provides a list of all possible increment and decrement operators along with their applications.
Illustrates application of increment and decrement operators.
#include <stdio.h>
void main()
{
int x = 3, y = 5,z = 10,p= 8, A,B,C,D;
clrscr();
A= ++x*++y; // ++has higher precedence level than *
printf("A = %d\t x = %d\t y = %d\n", A, x, y);
B = y--* y-- ;
printf("B = %d \t y = %d \n", B, y);
C = ++z*--z ; //++ and -- have higher precedence level than*
printf("C = %d\t z = %d\n", C, z );
D = p-- * --p; //--p has higher precedence level than *
printf("D= %d\t p= %d\n", D, p);
}
For the first line of the output both x and y are incremented before the multiplication, so, in the result, A is equal to 24, x = 4, y = 6. For the next line of the output, the value y is now 6, so, B = 36; decrement (twice) is done after the multiplication. For the third line of output, Z is first incremented and then decremented before the multiplication because++ and — have higher precedence level than *. So, the result is C = 100 and Z = 10. For the fourth line of the output, the decrement operation –p is carried out before the multiplication. The value of p is again decremented by 1 after the multiplication because of p–. So, D = 49 and p = 6.
By Dinesh Thakur
In this program user ask to define the left shift operator. Left Shift Operator The right operands value is moved left by the number of bits specified by the right operand. User declares required variables for storing the value. User put a condition to shift the bits from Right to left. Display result on the screen.
Problem Statement:
This is C Program that ask user to define a left shift operator with use of Logic.
Here is C source code for define the left shift bit wise operator. Output of this program shown below.
void main()
{
unsigned int Value=4; /* 4 = 0000 0100 */
unsigned int Shift=2;
Value = Value << Shift; /* 16 = 0001 0000 */
Value <<= Shift; /* 64 = 0100 0000 */
printf("Shifting Bits Left = %d\n", Value); /* Prints 64 */
}
By Dinesh Thakur
This is C program that asks user to return the character in Lower Case. User just declares a character and put char method to evaluate the condition. Display result on the screen as output.
Problem Statement
This is C program that ask user to return the lower case equivalent.
Here is C source code to find lower case equivalent. The output of this program shown below.
#include <ctype.h>
#include <stdio.h>
main()
{
putchar(tolower('Q'));
}
By Dinesh Thakur
This is C program that asks user to return the character in Upper Case. User just declares a character and put char method to evaluate the condition. Display result on the screen as output.
Problem Statement
This is C program that ask user to return the uppercase equivalent.
Here is C source code to find uppercase equivalent. The output of this program shown below.
#include <ctype.h>
#include <stdio.h>
int main(void)
{
putchar(toupper('a'));
}
By Dinesh Thakur
#include<stdio.h>
main()
{
char a,b,c,d,e,f;
a='D';
b='I';
c='N';
d='E';
e='S';
f=’H’;
clrscr();
printf("my name is =%c%c%c%c%c%c",a,b,c,d,e,f);
getch();
}
By Dinesh Thakur
#include <stdio.h>
main( ) /* read and w r i t e a l i n e o f t e x t */
{
char l i n e [ 8 0 ] ;
scanf(" % [ ^ \ n ] " , l i n e ) ;
p r i n t f ( "%s", l i n e ) ;
}
By Dinesh Thakur
In this program user ask to define the ternary operator. User declares some required variables that contain values in it. Ternary operator denote by ?: this can be used as a shortcut for an if-else statement. Here user declare a condition to evaluate the method user ask to enter the value. b= (a>5?1:0); and display result on the screen as output.
Problem Statement:
This is C program that ask user to define the ternary operator.
Here is C source code for using ternary operator. The output of this program shown below.
#include<stdio.h>
void main()
{
int a,b;
clrscr();
printf("enter any value ");
scanf("%d",&a);
b=(a>5?1:0);
printf("%d",b);
getch();
}
By Dinesh Thakur
String copy is one of the basic operations in manipulating strings. In this operation, all the characters in the source string, including the null terminator, are copied to the target string making it an exact replica of the source string.
As we already know, we cannot copy a string using an assignment operator, as in str2 = strl. Instead, the C standard library provides the strcpy and strncpy functions to copy a string. The strcpy function copies the entire string, whereas the strncpy function copies at most the first n characters. A typical call to the strcpy function takes the following form:
strcpy (dest , src) ;
This function copies string src to dest including the null terminator and it returns a pointer to dest, which is often ignored, as in the above call. As mentioned before, the array dest must have enough character positions to accommodate string src. Observe that the order of arguments dest and src follows the conventions of assignment operation in which the variable being assigned is on the left hand side.
Example of Copy String
The code given below performs string copy using the strcpy function.
char str1[20] =”Orange”;
char str2[20] = “Pineapple”;
strcpy(str2, str1);
printf(“strl: %s str2: %s\n”, strl, str2);
The character arrays strl and str2 can store up to 20 characters and are initialized with strings “Orange” and “Pineapple”, respectively. The strcpy function copies entire string strl to str2, overwriting its contents. Seven characters are copied in this operation, including the null · terminator. Thus, the output of this code is as follows:
str1: Orange str2: Orange
#include <string.h>
#include <stdio.h>
int main()
{
char first[20],last[20],full_name[20];
clrscr();
strcpy(first, "Dinesh"); /* Initialize first name */
strcpy(last, "Thakur"); /* Initialize last name */
strcpy(full_name, first); /* full = "Dinesh" */
/* Note: strcat not strcpy */
strcat(full_name, " "); /* full = "Dinesh " */
strcat(full_name, last); /* full = "Dinesh Thakur" */
printf("The full name is %s\n", full_name);
return (0);
}
By Dinesh Thakur
This is c program that user has to show how to enter the content through string copy function. User declare array type variable so that use to contain the data in it. Here user declares the strcpy function to initialize the name as content. In the last user display the print method to show the output in result.
Problem statement:
This is C program to define the String copy function.
Here is C source code to enter through string function. The output of this program shown below.
#include <string.h>
#include <stdio.h>
char name[30]; /* First name of someone */
int main()
{
strcpy(name, "Sam"); /* Initialize the name */
printf("The name is %s\n", name);
return (0);
}
By Dinesh Thakur
This is C program here user will find out the length of line (string) for this user declare the variables that contain value for it and string functions for string execution. Declaring a variable with array parameter user ask to enter the line as required in quoted area. After that user put the method about string functions along with it display the result as output as result.
Problem Statement:
This is C program that ask user to scan through fgets.
Here is C source code to scan through fgets. The output of this program shown below.
#include <string.h>
#include <stdio.h>
char line[100]; /* Line we are looking at */
int main()
{
printf("Enter a line: ");
fgets(line, sizeof(line), stdin);
printf("The length of the line is: %d\n", strlen(line));
return (0);
}
By Dinesh Thakur
By Dinesh Thakur
#include <stdio.h>
int main(void)
{
int a;
printf("Type in any integer: ");
scanf("%d",&a);
a-=a++;
printf("\na-=a++ => %d",a);
return 0;
}
By Dinesh Thakur
# include <stdio.h>
int main ()
{
int i=2;
printf("%d\n", 8*i/3);
/* Note: Integer arithmetic performed on 16/3 => 5 */
printf("%d\n", 8.0*i);
/* Error 1: Float value can't be printed with %d (int) specifier,prints 0 instead of 16.000*/
printf("%f\n",8*i);
/* Error 2: Float specifier can't print int value, prints 0.000 instead of 16*/
printf("%f\n",8*i/3);
/* Error 3: Again wrong specifier, Prints 0.000 instead of 5.000 */
printf("%d\n",8*i%3);
/* Note: Calculates 16mod3, ie the remainder is 1 */
printf("%f\n",8.0*i/3);
/* Correctly prints 5.333 */
return 0;
}
By Dinesh Thakur
By Dinesh Thakur
This is C program that asks user to find the area of a triangle using math’s function. Here user declares some variables that use to store the value of it. User asks to enter the value of triangle three sides of triangle. Here after value user using the method to find the area of triangle. And then print the result on the screen.
Problem statement:
This is C program that asks user to find out the area of triangle.
Method of Find area of Triangle:
” Area=sqrt(s*(s-a)*(s-b)*(s-c)); “
Here is C source code for finding the area of triangle. The output of this program shown below.
# include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,c;
float area, s;
clrscr();
printf("Enter the Three Sides of Triangle : ");
scanf("%d %d %d" ,&a, &b, &c);
s=(a+b+c)/2.0;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("Area of the Triangle= %f ", area);
getch();
}