Learn programming logic to write advanced programs on for loop.
- 047. Write a program to accept a number from user and print it’s factorial in C language
Eg: factorial of 5 is:-
5! = 5 * 4 * 3 * 2 * 1=120#include<stdio.h> #include<conio.h> main() { int i,fact=1,n; clrscr(); printf("Enter number..."); scanf("%d",&n); for(i=1;i<=n;i++) fact=fact*i; printf("facttorialof the given number is...%d",fact); getch(); }
- 048. Write a program to accept a number from user and print if it is prime or not in C language
#include<conio.h> #include<process.h> main() { int i,n; clrscr(); printf("Enter number..."); scanf("%d",&n); for(i=2;i<=n/2;i++) { if(n%i==0) { printf("Not Prime"); getch(); exit(0); } } printf("Prime "); getch(); }
- 049. Write a program to accept a number and print prime numbers between 2 and n in C language
#include<stdio.h> #include<conio.h> #include<process.h> main() { int i,flag=1,n,newn; clrscr(); printf("Enter number..."); scanf("%d",&n); for(newn=2;newn<=n;newn++) { flag=1; for(i=2;i<=newn/2 ;i++) { if(newn%i==0) { flag=0; break; } } if(flag==1) printf("%d ",newn); } getch(); }
- 050. Write a program to accept a number and print fibonacci series upto that level in C language
#include<stdio.h> #include<conio.h> main() { int pre=1,cur=1,temp,i,n; clrscr(); printf("Enter number..."); scanf("%d",&n); printf("%d\t%d",pre,cur); for(i=3;i<=n;i++) { temp=cur; cur=pre+cur; pre=temp; printf("\t%d",cur); } getch(); }
- 051. Write a program to print digits, alphabets in capital and lower case in C language
#include<stdio.h> #include<conio.h> main() { int i; clrscr(); for(i=65;i<=90;i++) printf("%c ",i); printf("\n\n\n\n"); for(i=97;i<=122;i++) printf("%c ",i); printf("\n\n\n\n"); for(i=48;i<=57;i++) printf("%c\t",i); getch(); }
- 052. Write a program to accept a number n from user and Add n terms of the series in C language
1/2! + 2/3! + 3/4! + 4/5! + 5/6! + ………
#include<stdio.h> #include<conio.h> main() { int i,j,n; float sum=0,fact=1; clrscr(); printf("Enter number...."); scanf("%d",&n); for(i=1;i<=n;i++) { fact=1; for(j=1;j<=i+1;j++) fact=fact*j; sum=sum+i/fact; } printf("Sum of the series....%f",sum); getch(); }
- 053. Write a program to read n numbers (Xi) from the user and print out their average and standard deviation. Formulae are
Average = (Sum of Xi)/n
#include<stdio.h> #include<conio.h> main() { int n,i,newn,sum_avg=0; float avg; clrscr(); printf("Enter number : "); scanf("%d",&n); printf("Enter numbers..."); for(i=1;i<=n;i++) { scanf("%d",&newn); sum_avg=sum_avg+newn; } avg=sum_avg/(float)n; //Type casting. printf("\nAverage = %f",avg); getch(); }
- 054. Write a program to print out ASCII chart on a single screen (all 256 characters from 0 to 255) in a tabular form. The ASCII code should be followed by the corresponding character in C language
#include<stdio.h> #include<conio.h> main() { int i; clrscr(); for(i=0;i<=255;i++) printf("%d-%c\t",i,i); getch(); }
- 055. Write a program to print following output
******************************* * * * * * * *******************************
#include<stdio.h> #include<conio.h> main() { int i,j; clrscr(); for(i=1;i<=80;i++) printf("*"); for(i=2;i<=23;i++) { printf("*"); for(j=2;j<=79;j++) printf(" "); printf("*"); } for(i=1;i<=80;i++) printf("*"); getch(); }
- 056. Write a program to print following output
#include<stdio.h> #include<conio.h> main() { int i, j; clrscr(); printf("╔"); for(i=2;i<=79;i++) printf("═"); printf("╗"); for(i=2;i<=24;i++) { printf("║"); for(j=2;j<=79;j++) printf(" "); printf("║"); } printf("╚"); for(i=2;i<=79;i++) printf("═"); printf("╝"); getch(); }
- 057. Write a program to accept a single value interger from user and print that integer in words in C language
#include<stdio.h> #include<conio.h> main() { int n; clrscr(); printf("Enter number..."); scanf("%1d",&n); //%1d to scan single integer switch(n) { case 0 : printf("Zero");break; case 1 : printf("One");break; case 2 : printf("Two");break; case 3 : printf("Three");break; case 4 : printf("Four");break; case 5 : printf("Five");break; case 6 : printf("Six");break; case 7 : printf("Seven ");break; case 8 : printf("Eigth ");break; case 9 : printf("Nine ");break; } getch(); }