2. Print the following shapes using the loop construct of C programming.
#include<stdio.h>int main(){int A;int B;for(A=1;A<=6;A++){for(B=1;B<=A;B++){printf("*");}printf("\n");}return 0;}
#include<stdio.h>int main(){int A;int B;for(A=1;A<=6;A++){for(B=1;B<=6;B++){printf("*");}printf("\n");}return 0;}
#include<stdio.h>int main(){int A;int B;int C;for(A=6;A>=1;A--){for(B=1;B<=A;B++){printf("*");}printf("\n");}return 0;}
#include<stdio.h>int main(){int A;int B;int C;for(A=1;A<=6;A++){for(B=6;B>=A;B--){printf(" ");}for(C=1;C<=A;C++)printf("*");printf("\n");}return 0;}
#include<stdio.h>int main(){int A;int B;int C;for(A=1;A<=6;A++){for(B=2;B<=A;B++){printf(" ");}for(C=6;C>=A;C--){printf("*");}printf("\n");}return 0;}
#include<stdio.h>int main(){int A;int B;for(A=1;A<=5;A++){for(B=1;B<=A;B++){printf("%i",B);}printf("\n");}return 0;}
3. Find the minimum and maximum of sequence of 10 numbers.
#include<stdio.h>int main(){int x,max=0,min=0,num;printf("Enter 10 numbers:\n");scanf("%i",&num);min=num;max=num;for(x=1;x<=9;x++){scanf("%i",&num);if(num>max){max=num;}if(num<min)min=num;}printf("Maximum value is:%i\n",max);printf("Maximum value is:%i",min);return 0;}
4. Find the total and average of a sequence of 10 numbers.
#include <stdio.h>int main (){int x, number;float average;int total = 0;printf("Enter 10 numbers\n");for (x=1; x<=10; x++){scanf("%i", &number);total = total+number;}average = total /10.0;printf("Total : %i\n",total);printf("Average : %.2f",average);return 0;}
5. Write a program to generate and display a table of n and n 2 , for integer values of n ranging from 1 to 10. Be certain to print appropriate column headings.
#include<stdio.h>int main(){int x,c;printf(" n n^2\n\n");for(x=1;x<=10;x++){printf("%2i %4i\n",x,x*x);}return 0;}
6. A triangular number can also be generated by the formula
triangularNumber = n (n + 1) / 2
for any integer value of n. For example, the 10th triangular number, 55, can be generated by substituting 10 as the value for n in the preceding formula. Write a program that generates a table of triangular numbers using the preceding formula.
#include<stdio.h>int main(){int n,x;int triangularNumber=0;printf("Enter number:");scanf("%i",&n);printf("number\ttriangular number\n");printf("------\t------------------\n");for(x=1;x<=n;x++){triangularNumber=x*(x+1)/2;printf("%3i %5i\n",x,triangularNumber);}return 0;}
#include<stdio.h>int main(){int x;unsigned long long total = 1;printf("Enter a non-negative number:");scanf("%i",&x);if(x<0){printf("Factorial is not defined for negative numbers.\n");}else{printf("%i! = ",x);for(x;x>=2;x--){total*=x;printf("%i*",x);}printf("1 = %llu\n",total);}return 0;}
8. Write a program to generate and print a table of the first 10 factorials.
#include<stdio.h>int main(){int total=1,x;printf("First 10 factorials\n\n");for(x=1;x<=10;x++){total=total*x;printf("%2i%8i\n",x,total);}return 0;}
9. Display the n terms of harmonic series and their sum.
1 + 1/2 + 1/3 + 1/4 + 1/5 ... 1/n terms
Test Data :
Input the number of terms : 5
Expected Output : 1/1 + 1/2 + 1/3 + 1/4 + 1/5 +
Sum of Series upto 5 terms : 2.283334
#include<stdio.h>int main(){int x,n;float Sum=0;printf("Enter the number of terms:");scanf("%i",&n);for(x=1;x<=n;x++){Sum += 1.0/x;printf("1/%i + ",x);}printf("\nSum of series up to 5 terms : %.6f",Sum);return 0;}
10. Write a program to generate students repot as shown below.
#include<stdio.h>int main(){int x,n;int Index[100];int m[100];int p[100];int c[100];int total[100];float Average[100];char Name[100][100];char Grade[100];printf("How many students do you entered:");scanf("%i",&n);for(x=0;x<n;x++){printf("\nStudent 0%i\n",x+1);printf("-----------\n");printf("Enter Index number:");scanf("%i",&Index[x]);printf("Enter Name:");scanf("%s",&Name[x]);printf("Enter Maths marks:");scanf("%i",&m[x]);printf("Enter Physics marks:");scanf("%i",&p[x]);printf("Enter Chemistry marks:");scanf("%i",&c[x]);total[x]=m[x]+p[x]+c[x];Average[x] = total[x]/3.0;if(Average[x]>=90)Grade[x] = 'A';else if(Average[x]>=80)Grade[x] = 'B';else if(Average[x]>=70)Grade[x] = 'C';else if(Average[x]>=60)Grade[x] = 'D';else if(Average[x]>=40)Grade[x] = 'E';elseGrade[x] = 'F';}printf("\nIndex Number Name Maths Physics Chemistry Total Average Grade\n");printf("----------------------------------------------------------------------------------------\n");for(x=0;x<n;x++){printf("%4i\t",Index[x]);printf("\t%s\t",Name[x]);printf("%3i\t",m[x]);printf("%3i\t",p[x]);printf("%3i\t\t",c[x]);printf("%3i\t ",total[x]);printf("%3.2f\t ",Average[x]);printf("%c\n",Grade[x]);}return 0;}
11. Assume a village has 20 houses. Input electricity unit charges and calculate total electricity bill according to the following criteria:
ï‚· For first 50 units Rs. 0.50/unit
ï‚· For next 100 units Rs. 0.75/unit
ï‚· For next 100 units Rs. 1.20/unit
ï‚· For unit above 250 Rs. 1.50/unit
An additional surcharge of 20% is added to the bill
Post a Comment
0Comments