Saturday, June 16, 2012

C Programmirigs (Nested Loop)

Loading

C Programmirigs     (Nested Loop)
Write a program to print multiplication table of first 10 natural number.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,c;
for(i= 1 ;i<= 1 0;++i)
for(i= 1 ;j<=10;++j)
{
C=i*j;
printf(%d*%d=%d\t”,i,j,c);
}
printf("\n");
}
getch();
}

2. Write a program to display factorial number of first 10 natural number.
 #include<stdio.n>
#lnclude<conlo.n>
void main()
{
int i,j,f
for(i=1;i<=10;÷+i)
f=i*j;
for(j=i;j>O;--j)
{
f=f*j
printf( %d=%d\n ,i,f);
getch();
}

3. Write a program to display prime number upto 500 numbers.
Ans: #include<stdio.h>
#include<conio.h>
void main()
t
int i,j;
for(i=2;i<=500;++i)
{
for(j=2;j<=i;++j)
{
if(i%j==0)
break;
{
if(i==j)
printf( "%d\t" ,i);
{
getch();
}

4. Write a program to generate the following:
1
22
333
4444
55555
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
for(i=1;j<=5;++i)
{
for(j=1;j<=i;++j)
{
printf("%d",i);
}
printf("\n");
}
getch();
}
5. Write a program to display the following series:
1
22
3 3 3
4 4 4 4
5 5 5 5 5
#include <stdio.h>
#include <conio.h>
voidmain()
{
int n,a,j;
for(a=1;a<=5;a++)
{
for (j= 0; j < = 5-a ; j+ +)
printf ("   ")
for(j=1;j<=a;j++)
printf ("%d “, a);
printf ( "\n" );
}
getch ();
}
6. Write a program to generate the following:
1
12
123
1234
12345
#include<stdio.h>
 #include<conio.h>
void main()
{
int i,j;
 for(i=1 ;i<=5;++i)
{
for(j= 1 ;j<=i;++j)
{
printf("%d” ,j);
}
printf("\n");
}
getch();
}
7. Write a C Program to display the following:
55555
4444
333
22
1
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
for(i=5;i>=0;--i)
{
for(j= 1 ;j<=i;++j)
{
printf( "%d” ,i);
printf"(\n”);
}
getch();
}
8. Write a program to display the following table:
10 20 30 40
20 30 40 50
30 40 50 60
4050 60 70
#include<stdio.h>
 #include<conio.h>
void main()
{
int i,j;
 for(i=0;i<=4;++i)
{
for(j= 1 ;j.<=4;++j)
{
printf("%d\t",(i+j)* 10);
}
printf("\n”);
}
getch();
}
9. Write a C program to display the pyramid of numbers for a given number.
1
121
1 23 21
1 2 34 32 1
123454321
1 2345654321
12 345 67654321
 #include<stdio.h>
#include<conio.h>
void main()
{
int n,a,j,k;
printf(”Enter a number to form a pyramid:”);
scanf("%d”,&n);
printf("\n\t\n\t”);
for (a = 1; a < =n; a ++)
{
for(j = 1;j<=n-a;j ++)
}
printf (“  ");
for (k=1 ;k<=a;k++)
printf (“%d", k);
for (k=a-1 ;k>0;k--)
printf("%d ", k);
printf (“\n\t\t”);
}
getch ();
}
10. Write a program to display the following series
1
23
45 6
78 9 10
11 12 13 14 15 [i.e.Floyd’s Theorem]
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j=l,k;
for(i= 1 ;i<=5;++i)
{
for(k =1 ;k<=i;j++,k++)
printf("%dt",j);
printf("\n");
}
getch();
}



Or,
#include<stdio.h>
#include<conio.h>
 void main()
{
int i,j,sum=0;
for(i=1 ;i<5;++i)
{
for(j=0;j<i;++j)
{
sum=sum+1
printf("%d\t”,sum);
}
printf("\n");
}
getch();
 }

1.     What is an array? Write down its syntax with examples.
Ans: An array can be defined as a set of finite number of homogeneous elements or data items.It means an array can contain one type of data only,either all integers,all characters,all floating point etc.The individual data items are represented by their corresponding array elements(i.e.first data item is represented by first array element,second data item is represented by second array element and so on). The individual array elements are distinguished from one another by the value that is assigned to a subscript.

Declaring of an array
int a[5];
Where int specifies data type of elements array,"a” is the name of array and number specified inside the square brackets is number of elements an array can stores.
Followings are some of the concept to be remembered about arrays:
a) The individual element of an array can be accessed by specifying name of array,followed by index or subscript inside square brackets.Example:int 45];
b) The first element of array has index zero[0].It means first element and last element will be specified as a[0] and a[4] respectively.
c) The elements of array always be stored in consecutive memory locations.
d) The size of array is given by (Upperbound-lowerbound)+1
(4-0)+ 1 =5.
where O=lowerbound and 4=upperbound.
e) Arrays can be read or written through loop.If we read a one- dimensional array,it requires one loop for reading and writing.
If we are reading or writing two-dimensional array it would require two loops.Sirnilarly array of n dimension would require n loops.
There are two types of array and these are
(a) One-dimensional array
(b) Two- dimensional array
(a) One dimensional array: It consists only either rows or columns.
syntax> data _ type array_name [expression];
e.g. int marks [500];
char sentence [100];
float percentage [8]; etc.
(b) Two dimensional array: It consists both rows and columns. syntax> data — type array name [expression 1] [expression2];
e.g.
int matrix [5] [5];
char name [10] [30]; etc.
2.     Differentiate between one-dimensional and two-dimensional array.
Differentiation between one-dimensional and multi-dimensional array:


One-Dimensional array
Two-dimensional array
a)Only one subscript is used in
one-dimensional array
Two subscript is used in two-
dimensional array.
b) Syntax>data_type array_name [subscript];
Examples:
char name[20];
int marks[5]; etc
Here, subscript is used to denote either rows or columns.
Syntax>data_type array_name [subscript 1]
[subsc ript2];
Examples:
int marks[100][5];
int matrix[5][5]; etc. Here, subscriptl is used to denotes rows and subscript2 denotes columns.
c) Only one loop statement is used to create or retrieve array elements,
Two looping statements are used to create or retrieve array elements, i.e. first loop for rows and second loop for columns.
d) Example: Display name & age of a student. #include <stdio.h>
#include <conio.h>
void main ()
{
char name [20];
int age;
printf (“Enter name:”);
gets (name);
printf (“Enter age \n”);
scanf("%d”, &age);
 printf (“\n Name :%s”, name);
printf (“Age:%d”, age);
getch();
}
Example: Display elements of a matrix:
#include <stdio.h>
#include <conio.h>
void main ()
{
int
matrixa[2] [2],i,j;
printf(”Enter elements of a matrix:”):
for(i=0;i<2;++i)
{
for(j=0;j<2;++j)
scanf("%d”,&matrixa[i] [ii);
}
//Displaying data
for(i=0;i<2;++i)
{
for(j=0;j<2;++j)

printf("%d\t”,matrixa[i] [j]);
printf("\n”) ;
}
Getch();
}


3. What is a string?Explain,how string is declared?
A string is a group of characters of any length. The string enclosed within double quotation marks is known as a literal. For example, “Hello” is literal. We can use such strings in our programs to display various kinds of messages on the screen. The strings can be stored and manipulated as array of characters in C and C++. The last character in a string is always ‘\0’, a null character. For example, the string “PROGRAM” can be stored in an array as
                  0      1    2     3     4    5      6     7
P
R
O
G
R
A
M
\0
items

The string requires total 8 locations of array item: seven for the alphabets in the string PROGRAM and one for the null character. The array item can be declared as:
char item[8];
Other examples of array declarations are:
int x[25];
char name [20];
float sal[15];
Array declarations that include the assignment of initial values are
int x[5] =  { 56,    42,     90,        65,    55     };
                  x[0]    x[1]    x[2]     x[3]    x[4]
or char text[6] = { “today”};
char text[6] ={
‘t’
‘o’
‘d’
‘a’
‘y’
‘\0’
};


Text
[0]
Text
[1]
Text
[2]
Text
[3]
Text
[4]
Text
[5]
Text


intA[4][3] =        {           5,         2,         7,
3,         9,         6,
1,         7,         8
9,         2,         3};
int list[5][10] = {             “Ram”,
“Shyam”,
“Han”,
“Gopal”,
“Rampayri” };
3.     Write differences between array and unions with syntax.
Differentiation between structure and unions:
Array
Union
Array is the collection of
data items which will have
the same name.

Union is a collection of
hetrogeneous data types

Each element has specific
memory space

Each element has no specific
memory space but space
allocated for largest elementis is
utilized by other sized element.

Keyword is not required to
declare array,

Keyword is required to declare
union

Array has its type and these are:
(i) one dimensional array
(ii) multi dimensional array


Union has no type

The syntax of array:
(i) One dimensional array:
data_type array_name [size
of array];
It consists only rows or columns
i.e. char name [20];
(ii) Two-dimensional array:
data_type array_name[expl] [exp2];
e.g. char name [5] [25];
It contains rows & columns.



Syntax of union:
union user_defined_name
{
data_type member 1;
data_type member2;
…………….
data_type
membern;
}
union user_defined_name
van, var2
e.g struct student {
char name [20];
int age;
};
union student one;
Example : Display name & age:
#include <stdio.h>
 #include <conio.h>
void main ()
{
char name [20];
int age;
pnintf (“Enter name:”);
 gets (name);
printf (“Enter age \n");
scanf (“%d", &age);
pnintf (“\n Name :%s”, name);
printf("Age:%d”, age);
 getch () ;
}


Example: Display name & age:
#include <stdio.h>
 #include <conio.h>
void main ()
{
union student
{
char name [20];
int age;
}
one;
printf (“Enter name :\n");
scanf (“%s”, one.name);
printf (“\n Enter age:”);
scanf ("%d”, &one.age);
printf (“\n Name : %s”,one.name);
printf (“Age :%d”, one.age);
getch();
}



C Programmings—5
1. Write a program to display the following numbers.
3,56,76,5,90
#include <stdio.h>
 #include <conio.h>
void main ()
{
int i,num[5]={ 3,56,76,5,90};
printf("List of number\n”);
for(i=0;i<4;++i)
printf("%d” ,num[i]);
getch();
}

2. Write a C program to read age of 100 person from keyboard,display it in proper format.
#include<stdio.h>
#include<conio.h>
void main ()
{
int i,num[100];
//Giving input from keyboard
for(i=0;i<lOO;++i)
{
printf( Enter age of 100 persons: ")
scanf ("%d”,&num[i]);
}
//Displaying age of 100 persons
printf("List of number\n”);
for(i=0;i<100;++i)
printf("%d” ,num[i]);
getch():
}
3. Write a C program to read 10 numbers and print them in reverse order
#include<stdio.h>
#include<conio.h>
void main()
{
int num[10],i;
for(i=0;i<O;i++)
{
printf( Enter a number: );
scanf("%d”,&num[i]);
}
printf("The number in reverse order are:");
for(i=9;i>0;i--)
printf("%d\n\t\t\t\t”,num[i]);
getch();
}

4. Write a program to store ten different constant variables in any array and print out the greatest number. [HSEB-2064]
#include <stdio.h>
#include <conio.h>
void main ()
{
int a[10]={100,1,23,43,56,21,80,54,67,2};
int i, large;
large= a [0];
for(i=0;i<10;i++)
{
if (large < a [i])
large = a[i];
}
printf (“\n largest no = %d”, large);
getch ();
}
5. Write a ‘C’ program to read salaries of 200 employees and count the number of employees getting salary between 5000-10000. [HSEB-2062]
#include <stdio.h>
#include <conio.h >
void main ()
{
int count = 0, salary [200], i;
// Reading salary of 200 employees
for (i= 0; i< 200; i++)
{
printf (" Enter salary :" );
scanf (“%d", & salary [i]);
}
//counting number of employees getting salary between
//5000 - 10000
for (i = 0; i < 200; i ++)
{
if (salary [i]> 5000 && salary [i] < 10000)
count + +;
}
printf (“Total number of employees getting salaries between 500-10000 = %d”, count);
getch();
}

The marks obtained by a student in a 7 different subjects are entered through the keyboard. The student gets a division as per the following rules:
Percentage greater or equal to 60           First division
Percentage between 45 and 59               Second division
Percentage between 35 and 44               Third division
Percentage less than 35                         Fail
Write a program using C language to process result of all’ students based on specification stated above.
#include <stdio.h>
#include <conio.h>
void main ()
{
char name [20];
int i;
float x [7], per, total = 0;
printf (“Enter name =");
scanf (“%s”, name);
for (i= 0; i< 7; i++)
{
printf (“Enter mark of a subject =");
scanf (“%f", &x [i]);
total = total + x [i];
}
printf (“\n\n”);
per = total/7;
printf (“Name %s\n”, name);
printf (“Total = % f\n”, total);
printf (“Percentage = %fn”, per);
if (per>60)
printf (“First division\n”);
else if (per> = 45)
printf (“Second division\n”);
 else if (per> = 35)
printf (“Third division”);
else
printf (“Fail\n”);
 getch ();
}
7. Write a program using C language to read the age of 100 persons and count number of persons in the age group between 50 and 60. Use “For” and “Continue” statements. [HSEB 2061]
#include <stdio.h>
 #include <conio.h>
void main ()
{
int age [100];
int i, count 0;
//Reading the age of 100 persons
for(i=0;i<100;i++)
{
printf (“Enter age of a person :");
scanf("%d”, & age [i]);
}
//Comparing age group between 50 & 60
for (i= 0, i< 100; i++)
if (age [i] > = 50 && age {i] <= 6O)
count + +;
continue;
}
printf (“Total number of persons in the age group between 50 and 60 are: %d", count);
getch();
}
8. Write a program to find whether the given number is palindrome or not.
#include <stdio.h>
#include <conio.h>
void main ()
{
int a[10],num,i=0,result=O,n;
printf("Enter the number:”);
scanf(”%d”,&n);
num=n;
while(num!=0)
result=result* 10;
a[i]=num%1O;
num=num/ 10;
result=result+a[i];
++i;
}
if(result==n)
printf("The number is palindrome”);
getch();
}

9. Write a ‘C’ program to input ‘n’ numbers and find out the greatest and smallest number. [HSEB 2O62]

#include <stdio.h>
 #include <conio.h>
void main ()
{
int n, i, max, a[100], min;
printf (“How many nos ?“);
scanf (“%d”, &n);
//Reading numbers for(i=0;i<n;i++)
printf (“Enter numbers :“);
scanf(”%d”, &a [i]);
}
// Finding the greatest and smallest number.
max=a[O];
min=a[0];
for(i=0;i<n;i++)
{
if (a [i] > max)
max = a[i];
else if( a[i]<min)
min = a[i];
else
;
}
printf (“The greatest number %d\n”, max);
printf (“The smallest number = %d”, min);
getch ();
}

10. Write a program, which reads marks of N students in an array and add a grace of 10 marks to every element of the array.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,mark[ 10] ,sum[1 0];
printf("Enter how many students:”);
scanf("%d” ,&n);
printf("\n\n”);
for(i=0;i<n;i++)
{
printf(”Enter mark:”);
scanf("%d”,&mark[i]);
sum [i]=mark[i] + 10;
}
printf("\n\n”);
for(i=0;i<n;i++)
printf(”The mark of studit after adding a grace 0f 10 marks:%d\n”,sum [i]);
getch();
}

11. Write a program to enter ‘n’ numbers into one dimensional array and sort and display them in ascending order.[HSEB-2065]
#include <stdio.h>
#include <conio.h>
#define MAX 500
void main ()
int a [MAX], i, j, n, temp; printf (“How many numbers ?“);
scanf (“%d”, &n);
for(i=0;i<n;i++)
{
printf (“Enter numbers =");
 scanf (“%d”,& a [i]);
for(i=0;i<n;i++)
     {
      for (j = i+1; j < n; j ++)
         {
        if(a [i]> a [i])
             {
                 temp = a[i];
                a[i]=a[j];
                a [j] = temp;
             }
       }
   }
printf (‘Sorted array\n”);
for(i=0;i<n;i++)
    {
     printf (“%d\t", a[i]);
   }
getch();
}

12. Write a program to read a set of ‘n’ numbers from the standard input device and to sort them in descending order.

13. Write a C program that reads name and mark of 10 different students and prints them in tabular format.

14. Write a program to input names of ‘n’ number of students and sort them in alphabetical order. [HSEB-2062]
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main ()
{
char name [100] [100], temp[100];
 int i, j, n;
prmntf (“Enter number of students =“);
 scanf(”%d’, &n);
for(i=0;i<n;i++)
{
    printf (“Enter name :“);
    scanf (‘%s”, name [ii);
    fflush (stdin);
    for(i=0;i<n;i++)
          {
          for (j = + l;j <n;j +
             {
                 if(strcmpi (name [i], name [j]) > 0)
    {
                       strcpy(temp, name [i]);
        strcpy (hame [i], name [j]);
       strcpy (name [j], temp);
   }
}
        }
for(I =0;I < n;I ++)
{
printf (“%s\n”, name [1]);
}
getch ( );
}
I 15. Write a C program to read anme and address of 5 different students,sort them in alphabetical order.
Ans: #include<stdio.h>
 #include<conio.h>
 #includecstring.h>
 void main( )
{
 char name[5] [80] ,add[5] [80] ,temp[80,temp 1 [80];
 int i,j;


printf("enter name and age:");
 for(i=0;i<5 ;i++) scanf(”%s%s” ,name[i] ,add [i]);
for(i=0;i<5 ;i++)
{
for(j=i+1 ;j<5;j++)
       if(strcmp (name [i] ,name [3]) >0)
{
       strcpy(temp,name[i]);
       strcpy(name [i] ,name[j]);
       strcpy(name [j] ,temp);


       strcpy(temp 1 ,add[i])
    strcpy(add[i],add[j]);
       strcpy(add [j] ,temp 1);
}
}
printf(”The result\n”);
 for(i=0;i<5 ;i÷+)
 printf(”%s\t%s\n” ,name[i] ,add[i]);


getch();

16. Write a C program to read elements of any two matrices and sum of them.
                                                           Or,
Write a program to add two matrices. [HSEB 2065]
#include <stdio.h>                              
#include <conio.h>
void main ()
{
    int a[5][5], b[5][5], c[5] [5], i, j;
   Printf (“Enter the elements of matrix A = “);
  for (i= 0; i< 5; i+ +)
     {
      for (j= 0; j< 5; j+ +)
       {
        scanf("%d",&a[i][j]);
        }
     }
printf (“Enter elements of matrix B =“);
  for (i= 0; i< 5; i+ +)
     {
      for (j= 0; j< 5; j+ +)
       {
        scanf("%d",&b[i][j]);
        }
     }
  for (i= 0; i< 5; i+ +)
     {
      for (j= 0; j< 5; j+ +)
       {
            c[i][j] = a[i][j] +b[i][j];
        }
     }
  for (i= 0; i< 5; i+ +)
     {
      for (j= 0; j< 5; j+ +)
       {
            printf("%d\t",c[i][j]);
        }
     }
getch();
}




17. Write a program to find row sum and column sum of a matrix
18. Write a program to obtain transpose of a 3X3 matrix. Ans: #include <stdio.h>
19. Write a program to read a set of numbers from keyboard and to sort out the given array of elements in ascending order using a function.
21. Read the maximum and minimum temperature of 7 days of Kathmandu Valley and creates a function to calculate minimum and maximum temperature, also find the average temperature.

No comments:

Post a Comment