Вы находитесь на странице: 1из 9

this program 1 22 333 4444 55555 main() { int i,j; for(i=1;i<=5;i++) { for(j=i;j<=i;j++) { printf("%d",i); printf("\n"); } } } Question 1(a): Write

a program to print the following pattern: 1 12 123 1234 12345 Answer: #include<stdio.h> #include<conio.h> void main() { int i,j,num_lines; clrscr(); printf("\nEnter the number of lines to print: "); scanf("%d", &num_lines); for(i=1; i<=num_lines; i++) { for(j=1; j<=i; j++) { printf("%d ", j); } printf("\n"); }

getch(); } Question 1(b): Write a program to print the following pattern: 1 22 333 4444 55555 Answer: #include<stdio.h> #include<conio.h> void main() { int i,j,num_lines; clrscr(); printf("\nEnter the number of lines to print: "); scanf("%d", &num_lines); for(i=1; i<=num_lines; i++) { for(j=1; j<=i; j++) { printf("%d ", i); } printf("\n"); } getch(); }

2. Union and Intersection of two sorted arrays


For example, if the input arrays are: arr1[] = {1, 3, 4, 5, 7} arr2[] = {2, 3, 5, 6} Then your program should print Union as {1, 2, 3, 4, 5, 6, 7} and Intersection as {3, 5}.

#include #include void main() { clrscr(); int x[5],y[5],z[10],i,i1,i2,count=0,j;

clrscr(); cout<<"\nenter 1st array\n"; for(i=0;i>x[i]; cout<<"\nenter 2nd array\n"; for(i=0;i>y[i]; cout<<"\nthe union of 2 arrays is\n"; for(i1=0;i1<5;i1++) z[i1]=x[i1]; for(i2=0;i2<5;i2++) { for(j=0;j<5;j++) { if(z[j]==y[i2]); count++; if(count==0) { z[i1]=y[i2]; i1++; } } count=0; } for(j=0;j<i1;j++) cout<<endl<<z[j]; } 2. Union and Intersection of two sorted arrays For example, if the input arrays are: arr1[] = {1, 3, 4, 5, 7} arr2[] = {2, 3, 5, 6} Then your program should print Union as {1, 2, 3, 4, 5, 6, 7} and Intersection as {3, 5}.

#include #include void main() { clrscr(); int x[5],y[5],z[10],i,i1,i2,count=0,j; clrscr(); cout<<"\nenter 1st array\n"; for(i=0;i>x[i]; cout<<"\nenter 2nd array\n"; for(i=0;i>y[i];

cout<<"\nthe union of 2 arrays is\n"; for(i1=0;i1<5;i1++) z[i1]=x[i1]; for(i2=0;i2<5;i2++) { for(j=0;j<5;j++) { if(z[j]==y[i2]); count++; if(count==0) { z[i1]=y[i2]; i1++; } } count=0; } for(j=0;j<i1;j++) cout<<endl<<z[j]; } Question: Write an interactive program to generate progress reports for the students of class XII (Science group) Answer: Interactive C program to generate progress reports of the students is as follows: #include<stdio.h> #include<conio.h> #define STU_NUM 3 #define NAME_LEN 100 #define SUB_NUM 3 struct student { int id; char name[NAME_LEN]; char group; int subject_marks[SUB_NUM]; int total; int percentage; }; void main() { int i, j, k, l, total, percentage;

struct student stud[STU_NUM]; clrscr(); //Get the required data from user. for(i=0; i<STU_NUM; i++) { j = i+1; printf("\n\nEnter the ID of student %d: ",j); scanf("%d",&stud[i].id); fflush(stdin); printf("\nEnter the name of student %d: ",j); gets(stud[i].name); fflush(stdin); printf("\nEnter the group selected by student %d (A/B): ",j); scanf("%c",&stud[i].group); fflush(stdin); for(k=0; k<SUB_NUM; k++) { l = k+1; printf("\nEnter the marks of subject %d for student %d: ",l,j); scanf("%d",&stud[i].subject_marks[k]); } fflush(stdin); } //Calculate total & percentage of the student. for(i=0; i<STU_NUM; i++) { total = 0; percentage = 0; for(k=0; k<SUB_NUM; k++) { total = total + stud[i].subject_marks[k]; } percentage = total / SUB_NUM; stud[i].total = total; stud[i].percentage = percentage; } //Display the progress report of the students. clrscr();

