• No results found

SET-4 SOLUTION. 1. Write a C program to display a multiplication table of entered any number up to 10 terms using nested loop. terms. or not.

N/A
N/A
Protected

Academic year: 2021

Share "SET-4 SOLUTION. 1. Write a C program to display a multiplication table of entered any number up to 10 terms using nested loop. terms. or not."

Copied!
6
0
0

Loading.... (view fulltext now)

Full text

(1)

SET-4 SOLUTION

1. Write a C program to display a multiplication table of entered any number up to 10 terms using nested loop.

#include <stdio.h>

int main() {

int n, i, j, p;

printf("Enter any number:");

scanf("%d", &n);

for(i=1; i<=n; i++) {

printf("The multiplication table of %d:\n", i);

for(j=1; j<=10; j++) {

p=n*j;

printf("%dx%d=%d\n",n, j, p);

} }

return 0;

}

2.

Write a C program to generate a series 1, 7, 8, 15, 23, 38, 61,…. up to nth terms.

#include <stdio.h>

int main() {

int n, i, t1=1, t2=7, t;

printf("Enter the number of terms:");

scanf("%d", &n);

printf("The terms of a sequence are:%d", t1);

for(i=1; i<n; i++) {

printf("\t%d", t2);

t=t1+t2;

t1=t2;

t2=t;

}

return 0;

}

3.

Write a C program to input a word and determine whether it is palindrome or not.

#include <stdio.h>

#include <string.h>

(2)

int main() {

char word[20], wordrev[20];

printf("Enter any word:");

scanf("%s", word);

strcpy(wordrev, word);

if(strcmp(wordrev, strrev(word))==0) printf("%s is palindrome", wordrev);

else

printf("%s is not palindrome", wordrev);

return 0;

}

4.

Write a C program to read the age of 10 persons and arrange it in ascending order.

#include <stdio.h>

int main() {

int a[10], i, j, temp;

printf("Enter the age of any 10 persons:");

for(i=0; i<10; i++) {

scanf("%d", &a[i]);

}

for(i=0;i<10; i++) {

for(j=i+1; j<10; j++) {

if(a[i]>a[j]) {

temp=a[i];

a[i]=a[j];

a[j]=temp;

} }

}

printf("The age in ascending order:\n");

for(i=0; i<10; i++)

printf("%d\n", a[i]);

return 0;

}

5.

Differentiate between ‘while’ and ‘do...while’ loop.

Ans:

Both while and do-while loop are used when the number of repetitions or loop is not known in advance by the programmer.

(3)

The differences between while and do-while loop are:

while loop do...while loop

It is an entry-controlled loop statement (i.e., test condition is evaluated first and body of loop is executed only if test condition is true).

It is an exit-controlled loop statement (i.e., the body of the loop executed first and the test condition is evaluated for repetition of next time).

The body of loop may not be executed at all if the test condition is false at the very first attempt.

The body of loop is always executed at least once even if the test condition is false.

It doesn’t generate output if the test condition is false or invalid.

It generates output at least once even if the test condition is false.

The syntax of while loop is:

while (test_condition) {

//Body of while loop }

The syntax of do-while loop is:

do {

//Body of while loop }while(test_condition);

Example:

#include <stdio.h>

int main() {

int i=1;

while(i<10) {

printf("%d", i);

i=i+2;

} }

Example:

#include <stdio.h>

int main() {

int i=1;

do {

printf("%d", i);

i=i+2;

}while(i<10);

}

6.

Write a C program to input a natural number and find its factorial.

#include <stdio.h>

int main() {

int n, i, fact=1;

printf("Enter any number:");

scanf("%d", &n);

for(i=n; i>=1; i--) {

fact=fact*i;

}

printf("Factorial of %d is %d", n, fact);

return 0;

}

(4)

7.

Write a program to read the elements of the two matrices of order 3x3 and perform its addition.

#include <stdio.h>

int main() {

int a[3][3], b[3][3], sum[3][3], i, j;

printf("Enter the elements of first matrix:\n");

for(i=0; i<3; i++) {

for(j=0;j<3; j++) {

scanf("%d", &a[i][j]);

} }

printf("Enter the elements of second matrix:\n");

for(i=0; i<3; i++) {

for(j=0;j<3; j++) {

scanf("%d", &b[i][j]);

} }

printf("The matrix after addition is:\n");

for(i=0; i<3; i++) {

for(j=0;j<3; j++) {

sum[i][j]=a[i][j]+b[i][j];

printf("%d\t", sum[i][j]);

}

printf("\n");

}

return 0;

}

(5)

8.

Write a program that read successive records from the new file using file handling and display each record on the screen in an appropriate formatted form.

#include <stdio.h>

int main() {

char name[20], c;

int roll, marks, i, n;;

FILE *fptr;

printf("Enter the number of students:");

scanf("%d", &n);

fptr=fopen("student.txt", "w");

for(i=1; i<=n; i++) {

printf("Enter roll, name and marks:");

scanf("%d%s%d", &roll, name, &marks);

fprintf(fptr,"\n%d\t%s\t%d", roll, name, marks);

}

fclose(fptr);

fptr=fopen("Student.txt", "r");

printf("\n---\n");

printf("roll\tName\tMarks\n");

do {

fscanf(fptr, "%d%s%d", &roll, name, &marks);

printf("\n%d\t%s\t%d", roll, name, marks);

}while(c=fgetc(fptr)!=EOF);

fclose(fptr);

return 0;

}

9.

Write a C program to input n numbers and sort them in descending order and display them.

#include <stdio.h>

int main()

#define size 100 {

int a[size], i, j, n, temp;

printf("Enter number of data items you want to arrange:");

scanf("%d", &n);

printf("Enter any %d numbers:", n);

(6)

for(i=0; i<n; i++) {

scanf("%d", &a[i]);

}

for(i=0;i<n; i++) {

for(j=i+1; j<n; j++) {

if(a[i]<a[j]) {

temp=a[i];

a[i]=a[j];

a[j]=temp;

} }

}

printf("The age in ascending order:\n");

for(i=0; i<n; i++)

printf("%d\n", a[i]);

return 0;

}

10.

Write a program to develop the following pattern using nested loop.

1 2 2 3 3 3 4 4 4 4 5 5 5 5 5

#include <stdio.h>

int main() {

int i, j;

for(i=1; i<=5; i++) {

for(j=1; j<=i; j++) {

printf("%d\t", i);

}

printf("\n");

}

return 0;

}

References

Related documents

• After the initialize statement, the condition part of the loop will be executed if the condition becomes true then body of the loop will be executed otherwise the loop will

We implemented this using a Symmetrix VMAX 10K storage array together with an existing VNX5300 storage array to form a bi-directional disaster recovery solution.. We achieved this

Aquatic Heteroptera Of the Lake Manguao Catchment, Palawan Aquatic Heteroptera Of the Lake Manguao Catchment, Palawan and New Rank Of Rhagovelia kawakamii hoberlandti Hungerford

Pilates as body, mind and spirit exercises is a powerful body conditioning to improves spirit exercises is a powerful body conditioning to improves muscles strength, flexibility,

“You Stepped Out Of A Dream,” from the studio album, features Steve mixing it up on the brushes with a uniquely modern jazz approach in the context of a sax, bass, and

The regiment as a whole was awarded the French military honor, the Croix de Guerre, and 171 of the officers and troops received individual citations for bravery, more than any

The upward swimming velocity of competent non-cue exposed larvae showed opposite 311 . trends with larval age in the two

On the other hand, operation (ii) is used to test whether the condition is true or false. If the condition is true, the program executes the body of the loop and then the value of