Tuesday, 9 December 2014

Foor loop 

The general design of the for loop is reflected in some form or another in all procedural
programming languages. However, in C, it provides unexpected flexibility and power.
The general form of the for statement is
for (initialization; condition; increment) statement ;
The for loop allows many variations, but its most common form works like this: The initialization is
an assignment statement that is used to set the loop control variable. The condition is a relational
expression that determines when the loop exits. The increment defines how the loop control variable
changes each time the loop is repeated. You must separate these three major sections by semicolons.
The for loop continues to execute as long as the condition is true.

A simple program for understanding for loop of adding 1 to k and k will be read from users.

#include<stdio.h>
#include<conio.h>

void main()
{
int k,i,sum=0;
printf("Enter the number till you want your addition:");
scanf("%d",&k);
for(i=0;i<=k;i++)
{
sum=sum+i;
}
printf("The addition of numbers is %d",sum);
getch();
}

Monday, 8 December 2014

Hard programs from the paper of Computer(5 programs)

Write program to check whether given string is palindrome or not:

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
  char st[20];
  int flag=0,i,l;
  clrscr();
  printf("enter any string\n");
  gets(st);
  l=strlen(st);
  for(i=0;i<l/2;i++)
     if(st[i]!=st[l-1-i])
     {
                flag=1;
                break;
     }
  if(flag==0)
    printf("String is Palindrom");
  else
    printf("String is not palindrom\n");
getch();
}

Write program to find sum of two matrix:

#include <stdio.h>

int main()
{
   int m, n, c, d, first[10][10], second[10][10], sum[10][10];

   printf("Enter the number of rows and columns of matrix\n");
   scanf("%d%d", &m, &n);
   printf("Enter the elements of first matrix\n");

   for ( c = 0 ; c < m ; c++ )
      for ( d = 0 ; d < n ; d++ )
         scanf("%d", &first[c][d]);

   printf("Enter the elements of second matrix\n");

   for ( c = 0 ; c < m ; c++ )
      for ( d = 0 ; d < n ; d++ )
            scanf("%d", &second[c][d]);

   for ( c = 0 ; c < m ; c++ )
      for ( d = 0 ; d < n ; d++ )
         sum[c][d] = first[c][d] + second[c][d];

   printf("Sum of entered matrices:-\n");

   for ( c = 0 ; c < m ; c++ )
   {
      for ( d = 0 ; d < n ; d++ )
         printf("%d\t", sum[c][d]);

      printf("\n");
   }

   return 0;
}

Write program to find prime number from given n numbers:

#include<stdio.h>

#include<conio.h>
int main()
{
 int num,n,div,p;
 printf("Enter any number: ");
 scanf("%d", &num);
 for(n=2; n<=num; n++)
 {
  for(div=2; div<n; div++)
  {
   if(n%div==0)
   {
     p=0;
     break;
   }
   p=1;
  }
  if(p)
    printf("\t%d",n);
 }
 getch();
 return 0;
}

Write program to enter the details of student through structure:

#include<stdio.h>
#include<conio.h>

struct stud {
                char name[50];
                int roll_no;
                int marks[3];
                int total;
};

main()
{
                struct stud s[12];
                int i,j;
               
                printf("Enter the students details:\n");
                printf("In details enter the name,roll no,marks1,marks2,marks3\n");
               
                for(i=0;i<=2;i++)
                {
                                scanf("%s %d %d %d %d\n",&s[i].name,&s[i].roll_no,&s[i].marks[0],&s[i].marks[1],&s[i].marks[2]);
                }o 
                for(i=0;i<=2;i++)
                {
                                s[i].total=s[i].marks[0]+s[i].marks[1]+s[i].marks[2];
                }
                for(i=0;i<=2;i++)
                {
                                printf("name=%s\troll no=%d\tmarks1=%d\tmarks2=%d\tmarks3=%d\ttotal=%d\n",s[i].name,s[i].roll_no,s[i].marks[0],s[i].marks[1],s[i].marks[2],s[i].total);
                }
               
                getch();
}

 Write program to add numbers from 1 to K:

#include<stdio.h>
#include<conio.h>

void main()
{
                int k,i,sum=0;
                printf("Enter the number till you want your addition:");
                scanf("%d",&k);
                for(i=0;i<=k;i++)
                {
                                sum=sum+i;
                }
                printf("The addition of numbers is %d",sum);
                getch();
}


Be connected to my blog for more information about C language

Please comment if you have any query................


Wednesday, 3 December 2014

Program of Switch statement...............


#include<stdio.h>
#include<conio.h>

int a,b,c,add,min,pro,mod;
int sum();
int sub();
int mul();
int div();

int main()
{
int i;
printf("1:sum\n2:sub\n3:mul\n4:div\n");
printf("Enter the a:");
scanf("%d",&a);
printf("Enter the b:");
scanf("%d",&b);
printf("Enter the choice:");
scanf("%d",&i);
switch(i)
{
case 1:
sum();
printf("The answer is %d",add);
break;
case 2:
sub();
printf("The answer is %d",min);
break;
case 3:
mul();
printf("The answer is %d",pro);
break;
case 4:
div();
printf("The answer is %d",mod);
break;
default:
printf("Out of course hai bhai---->.........................");
}
return 0;
}

