Posts

Showing posts with the label C tutorials

Program for accepting values in array and sort them in ascending order.

Program for accepting values in array and sort them in ascending order. /*Program for accepting values in array and sort them in ascending order.*/ #include<stdio.h> #include<conio.h> void main() { int a[5],i,j,t; clrscr(); printf("\nEnter 5 values in array: "); for(i=0;i<=4;i++) { scanf("%d",&a[i]); } printf("\nBefore arranging array values are:"); for(i=0;i<=4;i++) { printf("%d\t",a[i]); } for(i=0;i<=4;i++) { for(j=i+1;j<=4;j++) { if(a[i]>a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } } } printf("\nAfter arranging in accending order array values are: \n"); for(i=0;i<=4;i++) printf("%d\t",a[i]) ; getch(); }

Program for accepting values in an array and finding greatest and smallest value in that array.

Program for accepting values in an array and finding greatest and smallest value in that array. /*Program for finding greatest and smallest value in an array.*/ #include<stdio.h> #include<conio.h> void main() { int a[5],i,g,s; clrscr(); printf("\nEnter 5 values in array: \n"); for(i=0;i<=4;i++) { scanf("\n%d",&a[i]); } g=s=a[0]; printf("\n Accepted array values are:"); for(i=0;i<=4;i++) { if(g<a[i]) g=a[i]; if(s>a[i]) s=a[i]; printf("%d\t",a[i]); } printf("\n Greatest value is=%d",g); printf("\n\n Smallest value is=%d",s); getch(); }

A Program for accepting values to 2-Dimensional array and finding the sum of all the elements of 2-Dimensional array.

A Program for accepting values to 2-Dimensional array and finding the sum of all the elements of 2-Dimensional array. Just copy and paste the code below and compile and run the program. #include<stdio.h> #include<conio.h> void main() { int a[3][3],i,j,s=0; clrscr(); for(i=0;i<3;i++) { for(j=0;j<3;j++)     {       printf("\nEnter a value:");       scanf("%d",&a[i][j]);     }   } printf("\nYour array\n"); for(i=0;i<3;i++) {   for(j=0;j<3;j++)   {   s=s+a[i][j];   printf("%d\t",a[i][j]);   } } printf("\nSum of array elements=%d",s); getch(); }

Change the output's text colour in C

Hi friends, today I am going to share a funny code with you, which will change the colour of the text of out of your C console proram.  here's the code is. : #include<stdio.h> #include<conio.h> int main() {    clrscr();    textcolor(8);    cprintf("Hello world");    getch();    return 0; } You can change the value of  "textcolor(n);" function.  here "n" may be any interger value. Try it and have fun..

Sum up to given number.

A program for find the sum upto the number entered by the user: #include<stdio.h> #include<conio.h> void main() { clrscr(); int y,t,sum=0; printf("Enter the last value of the series, up to where the sum is required:  "); scanf("%d",&t); for(y=1;y<=t;y++) {  sum=sum+y; } printf("Sum of the series is: %f",sum); getch(); getch(); }

Program to find "Factorial value".

Program code for factorial without recursion : #include<stdio.h> #include<conio.h> void main()     { int i,f=1,n; clrscr(); printf("\nEnter a value: \n"); scanf("%d",&n); for(i=1;i<=n;i++)   { f=f*i;   } printf("Factorial value = %d",f); getch();     }

Pattern Printing

Image
Hi friends.. After a long time i am here back with program codes now. today i am going to write a program for pattern printing. I am writing code to print the following pattern: Code to print this pattern is:  #include<stdio.h> #include<conio.h> void main() { int i,j,k; clrscr(); i=1; while(i<=13) { if(i<=4) { k=3;    while(k>=i)     {       printf(" ");       k--;     }      j=1;      while(j<=i)      {       printf("* ");       j++;      }  }  if(i>4 && i<=7)  {   k=5;   while(k<=i)   {   printf(" ");   k++;   }   j=7;   while(j>=i)   {   printf("* ");   j--;   }   }   if(i>7 && i<=10)   {...

goto statement