for(i=0; i<STU_NUM; i++) { printf("ID: %d \t Name: %s \t Group: %c \n", stud[i].id, stud[i].name, stud[i].group); for(k=0; k<SUB_NUM; k++) { l = k+1; printf("Subject %d: %d / 100 \n", l, stud[i].subject_marks[k]); } printf("Total: %d \t Percentage: %d \n", stud[i].total, stud[i].percentage); printf("\n----------------------------------------------------------------------\n\n"); } getch(); }

Question: Write an interactive program called DISTANCE CONVERTER that accepts the distance in millimetres / feet / miles / yards / kilometres and displays its equivalent in metres. Relationship between various units of distance is as follows: - 1 meter = 1000 mm - 1 meter = 3.28 feet - 1 mile = 1609 meter - 1 meter = 1.0936 yard - 1 kilometer = 1000 meter C program to convert distance from one unit to meter is as follows: #include<stdio.h> #include<conio.h> void main() { float distance_unit, distance_meter; int choice, cont = 1; clrscr(); printf("\nDistance Converter\n"); while(cont == 1) { clrscr();

printf("\n1.Enter distance in Millimeters.\n"); printf("\n2.Enter distance in Feet.\n"); printf("\n3.Enter distance in Miles.\n"); printf("\n4.Enter distance in Yards.\n"); printf("\n5.Enter distance in Kilometers.\n"); printf("\nEnter your choice: "); scanf("%d", &choice); printf("\nEnter distance: "); scanf("%f", &distance_unit); switch(choice) { case 1: //1 meter = 1000 mm distance_meter = distance_unit / 1000; break; case 2: //1 meter = 3.28 feet distance_meter = distance_unit / 3.28; break; case 3: //1 mile = 1609 meter distance_meter = distance_unit * 1609; break; case 4: //1 meter = 1.0936 yard distance_meter = distance_unit / 1.0936; break; case 5: //1 kilometer = 1000 meter distance_meter = distance_unit * 1000; break; default: printf("\nInvalid choice selected.\n"); break; } printf("\nEquivalent distance in Metres is: %f\n", distance_meter); printf("\nDo you want to continue (1/0)? "); scanf("%d", &cont); } getch();

} C program to encrypt a string. published by shafi on Mon, 09/03/2012 - 10:21 Question: Write a program to crypt its input according to a specified transformation transformation scheme. Your program should display the phrase before and after the substitutions have been made. Example: Original String: I know C programming. String after the transformation: K mpqy E rtqitcookpi. Answer: #include<stdio.h> #include<conio.h> void read_string(char str[]); void crypt(char str[], int transform_scheme); void main() { int transform_scheme; char str[100]; clrscr(); printf("\nEnter a string (Not more than 100 chars): "); //Read data from the user read_string(str); printf("\nOriginal String is: %s\n", str); //Read transformation scheme from the user printf("\nEnter the transaformation scheme (In Integer): "); scanf("%d", &transform_scheme); //Logic to convert the input string to encrypted form crypt(str, transform_scheme); printf("\nConverted String is: %s\n", str); getch(); } void read_string(char str[]) { int i, temp;

i = 0; while((temp = getchar()) != '\n') { str[i] = temp; i = i+1; } str[i] = '\0'; } void crypt(char str[], int transform_scheme) { int i,len; len = strlen(str); for(i=0; i<len; i++) { if(str[i] >= 'A' && str[i] <= 'Z') { str[i] = str[i] + transform_scheme; if(str[i] > 'Z') { str[i] = 'A' + ((str[i] - 'Z') - 1); } if(str[i] < 'A') { str[i] = 'Z' - (('A' - str[i]) - 1); } } if(str[i] >= 'a' && str[i] <= 'z') { str[i] = str[i] + transform_scheme; if(str[i] > 'z') { str[i] = 'a' + ((str[i] - 'z') - 1); } if(str[i] < 'a') { str[i] = 'z' - (('a' - str[i]) - 1); } } } }

Вам также может понравиться