Walk Lightly on this PLANET and yet leave such a FOOTPRINT that cannot be erased for thousands of Years..!!!
Visit Codstech for Cyber Security related Posts !

Visitors

Showing posts with label C-For loop. Show all posts
Showing posts with label C-For loop. Show all posts

Saturday, September 17, 2011

C-43 : Sum of 1 to 100 numbers

Refer  Find Sum , Sum of n even numbers using user defined fun... 


1st Method :

#include<stdio.h>
#include<conio.h>
void main()
            {
            clrscr();
            int i,sum=0;
            for(i=1;i<101;i++)
                        sum=sum+i;
            printf("The sum of the numbers from 1 to %d is %d\n",i-1,sum);
            getch();
            }

2nd Method :

#include<stdio.h>
#include<conio.h>
void main()
            {
            clrscr();
            int i,sum=0;
            for(i=1;i<101;sum+=i++)
            ;
            printf("The sum of the numbers from 1 to %d is %d\n",i-1,sum);
            getch();
            }


3rd Method :

#include<stdio.h>
#include<conio.h>
void main()
            {
            clrscr();
            int i,sum=0;
            for(i=1;i<101 && (sum+=i++);)
            ;
            printf("The sum of the numbers from 1 to %d is %d\n",i-1,sum);
            getch();
            }



Output : 


The sum of the numbers from 1 to 100 is 5050
=================================================================
Back to Home   &  C

Saturday, February 19, 2011

C30 : Sum of n even numbers using user defined functions

Back to Home   &  C


//Program to find sum of n even numbers using user defined functions.

#include<stdio.h>
#include<conio.h>
int sum(int l);
void main()
{
clrscr();
int n,s;
printf("Enter limit value\n");
scanf("%d",&n);
s=sum(n);
printf("Sum of even numbers=%d",s);
getch();
}
int sum(int l)
{
int i,sum=0;
for(i=0;i<l;i++)
{
if(i%2==0)
{
sum=sum+i;
}
}
return(sum);
}
===================================
Output :
Enter limit value
4
Sum of even numbers= 2
=========================================================================
Back to Home   &  C

Thursday, February 17, 2011

C21 : Enter details using Structures

Back to Home   &  C


A ))) //Program to enter student details by using a structure called stud

#include<stdio.h>
#include<conio.h>
struct stud
{
int rno;
char name[20];
char branch[20];
};
void main()
    {
    clrscr();
    struct stud s;
    printf("Enter roll no\n");
    scanf("%d",&s.rno);
    printf("Enter name\n");
    scanf("%s",s.name);
    printf("Enter branch\n");
    scanf("%s",&s.branch);
       clrscr();
       printf("Details of the student is..\n");
       printf("Roll number-%d\nName-%s\nBranch-%s\n",s.rno,s.name,s.branch);
getch();
}
----------------------------------------------------------------------------
Output :
Enter roll no
1
Enter name
aaa
Enter branch
Computer Science
Details of the student is..
Roll No-1
Name-aaa
Branch-Computer Science
===============================================
//you can write the above printf statement as :
printf("Roll number=%d\n",s.rno);
printf("Name=%s\n",s.name);
printf("Branch=%s\n",s.branch);
=========================================================================

B ))) //Program to enter n number of employee details by using a structure called emp


#include<stdio.h>
#include<conio.h>
struct emp
{
char name[20];
char dept[20];
int code;
};
void main()
{
struct emp e[20];
int i,n;
clrscr();
printf("Enter number of employee\n");
scanf("%d\n",&n);
for(i=0;i<=n;i++)
printf("Enter employee name\n");
scanf("%s",e[i].name);
printf("Enter department\n");
scanf("%s",e[i].dept);
printf("Enter employee code\n");
scanf("%d",&e[i].code);
clrscr();
printf("Name\tDepartment\tcode\n");
for(i=0;i<=n;i++)
{
printf("%s\t%s\t%d\n",e[i].name,e[i].dept,e[i].code);
}
getch();
}
-------------------------------------------------------------------------------
Output
Enter number of employee
2
Enter employee name
aaa
Enter department
Computer Science
Enter employee code
11

Enter employee name
bbb
Enter department
Maths
Enter employee code
22
This will print as :
Name    Dept            code
aaa         cs                11
bbb        maths           22
===============================================