Image
The Goto statement :- C language supports " goto " statement to branch unconditionally from one point to another in the program. The goto requires a label in order to identify the place where the branch is to be made. A label is any valid variable name, and must be followed by a colon. The label is placed just before the statement where the control is to be transferred.   The general forms of goto and label statements are:- The " label: " can be anywhere in the program either before or after the " goto label; " statement. While executing the program,when a statement like "goto begin;" is met, the flow of control will jump to the statement immediately following the label "begin:". A goto breaks the normal sequencial execution of the program. If the "label:" is before the statement "goto label;"   a loop will be formed and some statments will be executed repeatedly. Such a jump is calle...

Looping

Image
Looping :- Looping is a condition, in which a sequence of statements are executed until some conditions for termination of the loop are satisfied. A program loop consists of two part, one known as body of the loop and the other is known as control statement.  The control statement checks the specified condition and then performs the repeated execution of the statements contained in the body of the loop . i.e. control statement is a looping's part  that tests wheter the given condition is satisfying or not, if it is satisfiying then it directs the flow control to the body of the loop to b executed, until the specified condition do not gets false. Body of the loop is the part of looping which contains the packet of command line, which gets executed when the given condition in control statements satisfies. A looping process include following four steps :-   i) Setting and initialization of a condition variable.   ii) Execution of the statements in the loop. ...

Conditional Operator

The Conditional (? :) operator :- Conditional operator is   a combination of "?" and ":" and takes three operands. It is useful for making two-way decisions. General form of this operator is :-   "conditional expression ? expression 1 : expression 2" While executing this operator, conditional expression is evaluated first, if the result is non-zero, expression 1 is evaluated and is returned as the value of the conditional expression. Otherwise, expression 2 is evaluated and its value is returned as the value of conditional expression. For example, consider an if else expression :- if (x < 0)     flag = 0; else      flag = 1; But using conditional operator, it can be written as :-                                                                 fla...

Switch statement

Image
We have seen that when we have many alternatives and we have to select one of them, we can use an if statement to control the selection. However, the complexity of such a program increases dramatically when number of alternatives increases. To, come out from these situations C has a built-in multiway decision statement known as "switch" . It tests the value of a given variable or expression against a list of case values and when a match is found, a block of statements associated with that case is executed. The general form of a switch statement is:-  "switch(expression)    {      case value 1:          block 1          break;      case value 2:          block 2          break;      case value 3:          block ...

Decision making using if statement

Image
The if statement is a powerful decision making statement and is used to control the flow of execution of statements. It's a basically two way decision statement and is used in conjuction with an expression. It's syntax is:- "if(test expression)" It allows the computer to evaluate the expression first and then, depending on condition, it transfers the control to a particular statement. This point of program has two paths to follow,one for the true and other for false condition.As shown in figure:-     Practical examples of if statement:- 1. if(bank balance is < 500)     deposit money 2. if(room is dark)     put on lights The if statement may be implemented in different forms depending on the complexity of conditions to be tested. The different forms of if statements are:- 1.Simple if statement 2.if....else statement 3.Nested if....else statement 4. else if ladder 1.Simple if statement.  The general f...

Decision making and branching

We have seen that a C program is a set of statements which are normally executed sequentially in the order in which they appear. This happens when no options are available. During programming, we have a number of situations where we may have to change the execution of statements on certain conditions, or repeat a group of statements until certain  specified conditions are met. This involves a kind of decision making to see whether a particular condition has occured or not and then  direct the computer to execute certain statements accordingly.  C language supports the following statements to possesses such decision making capabilities.  i)   if statement  ii)  switch statement  iii) Conditional operator statement  iv)  goto statement These statements helps in decision making, so these are called as decision making statements. Also these statements controls the flow of execution ,so they are also known as control statements.   ...

Introduction to Array

Image
Array is the sequential collection of memory variables having same data type.Or, Arrays are a data structure which holds multiple variables of the same data type. Whenever there is need to store a group of data of the same type in the memory, arrays are used. Normally variables are used to store specific values.,But Consider the case where a programmer needs to keep track of a number of students of a collage. So far, our initial attempt will be to create a specific variable for each student. It may seems like:--  int stud1 = 101; int stud2 = 232; int stud3 = 231; int stud4 = 234; this much of line , we will have to write to store the roll of only 4 students, then think how much line of codes we will have to write for 1000s of students???  It will be a typical problem for us. To overcome this situation we use the concept of array. Using array, The replacement of the above example using arrays looks like: int...