1. Write C Program to print 1,3,5, .... 20th term.
#include<stdio.h>
int main()
{
int i,a=1;
for(i=0;i<20;i++)
{
printf("%d\t",a);
a=a+2;
}
return 0;
}
Output:
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39
2. Write C Program to find sum of 2,4,...100th term.
#include<stdio.h>
int main()
{
int i,a=2,s=0;
for(i=0;i<100;i++)
{
s=s+a;
a=a+2;
}
printf("Sum of first 100 even numbers : %d",s);
return 0;
}
2.1 repeat same program to count total even numbers between 2 and 100.
#include<stdio.h>
int main()
{
int i,c=0;
for(i=2;i<=100;i=i+2)
{
c=c+1;
}
printf("Total number of even numbers bewteen 2 to 100 : %d",c);
return 0;
}
3. Write C Program to find sum of following series 1,1/2,1/3,1/4…. 1/nth term.
Algorithm to calculate sum of given series
1. Start 2. Set sum=0 3. Ask the value of n 4. Set i=1 5. Is i<=n? 5.1 If yes, calculate sum=sum+1/i and go to step 6 5.2 If no, go to step 7 6. Calculate i=i+1 and go to step 5 7. Print sum 8. Stop Flowchart to calculate sum of given seriesC Code to calculate sum of given series
#include <stdio.h> int main() { float n,i,sum=0; printf(" Enter the value of n: "); scanf("%f",&n); for(i=1;i<=n;i++) { sum=sum+1/i; } printf(" Sum = %0.2f ",sum); return 0; }Output
Enter the value of n: 3 Sum = 1.834. Write C Program to print factors of a number. E.g. factors of 6=1,2,3,6
5. Write C Program to print factorial value of a number. E.g. factorial value of 4 i.e. 4!=4x3x2x1=24
Algorithm to calculate the factorial of any number given by the user
1. Start 2. Input a number as n 3. Set f=1,i=n 4. Check is i >=1? 4.1 If yes, Calculate f=f*i and goto step 5 4.2 If no, goto step 6 5. Calculate i=i-1 and go to step 4 6. Print f 7. Stop Flowchart to calculate the factorial of any number given by the userC Code to calculate the factorial of any number given by the user
#include<stdio.h> int main() { int n,i,f=1; printf("Enter a number : "); scanf("%d",&n); for(i=n;i>=1;i--) { f=f*i; } printf("Factorial of %d = %d",n,f); return 0; }Output
Enter a number : 5 Factorial of 5 = 1206. Write C Program to print multiplication table of a number.
Algorithm to calculate and display the multiplication table of a number
1. Start 2. Input a number as n 3. Set i=1 4. Check is i <=10? 4.1 If yes, Calculate and display n*i and goto step 5 4.2 If no, goto step 6 5. Calculate i=i+1 and go to step 4 6. Stop Flowchart to multiplication table of a numberC Code to calculate and display the multiplication table of a number
#include <stdio.h> int main() { int i,n; printf("Enter the value of n : "); scanf("%d",&n); for(i=1;i<=10;i++) { printf("%d x %d = %d \n", n,i,n*i); } return 0; }Output
Enter the value of n : 2 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16 2 x 9 = 18 2 x 10 = 206.1 Repeat same program using nested loop
6.2 Repeat same program when you input different numbers and your program print their table. E.g. if you enter 2, it prints table of 2. Then if you enter 5 it prints table of 5 etc.
7. Write C Program to print Fibonacci series 1,1,2,3,5,… nth term.
Algorithm to display Fibonacci series having ‘n’ terms
- Start
- Set a=0,b=1, i=0
- Ask the value of ‘n’
- Is i<n?
4.1 If yes, Print "a" and calculate c=a+b . Also set a=b , b=c
4.2 If no, go to step 6 - Calculate i=i+1 and go to step 4
- Stop
Flowchart to display Fibonacci series having 'n' terms
C Code to display Fibonacci series having ‘n’ terms
#include <stdio.h> main() { int a=0,b=1,c,n,i; printf(" Enter the value of n : "); scanf("%d",&n); for(i=0;i<n;i++) { printf(" %d ",a); c=a+b; a=b; b=c; } }Output
Enter the value of n : 10 0 1 1 2 3 5 8 13 21 348. Write C Program to know a number is Armstrong or not.
#include<stdio.h>
int main()
{
int n,r,s=0,a;
printf("Enter a number : ");
scanf("%d",&n);
a=n;
while(n!=0)
{
r=n%10;
s=s+r*r*r;
n=n/10;
}
if(a==s)
printf(" Armstrong ");
else
printf("Not Armstrong");
return 0;
}
8.1 repeat same program to print all Armstrong numbers between 1 and 1000.
#include<stdio.h>
int main()
{
int n,r,s,i;
for (i=1;i<=1000;i++)
{
s=0;
n=i;
while(n!=0)
{
r=n%10;
s=s+r*r*r;
n=n/10;
}
if(i==s)
printf("%d\t",i);
}
return 0;
}
9. Write C Program to know a number is Palindrome or not.
#include<stdio.h>
int main()
{
int n,r,s=0,a;
printf("Enter a number : ");
scanf("%d",&n);
a=n;
while(n!=0)
{
r=n%10;
s=s*10+r;
n=n/10;
}
if(a==s)
printf(" Palindrome ");
else
printf("Not Palindrome");
return 0;
}
10. Write C Program to know a number is prime or not.
#include<stdio.h>
int main()
{
int n,i,c=0;
printf("Enter a number : ");
scanf("%d",&n);
for (i=1;i<=n;i++)
{
if(n%i==0)
c++;
}
if(c==2)
printf("%d is prime ",n);
else
printf(" %d is composite ",n);
return 0;
}
10.1 Write C Program to print all prime numbers between 1 and 100.
#include<stdio.h>
int main()
{
int i,j,count;
for(i=1;i<=100;i++)
{
count=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
count++;
}
if(count==2)
printf("%d\t",i);
}
return 0;
}
10.2 Write C Program to count total prime numbers between 1 and 100.
#include<stdio.h>
int main()
{
int i,j,count,c=0;
for(i=1;i<=100;i++)
{
count=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
count++;
}
if(count==2)
c=c+1;
}
printf("total prime numbers between 1 to 100 = %d",c);
return 0;
}
11.Write C Program to print following numeric or star patterns.
Q 11.1 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 | #include <stdio.h> int main() { int i,j; for(i=1;i<=5;i++) { for(j=1;j<=5;j++) { printf(" %d ",j); } printf("\n"); } return 0; } |
Q 11.2 * * * * * * * * * * * * * * * | #include <stdio.h> int main() { int i,j; for(i=5;i>=1;i--) { for(j=1;j<=i;j++) { printf(" * "); } printf("\n"); } return 0; } |
Q 11.3 1 1 1 1 1 2 2 2 2 3 3 3 4 4 5 | #include <stdio.h> int main() { int i,j; for(i=1;i<=5;i++) { for(j=i;j<=5;j++) { printf(" %d ",i); } printf("\n"); } return 0; } |
Q 11.4 5 4 4 3 3 3 2 2 2 2 1 1 1 1 1 | #include <stdio.h> int main() { int i,j; for(i=5;i>=1;i--) { for(j=5;j>=i;j--) { printf(" %d ",i); } printf("\n"); } return 0; } |
Q 11.5 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 | #include <stdio.h> int main() { int i,j; for(i=1;i<=4;i++) { for(j=0;j<5;j++) { printf(" %d ",i+j); } printf("\n"); } return 0; } |
Q 11.6 1 0 1 0 1 0 2 0 2 0 2 0 1 0 1 0 1 0 2 0 2 0 2 0 1 0 1 0 1 0 | #include <stdio.h> int main() { int i,j; for(i=1;i<=5;i++) { for(j=1;j<=3;j++) { if(i%2!=0) printf("10"); else printf("20"); } printf("\n"); } return 0; } |