C ))) //Program to enter n number of student details,
enter 3 subject mark, and find total marks (by using a structure called student)


#include<stdio.h>
#include<conio.h>
struct student
{
int rno;
char name[20];
int m1,m2,m3,t;
};
void main()
    {
    clrscr();
    struct student s[20];
    int i,n;
    printf("Enter number of students\n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
    printf("Enter roll no\n");
    scanf("%d",&s[i].rno);
    printf("Enter name\n");
    scanf("%s",s[i].name);
    printf("Enter 3 subject marks\n");
    scanf("%d%d%d",&s[i].m1,&s[i].m2,&s[i].m3);
    s[i].t=s[i].m1+s[i].m2+s[i].m3;
    }
     clrscr();
     printf("Details of the student is..\n");
     printf("Name\tRno\tMark1\tMark2\tMark3\tTotal\n");
     for(i=0;i<n;i++)
{
printf("%s\t%d\t%d\t%d\t%d\t%d\n",s[i].name,s[i].rno,s[i].m1,s[i].m2,s[i].m3,s[i].t);
getch();
     }
     }
-------------------------------------------------------------
Output
Enter number of students
2
Enter roll no
1
Enter name
aa
Enter 3 subject marks
21
34
56

Enter roll no
2
Enter name
bb
Enter 3 subject marks
65
65
89
This will print as :
Roll no    name          mark1     mark2    mark3    total
1              aa                  21             34          56        111
2              bb                 65            65           89         219
===============================================
Back to Home   &  C

Wednesday, February 16, 2011

C20 : Matrix addition

Back to Home   &  C


//Program to enter a matrix and print its addition

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[5][5],b[5][5],c[5][5],i,j,r,cl;
printf("Enter row and column\n");
scanf("%d%d",&r,&cl);
printf("Enter elements of first matrix\n");
for(i=0;i<r;i++)
{
for(j=0;i<cl;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Matrix elements of second matrix");
for(i=0;i<r;i++)
{
for(j=0;i<cl;j++)
{
 scanf("%d",&b[i][j]);
}
}
for(i=0;i<r;i++)
{
for(j=0;i<cl;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
for(i=0;i<r;i++)
{
for(j=0;i<cl;j++)
{
printf("%d\t",c[i][j]);
printf("\n");
}
getch();
}
}
-------------------------------------------------------------------
Output
Enter row and column
2
2
Enter elements of first matrix
1
1
1
1
Enter elements of second matrix
2
2
2
2
----------------
3   3
3   3
(May be some error in output)
================================
Back to Home   &  C

Saturday, February 12, 2011

C19 : Enter a matrix and print it

Back to Home   &  C


//Program to enter a matrix and print it

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[5][5],i,j,r,c;
printf("Enter row and column size\n");
scanf("%d%d",&r,&c);
printf("Enter elements of matrix\n");
for(i=0;i<r;i++)
{
for(j=0;i<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Matrix elements...\n");
for(i=0;i<r;i++)
{
for(j=0;i<c;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
getch();
}
-------------------------------------------------------------
Output ( May be some error in output)
Enter row and column size
2
2
Enter elements of matrix
34
65
78
98
Matrix elements...
34        65
78        98
(Will print in matrix format)
================================
Back to Home   &  C

Friday, February 11, 2011

C18 : Sort n numbers of array elements in ascending order

Back to Home   &  C


//Enter n numbers into an array and sort it in ascending order.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,n,a[20],t;
printf("Enter limit value\n");
scanf("%d",&n);
printf("Enter array elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
 }
printf("Array elements in ascending order...\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
getch();
}
-------------------------------------------------------------------
Output
Enter limit value
3
Enter array elements
3
1
6
Array elements in ascending order...
1
3
6
===============================================
Back to Home   &  C

C17 : Enter n numbers into an Array and print it

Back to Home   &  C


//Program to enter n numbers into an array and print it

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[10],i,n;
printf("Enter limit value\n");
scanf("%d",&n);
printf("Enter array elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Array elements are...\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
getch();
}
-----------------------------------------------------------------
Output
Enter limit value
2
Enter array elements
16
76
Array elements are...

16
76
===============================================
Back to Home   &  C

C16 : Array Declaration

Back to Home   &  C


//Program to declare an Array and initialize value to it

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[5]={20,30,10,40,60};
int i;
for(i=0;i<5;i++)
 {
 printf("\n%d\n",a[i]);
 }
getch();
}
---------------------------------------------------------
Output
20
30
10
40
60
===============================================
Back to Home   &  C

Thursday, February 10, 2011

C12. Print a multiplication table


Back to Home   &  C


//Program to print a multiplication table

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a=0,n,i;
printf("\n\nEnter the no for multiplication table\n\n");
scanf("%d",&n);
for(i=1;i<=10;i=i+1)
{
a=i*n;
printf("\n\n%d*%d=%d\n\n",i,n,a);
}
getch();
}
-----------------------------------------------------------------------
Output
Enter the no for multiplication table
2
1*2=2
2*2=4
3*2=6
.
.
.
10*2=20
=========================================

Back to Home   &  C

C11 : Read nos and display any of the given number as user's choice


Back to Home   &  C


//To read 10 nos and display any of the given number as user's choice

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,n,a[10];
printf("Enter 10 nos\n");
for(i=0;i<=n;i++)
{
   scanf("%d",&a[i]);
}
       //printf("Which no you want to display\n");
      //scanf("%d",&n);
for(i=0;i<=n;i++)
{
     printf("%d\n",a[n-1]);
}
getch();
}
----------------------------------------------------------------------------
Output
(Please correct this ..May be some error in output...)
Hint : Enter any 10 number..and the user want to display 7th number..
=========================================

Back to Home   &  C

C10 : Read a string and print it


Back to Home   &  C


A ))) //Read a string and print it

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
char n[20];
printf("Enter your name\n");
scanf("%s",n);
printf("Welcome %s",n);
getch();
}
--------------------------------------------------------------------
Output
Enter your name
IGNOU
Welcome IGNOU
=============================================
B ))) //Print a string 5 times (Using While Loop)


