The Computer Book _where you can learn more__ C Programmirigs (Nested Loop).pdf

14
10/26/2014 The Computer Book |where you can learn more|: C Programmirigs (Nested Loop) http://finesbook.blogspot.in/2012/06/c-programmirigs-nested-loop.html 1/14 The Computer Book |where you can learn more| SATURDAY, JUNE 16, 2012 C Programmirigs (Nested Loop) Search 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; Bhuwan Bhandari Follow 6 View my complete profile ABOUT ME 2012 (17) June (17) System Developm ent Life Cycle C Programmi rigs (Nested Loop) Course in detail for grade XII (Computer Science) C - programmi ng Report Writing for Project: Software BLOG ARCHIVE 0 More Next Blog» Create Blog Sign In

Transcript of The Computer Book _where you can learn more__ C Programmirigs (Nested Loop).pdf

Page 1: The Computer Book _where you can learn more__ C Programmirigs (Nested Loop).pdf

10/26/2014 The Computer Book |where you can learn more|: C Programmirigs (Nested Loop)

http://finesbook.blogspot.in/2012/06/c-programmirigs-nested-loop.html 1/14

The Computer Book |where you can learn more|

SATURDAY, JUNE 16, 2012

C Programmirigs (Nested Loop)

Search

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,ffor(i=1;i<=10;÷+i)f=i*j;for(j=i;j>O;--j){f=f*jprintf( %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()tint i,j;for(i=2;i<=500;++i){for(j=2;j<=i;++j){if(i%j==0)break;

BhuwanBhandari

Follow 6

View my completeprofile

ABOUT ME

▼ 2012 (17)

▼ June (17)System

Development LifeCycle

CProgrammirigs(NestedLoop)

Course indetail forgrade XII(ComputerScience)

C -programming

ReportWriting forProject:

Software

BLOG ARCHIVE

0 More Next Blog» Create Blog Sign In

Page 2: The Computer Book _where you can learn more__ C Programmirigs (Nested Loop).pdf

10/26/2014 The Computer Book |where you can learn more|: C Programmirigs (Nested Loop)

http://finesbook.blogspot.in/2012/06/c-programmirigs-nested-loop.html 2/14

{if(i==j)printf( "%d\t" ,i);{getch();}

4. Write a program to generate the following:122333444455555#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:1223 3 34 4 4 45 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:112123123412345#include<stdio.h> #include<conio.h>void main(){int i,j;

Engineering Practicle

8. SoftwareCostEstimation

7. SoftwareQualityandQualityAssurance

Chapter 6.Verification andValidation

Chapter 5.Implementation andMaintenance

Chapter 4.SoftwareDesign

Chapter 3.SoftwareRequirements

Chapter 2.ProjectManagement

Chapter 1.Introduction toSoftwareEngineering

SoftwareEngineering syllabus(Diploma)

COMPUTERNETWORK

HOWSHOULDREAD ABOOKBETTER?

Home

PAGES

Page 3: The Computer Book _where you can learn more__ C Programmirigs (Nested Loop).pdf

10/26/2014 The Computer Book |where you can learn more|: C Programmirigs (Nested Loop)

http://finesbook.blogspot.in/2012/06/c-programmirigs-nested-loop.html 3/14

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:555554444333221#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 4020 30 40 5030 40 50 604050 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.11211 23 211 2 34 32 11234543211 234565432112 345 67654321 #include<stdio.h>#include<conio.h>

Page 4: The Computer Book _where you can learn more__ C Programmirigs (Nested Loop).pdf

10/26/2014 The Computer Book |where you can learn more|: C Programmirigs (Nested Loop)

http://finesbook.blogspot.in/2012/06/c-programmirigs-nested-loop.html 4/14

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 series12345 678 9 1011 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+1printf("%d\t”,sum);}printf("\n");}getch(); }

1. What is an array? Write down its syntax with examples.

Page 5: The Computer Book _where you can learn more__ C Programmirigs (Nested Loop).pdf

10/26/2014 The Computer Book |where you can learn more|: C Programmirigs (Nested Loop)

http://finesbook.blogspot.in/2012/06/c-programmirigs-nested-loop.html 5/14

Ans: An array can be defined as a set of finite number of homogeneous elements or data items.Itmeans an array can contain one type of data only,either all integers,all characters,all floating pointetc.The individual data items are represented by their corresponding array elements(i.e.first dataitem is represented by first array element,second data item is represented by second array elementand so on). The individual array elements are distinguished from one another by the value that isassigned to a subscript.

Declaring of an arrayint a[5];Where int specifies data type of elements array,"a” is the name of array and number specifiedinside 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 byindex 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 bespecified 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 oneloop for reading and writing.If we are reading or writing two-dimensional array it would require two loops.Sirnilarly array of ndimension 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 arraya)Only one subscript is used inone-dimensional array

Two subscript is used in two-dimensional array.

b) Syntax>data_type array_name [subscript];Examples:char name[20];int marks[5]; etcHere, subscript is used to denote either rowsor columns.

Syntax>data_type array_name [subscript 1][subsc ript2];Examples:int marks[100][5];int matrix[5][5]; etc. Here, subscriptl is used todenotes rows and subscript2 denotes columns.

c) Only one loop statement is used to createor retrieve array elements,

Two looping statements are used to create orretrieve array elements, i.e. first loop for rowsand 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;

Example: Display elements of a matrix:#include <stdio.h>#include <conio.h>void main (){intmatrixa[2] [2],i,j;

Page 6: The Computer Book _where you can learn more__ C Programmirigs (Nested Loop).pdf

10/26/2014 The Computer Book |where you can learn more|: C Programmirigs (Nested Loop)

http://finesbook.blogspot.in/2012/06/c-programmirigs-nested-loop.html 6/14

P R O G R A M \0

printf (“Enter name:”);gets (name);printf (“Enter age \n”);scanf("%d”, &age); printf (“\n Name :%s”, name);printf (“Age:%d”, age);getch();}

printf(”Enter elements of a matrix:”):for(i=0;i<2;++i){for(j=0;j<2;++j)scanf("%d”,&matrixa[i] [ii);}//Displaying datafor(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 isknown as a literal. For example, “Hello” is literal. We can use such strings in our programs todisplay various kinds of messages on the screen. The strings can be stored and manipulated asarray of characters in C and C++. The last character in a string is always ‘\0’, a null character. Forexample, the string “PROGRAM” can be stored in an array as 0 1 2 3 4 5 6 7

items

The string requires total 8 locations of array item: seven for the alphabets in the string PROGRAMand 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 areint x[5] = { 56, 42, 90, 65, 55 }; x[0] x[1] x[2] x[3] x[4]or char text[6] = { “today”};

chartext[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, 89, 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 UnionArray is the collection ofdata items which will havethe same name.

Union is a collection ofhetrogeneous data types

Each element has specific Each element has no specific

Page 7: The Computer Book _where you can learn more__ C Programmirigs (Nested Loop).pdf

10/26/2014 The Computer Book |where you can learn more|: C Programmirigs (Nested Loop)

http://finesbook.blogspot.in/2012/06/c-programmirigs-nested-loop.html 7/14

memory space memory space but spaceallocated for largest elementis isutilized by other sized element.

Keyword is not required todeclare array,

Keyword is required to declareunion

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 [sizeof array];It consists only rows or columnsi.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_typemembern;}union user_defined_namevan, var2e.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—51. 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”);

Page 8: The Computer Book _where you can learn more__ C Programmirigs (Nested Loop).pdf

10/26/2014 The Computer Book |where you can learn more|: C Programmirigs (Nested Loop)

http://finesbook.blogspot.in/2012/06/c-programmirigs-nested-loop.html 8/14

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 keyboardfor(i=0;i<lOO;++i){printf( Enter age of 100 persons: ")scanf ("%d”,&num[i]);}//Displaying age of 100 personsprintf("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 greatestnumber. [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 employeesgetting salary between 5000-10000. [HSEB-2062]#include <stdio.h>

Page 9: The Computer Book _where you can learn more__ C Programmirigs (Nested Loop).pdf

10/26/2014 The Computer Book |where you can learn more|: C Programmirigs (Nested Loop)

http://finesbook.blogspot.in/2012/06/c-programmirigs-nested-loop.html 9/14

#include <conio.h >void main (){int count = 0, salary [200], i;// Reading salary of 200 employeesfor (i= 0; i< 200; i++){printf (" Enter salary :" );scanf (“%d", & salary [i]);}//counting number of employees getting salary between//5000 - 10000for (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. Thestudent gets a division as per the following rules:Percentage greater or equal to 60 First divisionPercentage between 45 and 59 Second divisionPercentage between 35 and 44 Third divisionPercentage less than 35 FailWrite a program using C language to process result of all’ students based on specification statedabove.#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”);elseprintf (“Fail\n”); getch ();}7. Write a program using C language to read the age of 100 persons and count number of persons

Page 10: The Computer Book _where you can learn more__ C Programmirigs (Nested Loop).pdf

10/26/2014 The Computer Book |where you can learn more|: C Programmirigs (Nested Loop)

http://finesbook.blogspot.in/2012/06/c-programmirigs-nested-loop.html 10/14

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 personsfor(i=0;i<100;i++){printf (“Enter age of a person :");scanf("%d”, & age [i]);}//Comparing age group between 50 & 60for (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. [HSEB2O62]

#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];

Page 11: The Computer Book _where you can learn more__ C Programmirigs (Nested Loop).pdf

10/26/2014 The Computer Book |where you can learn more|: C Programmirigs (Nested Loop)

http://finesbook.blogspot.in/2012/06/c-programmirigs-nested-loop.html 11/14

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 toevery 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 inascending order.[HSEB-2065]#include <stdio.h>#include <conio.h>#define MAX 500void 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; } } }

Page 12: The Computer Book _where you can learn more__ C Programmirigs (Nested Loop).pdf

10/26/2014 The Computer Book |where you can learn more|: C Programmirigs (Nested Loop)

http://finesbook.blogspot.in/2012/06/c-programmirigs-nested-loop.html 12/14

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 indescending order.

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

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 alphabeticalorder.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:");

Page 13: The Computer Book _where you can learn more__ C Programmirigs (Nested Loop).pdf

10/26/2014 The Computer Book |where you can learn more|: C Programmirigs (Nested Loop)

http://finesbook.blogspot.in/2012/06/c-programmirigs-nested-loop.html 13/14

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

Page 14: The Computer Book _where you can learn more__ C Programmirigs (Nested Loop).pdf

10/26/2014 The Computer Book |where you can learn more|: C Programmirigs (Nested Loop)

http://finesbook.blogspot.in/2012/06/c-programmirigs-nested-loop.html 14/14

Newer Post Older PostHome

Subscribe to: Post Comments (Atom)

Posted by Bhuwan Bhandari at 11:56 PM

}getch();}

17. Write a program to find row sum and column sum of a matrix18. 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 ofelements in ascending order using a function.21. Read the maximum and minimum temperature of 7 days of Kathmandu Valley and creates afunction to calculate minimum and maximum temperature, also find the average temperature.

Recommend this on Google

Enter your comment...

Comment as: Google Account

Publish

Preview

No comments:

Post a Comment

Picture Window template. Powered by Blogger.