C Programs - Problems
C Programs - Problems
#include<stdio.h> main()
int a,b,c; clrscr(); printf(Enter any three numbers=); scanf(%d%d%d,&a,&b,&c); if ((a>b) && (a>c))
{
printf(A is big); else if (b>c) printf(B is big);
} { }
{ }
getch();
Program to use switch statement:
#include<stdio.h> main()
int a,b,c,choice; clrscr(); printf(Choice of Demonstration\n); printf(1.Addition\n); printf(2.Subtraction\n); printf(3.Multiplication\n); printf(4.Division\n); printf(Enter the number corresponding to your choice=); scanf(%d,&choice); switch(choice)
case 1:
printf(A+B=%d\n,c); break; case 2: printf(Enter any two numbers=); scanf(%d%d,&a,&b); c=a-b; printf(A-B=%d\n,c); break; printf(Enter any two numbers=); scanf(%d%d,&a,&b); c=a*b; printf(A*B=%d\n,c); break; printf(Enter any two numbers=); scanf(%d%d,&a,&b); c=a/b; printf(A/B=%d\n,c); break; break;
case 3:
case 4:
default:
printf(You have entered the wrong number please try again\n);
getch();
}
Program for prime number or not #include<stdio.h> main() { int n, c = 2; printf("Enter a number to check if it is prime\n"); scanf("%d",&n); for ( c = 2 ; c <= n - 1 ; c++ ) { if (n%c == 0) { printf("%d is not prime.\n", n); break; } } if ( c == n ) printf("%d is prime.\n", n); }
#include<stdio.h> { int a=0,b=1,n; clrscr(); do { scanf(%d,&n); b=b*n; a++; }while(a<3) printf(Multiplication=%d,b); getch(); }
#include<stdio.h> main() { int a,b; long fact = 1; clrscr(); printf(Enter a number=); scanf(%d,&a); b=1; do { fact = fact*b; b++; }while(b<=a); printf(The factorial of %d is %ld, a,fact); getch(); }
Write a program to create Fibonacci series
#include<stdio.h> #include<conio.h> main() { int i,f1,f2,f3,n; clrscr(); printf("\n Enter a number:"); scanf("%d",&n); f1=1;f2=1; printf("%d%d",f1,f2); for(i=1;i<=n;i++) { f3=f1+f2; printf("%d",f3); f1=f2; f2=f3; } getch(); }
Output: Enter a number: 10 0 1 1 2 3 5 8 13 21 34
#include<stdio.h> main()
{
int a,b; clrscr(); printf(Enter the number\n=); scanf(%d,&a); for(b=1;b<=10;b++) { printf(%d x %d = %d\n,a,b,a*b); printf(\n); } getch();
}
Write a program to produce multiplication table from 1 to 10.Each up to 20.
#include<stdio.h> #include<conio.h> main() { int i,j,p; clrscr(); for(i=1;i<=10;i++) { clrscr(); printf("\n print%d table",i); for(j=1;j<=20;j++) { p=i*j; printf("\n %d*%d=%d", i,j,p); } getch(); } getch(); }
#include<stdio.h> void main() { int sum(int a,int b); int c; clrscr(); printf("\nEnter a & b values="); scanf("%d%d",&a,&b); c=add(a,b); printf("Result is=%d",c); } int add(int x,int y) { int c; c=a+b; return c; }
3 2 1 4 6
a[0] #include<stdio.h> a[1] void main () a[2] { a[3] int a[5]={5,8,3,1,6}; a[4] int i; clrscr(); for(i=0;i<5;i++) printf("\nArray of element%d is:%d", i+1,a[i]); getch(); }
Single Dimensional Arrays
#include<stdio.h> void main() { int i,a[5]; clrscr (); printf(\nEnter array elements=); for(int i=0,i<5;i++) scanf(%d,&a[i]); printf(\nEntered elements are=); for(i=0;i<5;i++) printf(\t%d, a[i]);
}
Reading & writing of 2 dimensional arrays
#include<stdio.h> void main() { int a[3][3],r,c; clrscr(); printf("\nEnter any nine elements:"); for(r=0;r<3;r++) { for(c=0;c<3;c++) scanf("%d",&a[r][c]); } printf("Given array elements are:"); for(r=0;r<3;r++) { for(c=0;c<3;c++) printf("%2d",a[r][c]); getch(); } }
Strings: strings are arrays of characters i.e. they are characters arranged one after another in memory. To mark the end of the string, C uses the null character. Strings in C are enclosed with in double quotes.