#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i=1;
while(i<=5)
{
printf("Hai\n");
i++;
}
getch();
}


--------------------------------------------------------------------
Output
Hai
Hai
Hai
Hai
Hai
=============================================
C ))) //Print a string n times (Using for Loop)



#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,n;
printf("Enter limit value\n");
scanf("%d",&n);
for(i=0;i<=n;i++)
 {
 printf("IGNOU\n");
 }
getch();
}


--------------------------------------------------------------------
Output
Enter limit value
4
IGNOU
IGNOU
IGNOU
IGNOU
=============================================

Back to Home   &  C

Wednesday, February 9, 2011

C7 : Even-Odd numbers Print/check/sum


Back to Home   &  C


A ))) //Program to print even numbers upto a given limit


Method -1
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i=2,n,e;
printf("Enter the limit\n");
scanf("%d",&n);
printf("Even nos upto %d\n",n);
for(i=2;i<=n;i=i+2)
         {
printf("%d\n",i);
         }
getch();
}

------------------------------------
Output
Enter the limit (Enter your choice)
8
Even numbers upto 8 
2
4
6
8



Method -2

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,n,
printf("Enter the limit\n");
scanf("%d",&n);
for(i=2;i<=n;i++)
     {
      if(i%2==0)
            {
             printf("%d\n",i)
            }
     }
getch();
}



------------------------------------
Output
Enter the limit (Enter your choice)
8
2

4
6
8

===============================================


B ))) //Check whether the given number is odd or even


#include<stdio.h>
#include<conio.h>
void main()
{
   clrscr();
   int i=1;
   printf("Enter the no\n");
   scanf("%d",&i);
   if(i%2==0)
      {
      printf("%d is even\n");
      }
   else
      {
      printf("%d is odd\n");
      }
   getch();
   }

------------------------------------------------------------------
Output
Enter the number
5
5 is odd

===============================================

C ))) ////Print the total number of odd or even within a given range(1 to 10)


#include<stdio.h>
#include<conio.h>
void main()
{
   clrscr();
   int i=1,o,e;
   printf("Enter the number\n");
   scanf("%d",&i);
   for(i=1;i<=10;i=i+1)
     {
      if(i%2==0)
         {
e=e+1;
         }
     else
         {
o=o+1;
         }
     }
   printf("Total no of even is %d,and odd is %d",e,o);
   getch();
}

------------------------------------------------------------------
Output
Enter the number
9
Total number of even is 4,and odd is 5
===============================================