int sum()
{
add=a+b;
return add;
}

int sub()
{
min=a-b;
return min;
}

int mul()
{
pro=a*b;
return pro;
}

int div()
{
mod=a/b;
return mod;
}

Switch statement..........


C has a built-in multiple-branch selection statement, called switch, which successively tests the
value of an expression against a list of integer or character constants. When a match is found, the
statements associated with that constant are executed. The general form of the switch statement is

switch (expression) {
case constant1:
statement sequence
break;
case constant2:
statement sequence
break;
case constant3:
statement sequence
break;
.
.
.
default
statement sequence
}
The expression must evaluate to an integer type. Thus, you can use character or integer values, but
floating-point expressions, for example, are not allowed. The value of expression is tested against
the values, one after another, of the constants specified in the case statements. When a match is
found, the statement sequence associated with that case is executed until the break statement or the
end of the switch statement is reached. The default statement is executed if no matches are found.
The default is optional, and if it is not present, no action takes place if all matches fail.
C89 specifies that a switch can have at least 257 case statements. C99 requires that at least 1,023
case statements be supported. In practice, you will usually want to limit the number of case
statements to a smaller amount for efficiency. Although case is a label statement, it cannot exist by
itself, outside of a switch.
The break statement is one of C's jump statements. You can use it in loops as well as in the switch
statement (see the section ''Iteration Statements"). When break is encountered in a switch, program
execution "jumps" to the line of code following the switch statement.
There are three important things to know about the switch statement:
• The switch differs from the if in that switch can only test for equality, whereas if can evaluate any
type of relational or logical expression.
• No two case constants in the same switch can have identical values. Of course, a switch statement
enclosed by an outer switch may have case constants that are in common.
• If character constants are used in the switch statement, they are automatically converted to integers
(as is specified by C's type conversion rules).

Program for ternary operators...............




#include <stdio.h>
int f1(int n);
int f2(void);
int main(void)
{
int t;
printf("Enter a number: ");
scanf("%d", &t);
/* print proper message */
t ? f1(t) + f2() : printf("zero entered.");
printf("\n");
return 0;
}
int f1(int n)
{
printf("%d ", n);
return 0;
}

Saturday, 4 October 2014

IF - ELSE STATEMENT

This If - Else statement comes in the decision taking group. It is the basic part of the decision taking group. 

Syntax:

if(condition)
       statement1;
else
       statement2;


Write program to add two integer number in run time value

INPUT

#include<stdio.h>
#include<conio.h>

void main()

{
                int a,b,total;

                printf("Enter the value of a:");
                scanf("%d",&a);
                printf("Enter the value of b:");
                scanf("%d",&b);
                total=a+b;
                printf("The sum is %d",total);
                getch();
}

OUTPUT :

Enter the value of a: 4
Enter the value of b: 5
The sum is 9

 
 

Friday, 3 October 2014

Understanding the structure of basic program

We have seen the structure of C language it was the basic one but important to know in detail to learn more about C language.

The starting of the C program is done by the file:
           #include<stdio.h>
It is known as header file known as standard input output file and ".h" is the extension for the header file. This file is important because without it program cannot be defined.

Thereafter  the function "main()" is used. It makes the compiler know that the program is going to be started and without this function output cannot come , it is one of the main functio of the file without it program cannot be runned.

Now you can see curly brackets "{" under the main function it indicates the starting of the program. The content of the program is written inside this curly bracket . 

Now we can see the line printed inside the "/*........*/" . It indicates the comment kept by the programmer to make other programmer understand the program. In fact the comments are of two types
1. /*.............*/ - Single line comment
2. //...............   - Double line comment
There is no much difference between both of them.

Then we can see the line inside "printf( ....);" .It is the statement given to the compiler to print and the ";" is the necessary symbol needed to complete that statement otherwise it will show error at the time of compiling.

The "getch();" is the function needed for the completion of program but it is not every time necessary to write.

This was all about the structure of C language program of basic .

NOTE:- To know more about the C language continue reading this blog - KING's BLOG.

Thursday, 2 October 2014

About basic C language history

The C language is the language that cannot be learned easily but very much soft to feel because it is made for feelings..  As we know the characteristics of the software is not audible but it can be filed same way the C language is too software based language so it can only be feeled.

The history of C language is very much interesting that it was developed by the scientist DENISE RICHIE in the bells laboratory in 1969.

This made the life of programmers very much easy and because they were using many hard language to make the programs so they were facing the problems with the errors in that but with the step of C language the problems became very much easy for all programmers. It was the first portable language that was ever developed in past which means it can be written on any machine and run on any machine .

STRUCTURE OF C LANGUAGE:


Here we are going to type one simple and basic program.

INPUT:

#include<stdio.h>
#include<conio.h>


void main()

{

          /*Start of program.......................*/
                printf("WELCOME TO THE KING's BLOG");
                getch();
          /*End of the program..................*/
}

OUTPUT:


WELCOME TO THE KING's BLOG

NOTE :- To know more about C language continue reading this blog -   THE KING's BLOG