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;
}