Back to Home   &  C



C6 : Displaying numbers(While,for.Do-While & Arrays)


Back to Home   &  C


A )))) //Display 4 numbers(While Loop)
#include<stdio.h>
#include<conio.h>
void main()
          {
          clrscr();
          int i=1;
          while(i<=4)
                  {
                  printf("%d\n",i);
                  i=i+1;
                 }
        getch();
         }
---------------------------------------------
Output
1
2
3
4
===============================================
B )))) //Display 4 numbers(for loop)
#include<stdio.h>
#include<conio.h>
void main()
          {
          clrscr();
          int i=1;
          for(i=1;i<=4;i=i+1)
              {
               printf("%d\n",i);
               }
   getch();
}

---------------------------------------
Output

1
2
3
4


==============================================


C ))) //To display any 10 numbers using arrays(Our choice)
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,a[10];
printf("Enter 10 nos\n");
for(i=0;i<10;i++)
         {
scanf("%d",&a[i]);
         }
         printf("The 10 nos are:\n");
        for(i=0;i<10;i++)
               {
       printf("%d\n",a[i]);
               }
getch();
}
------------------------------------------------------------
Output
Enter 10 numbers (Enter 10 numbers as per your choice)
1
2
3
4
5
6
8
4
5
6
The 10 numbers are :
1
2
3
4
5
6
8
4
5
6
===============================================

D ))) //To display 10 numbers using arrays 


#include<stdio.h>
#include<conio.h>
void main()
        {
clrscr();
int i,a[10];
printf("Displaying 10 nos using arrays:\n");
for(i=0;i<=10;i++)
{
printf("%d\n",a[i]);
}
getch();
}
(May be some error in output)
===============================================

E ))) //Display n nos using Arrays(any numbers upto the given limit)
(Note the difference in the output of E and F)

#include<stdio.h>
#include<conio.h>
void main()
           {
clrscr();
int i,n,a[100];
printf("How many nos you want to display\n");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
printf("%d",a[i]);
}
getch();
}
------------------------------------------------------------
Output
How many nos you want to display
3
1
2
3
===============================================
F ))) //Display n nos using Arrays(any numbers upto the given limit)
(Note the difference in the output of E and F)

#include<stdio.h>
#include<conio.h>
void main()
           {
clrscr();
print i,n,a[100];
printf("How many nos you want to display\n");
scanf("%d",&n);
printf("Enter %d no\n",n);
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
printf("The nos are:\n");
for(i=1;i<=n;i++)
{
printf("%d\n",a[i]);
}
getch();
}
------------------------------------------------------------
Output
How many nos you want to display
2
Enter 2 numbers
3
4
The nos are:
3
4
===============================================
G ))) //Display n numbers(Do-While)



#include<stdio.h>
#include<conio.h>
void main()
{
   clrscr();
   int i=1,n;
   printf("How many nos you want to display\n");
   scanf("%d",&n);
   do
   {
      printf("%d\n",i);
      i=i+1;
   }
   while(i<=n);
getch();
}
---------------------------------------------------------

Output
How many nos you want to display
3
1
2
3
===============================================
H ))) //Display n numbers(For loop) (As per our choice)
Refer Eg:J

#include<stdio.h>
#include<conio.h>
void main()
{
   clrscr();
   int i=1,n;
   printf("How many nos you want to display\n");
   scanf("%d",&n);
   for(i=1;i<=n;i=i+1)
   {
      printf("%d\n",i);
   }
   getch();
}
---------------------------------------------------------
Output
How many nos you want to display
3
1
2
3
===============================================
I ))) //Display n numbers(While loop)


#include<stdio.h>
#include<conio.h>
void main()
{
   clrscr();
   int i=1,n;
   printf("How many nos you want to display\n");
   scanf("%d",&n);
   while(i<=n)
   {
      printf("%d\n",i);
      i=i+1;
   }
   getch();
}

---------------------------------------------------------
Output
How many nos you want to display
3
1
2
3
==============================================
J ))) //Display n numbers(For loop) - Not as per our choice
Refer Eg:H



#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i=1,n;
for(i=1;i<=10;i=i+1)
{
printf("%d\n",i);
}
getch();
}

---------------------------------------------------------
Output
1
2
3
4
5
6
7
8
9
10

==============================



Back to Home   &  C