CSE105 Programming problems (Mid-1) :
*
***
*****
*******
*********
We have shown five rows above, in the program you will be asked to enter the numbers of rows you want to print in the pyramid of stars.
12. Prime number program in c: c program for prime number, this code prints prime numbers using c programming language. To check whether a number is prime or not see another code below. Prime number logic: a number is prime if it is divisible only by one and itself. Remember two is the only even and also the smallest prime number. First few prime numbers are 2, 3, 5, 7, 11, 13, 17....etc. Prime numbers have many applications in computer science and mathematics. A number greater than one can be factorized into prime numbers, For example 540 = 22*33*51
13. Armstrong number c program: c programming code to check whether a number is armstrong or not. A number is armstrong if the sum of cubes of individual digits of a number is equal to the number itself. For example 371 is an armstrong number as 33 + 73 + 13 = 371. Some other armstrong numbers are: 0, 1, 153, 370, 407.
14. Armstrong number in c: This program prints armstrong number. In our program we ask the user to enter a number and then we use a loop from one to the entered number and check if it is an armstrong number and if it is then the number is printed on the screen. Remember a number is armstrong if the sum of cubes of individual digits of a number is equal to the number itself. For example 371 is an armstrong number as 33 + 73 + 13 = 371. Some other armstrong numbers are 0, 1, 153, 370, 407.
15. C program to print Floyd's triangle:- This program prints Floyd's triangle. Number of rows of Floyd's triangle to print is entered by the user. First four rows of Floyd's triangle are as follows :-
1
2 3
4 5 6
7 8 9 10
It's clear that in Floyd's triangle nth row contains n numbers.
- C program to perform basic arithmetic operations which are addition, subtraction, multiplication and division of two numbers. Numbers are assumed to be integers and will be entered by the user.
- Check whether an input alphabet is a vowel or not. Both lower-case and upper-case are checked.
- C program to add digits of a number: Here we are using modulus operator(%) to extract individual digits of number and adding them.
- C program to find hcf and lcm: The code below finds highest common factor and least common multiple of two integers. HCF is also known as greatest common divisor(GCD) or greatest common factor(gcf).
- C program to convert decimal to binary: c language code to convert an integer from decimal number system(base-10) to binary number system(base-2). Size of integer is assumed to be 32 bits. We use bitwise operators to perform the desired task. We right shift the original number by 31, 30, 29, ..., 1, 0 bits using a loop and bitwise AND the number obtained with 1(one), if the result is 1 then that bit is 1 otherwise it is 0(zero).
- C program to find nCr and nPr: This code calculate nCr which is n!/(r!*(n-r)!) and nPr = n!/(n-r)!
- This c program add n numbers which will be entered by the user. Firstly user will enter a number indicating how many numbers user wishes to add and then user will enter n numbers.
- Swap two numbers without using third variable.
- C Program to reverse a number :- This program reverse the number entered by the user and then prints the reversed number on the screen. For example if user enter 123 as input then 321 is printed as output. In our program we use modulus(%) operator to obtain the digits of a number. To invert number look at it and write it from opposite direction or the output of code is a number obtained by writing original number from right to left. To reverse or invert large numbers use long data type or long long data type if your compiler supports it, if you still have large numbers then use strings or other data structure.
- Palindrome number in c: A palindrome number is a number such that if we reverse it, it will not change. For example some palindrome numbers examples are 121, 212, 12321, -454. To check whether a number is palindrome or not first we reverse it and then compare the number obtained with the original, if both are same then number is palindrome otherwise not. C program for palindrome number is given below.
- These program prints various different patterns of numbers and stars. These codes illustrate how to create various patterns using c programming. Most of these c programs involve usage of nested loops and space. A pattern of numbers, star or characters is a way of arranging these in some logical manner or they may form a sequence. Some of these patterns are triangles which have special importance in mathematics. Some patterns are symmetrical while other are not. Please see the complete page and look at comments for many different patterns.
*
***
*****
*******
*********
We have shown five rows above, in the program you will be asked to enter the numbers of rows you want to print in the pyramid of stars.
12. Prime number program in c: c program for prime number, this code prints prime numbers using c programming language. To check whether a number is prime or not see another code below. Prime number logic: a number is prime if it is divisible only by one and itself. Remember two is the only even and also the smallest prime number. First few prime numbers are 2, 3, 5, 7, 11, 13, 17....etc. Prime numbers have many applications in computer science and mathematics. A number greater than one can be factorized into prime numbers, For example 540 = 22*33*51
13. Armstrong number c program: c programming code to check whether a number is armstrong or not. A number is armstrong if the sum of cubes of individual digits of a number is equal to the number itself. For example 371 is an armstrong number as 33 + 73 + 13 = 371. Some other armstrong numbers are: 0, 1, 153, 370, 407.
14. Armstrong number in c: This program prints armstrong number. In our program we ask the user to enter a number and then we use a loop from one to the entered number and check if it is an armstrong number and if it is then the number is printed on the screen. Remember a number is armstrong if the sum of cubes of individual digits of a number is equal to the number itself. For example 371 is an armstrong number as 33 + 73 + 13 = 371. Some other armstrong numbers are 0, 1, 153, 370, 407.
15. C program to print Floyd's triangle:- This program prints Floyd's triangle. Number of rows of Floyd's triangle to print is entered by the user. First four rows of Floyd's triangle are as follows :-
1
2 3
4 5 6
7 8 9 10
It's clear that in Floyd's triangle nth row contains n numbers.
## Solution for problem 1 :
#include<stdio.h>
main()
{
int a,b,c,n;
scanf("%d",&i) ;
printf("enter the first number.\n");
scanf("%d",&a);
printf("enter the second number.\n");
scanf("%d",&b);
printf("press 1 for add, 2 for sub, 3 for div, 4 for mult \n");
scanf("%d",&n);
if (n==1)
{
c=a+b;
}
else if (n==2)
{
c=a-b;
}
else if (n==3)
{
c=a/b;
}
else if (n==4)
{
c=a*b;
}
else
{
printf("wrong input\n");
return0;
};
printf("\nThe result is: %d\n",c);
return 0;
}
**There may have some mistakes**
#include<stdio.h>
main()
{
int a,b,c,n;
scanf("%d",&i) ;
printf("enter the first number.\n");
scanf("%d",&a);
printf("enter the second number.\n");
scanf("%d",&b);
printf("press 1 for add, 2 for sub, 3 for div, 4 for mult \n");
scanf("%d",&n);
if (n==1)
{
c=a+b;
}
else if (n==2)
{
c=a-b;
}
else if (n==3)
{
c=a/b;
}
else if (n==4)
{
c=a*b;
}
else
{
printf("wrong input\n");
return0;
};
printf("\nThe result is: %d\n",c);
return 0;
}
**There may have some mistakes**
## Solution for problem 2:
#include <stdio.h>
int main()
{
printf("Please enter the letter\n");
char k;
scanf("%c",&k);
if(k=='a'||k=='A'||k=='e'||k=='E'||k=='i'||k=='I'||k=='o'||k=='O'||k=='u'||k=='U')
{printf("vowel");}
else {printf("not vowel");}
return 0;
}
#include <stdio.h>
int main()
{
printf("Please enter the letter\n");
char k;
scanf("%c",&k);
if(k=='a'||k=='A'||k=='e'||k=='E'||k=='i'||k=='I'||k=='o'||k=='O'||k=='u'||k=='U')
{printf("vowel");}
else {printf("not vowel");}
return 0;
}
## Solution for problem 3:
#include <stdio.h>
int main()
{
int n, sum = 0, remainder;
printf("Enter an integer\n");
scanf("%d",&n);
while(n != 0)
{
remainder = n % 10;
sum = sum + remainder;
n = n / 10;
}
printf("Sum of digits of entered number = %d\n",sum);
return 0;
}
#include <stdio.h>
int main()
{
int n, sum = 0, remainder;
printf("Enter an integer\n");
scanf("%d",&n);
while(n != 0)
{
remainder = n % 10;
sum = sum + remainder;
n = n / 10;
}
printf("Sum of digits of entered number = %d\n",sum);
return 0;
}
## Solution for problem 4:
#include <stdio.h>
int main()
{
printf("enter two integer numbers\n");
int a,b,n;
scanf("%d",&a);
scanf("%d",&b);
for(n=a;n<=a;n--)
{
if(a%n==0)
{
if(b%n==0)
{
printf("\nThe highest common factor is:%d\n\n",n);
break;
}
else;
}
else;
};
for (n=a;n>=a;n++)
{
if(n%a==0)
{
if(n%b==0)
{
printf("The lowest common multiple is:%d\n\n",n);
break;
}
else;
}
else;
}
return 0;
}
#include <stdio.h>
int main()
{
printf("enter two integer numbers\n");
int a,b,n;
scanf("%d",&a);
scanf("%d",&b);
for(n=a;n<=a;n--)
{
if(a%n==0)
{
if(b%n==0)
{
printf("\nThe highest common factor is:%d\n\n",n);
break;
}
else;
}
else;
};
for (n=a;n>=a;n++)
{
if(n%a==0)
{
if(n%b==0)
{
printf("The lowest common multiple is:%d\n\n",n);
break;
}
else;
}
else;
}
return 0;
}
## Solution for problem 5:
পারিনা :p
পারিনা :p
## Solution for problem 6:
#include <stdio.h>
int main()
{
int n=0,r=0;
int t,p1,g1,p2,g2,p3,g3,m1,m2,m3,q1,q2;
int f=n-r;
printf("enter the value of n in nCr\n");
scanf("%d",&q1);
printf("enter the value of r in nCr\n");
scanf("%d",&q2);
if(q1<q2||q1==0||q2==0)
{
printf("wrong input");
}
else
{
while (n>=0&&n<=q1)
{
p1=n;
if (p1==1)
{
g1=p1;
}
else
{
g1=g1*p1;
};
n++;
};
while (r>=0&&r<=q2)
{
p2=r;
if (p2==1)
{
g2=p2;
}
else
{
g2=g2*p2;
};
r++;
};
while (f>=0&&f<=n-r)
{
p3=f;
if (p3==1)
{
g3=p3;
}
else
{
g3=g3*p3;
};
f++;//
};
m1=g2*g3;
m2=g1/m1;
m3=g1/g3;
printf("nCr:%d\n",m2);
printf("nPr:%d\n",m3);
}
return 0;
}
নিজের ইচ্ছামত করলাম। না বুঝলে যোগাযোগ কইরেন; বুঝাইয়া দিবোনে ।।
#include <stdio.h>
int main()
{
int n=0,r=0;
int t,p1,g1,p2,g2,p3,g3,m1,m2,m3,q1,q2;
int f=n-r;
printf("enter the value of n in nCr\n");
scanf("%d",&q1);
printf("enter the value of r in nCr\n");
scanf("%d",&q2);
if(q1<q2||q1==0||q2==0)
{
printf("wrong input");
}
else
{
while (n>=0&&n<=q1)
{
p1=n;
if (p1==1)
{
g1=p1;
}
else
{
g1=g1*p1;
};
n++;
};
while (r>=0&&r<=q2)
{
p2=r;
if (p2==1)
{
g2=p2;
}
else
{
g2=g2*p2;
};
r++;
};
while (f>=0&&f<=n-r)
{
p3=f;
if (p3==1)
{
g3=p3;
}
else
{
g3=g3*p3;
};
f++;//
};
m1=g2*g3;
m2=g1/m1;
m3=g1/g3;
printf("nCr:%d\n",m2);
printf("nPr:%d\n",m3);
}
return 0;
}
নিজের ইচ্ছামত করলাম। না বুঝলে যোগাযোগ কইরেন; বুঝাইয়া দিবোনে ।।
## Solution for problem 7:
#include <stdio.h>
int main()
{
int n,p,u,h;
p=0;
printf("enter the amount of number that you want to add\n");
scanf("%d",&h);
printf("\n\nnow enter the numbers\n");
for(n=1;n<=h;n++)
{
scanf("%d",&u);
if (p==0)
{
p=u;
}
else {p=p+u;};
printf("Your current result is:%d\n",p);
};
printf("\n\nYour final result is:%d\n",p);
return 0;
}
#include <stdio.h>
int main()
{
int n,p,u,h;
p=0;
printf("enter the amount of number that you want to add\n");
scanf("%d",&h);
printf("\n\nnow enter the numbers\n");
for(n=1;n<=h;n++)
{
scanf("%d",&u);
if (p==0)
{
p=u;
}
else {p=p+u;};
printf("Your current result is:%d\n",p);
};
printf("\n\nYour final result is:%d\n",p);
return 0;
}
## Solution for problem 8:
#include <stdio.h>
int main()
{
int a, b;
printf("Enter two integers to swap\n");
scanf("%d%d", &a, &b);
a = a + b;
b = a - b;
a = a - b;
printf("a = %d\nb = %d\n",a,b);
return 0;
}
#include <stdio.h>
int main()
{
int a, b;
printf("Enter two integers to swap\n");
scanf("%d%d", &a, &b);
a = a + b;
b = a - b;
a = a - b;
printf("a = %d\nb = %d\n",a,b);
return 0;
}
## Solution for problem 9:
#include <stdio.h>
int main()
{
int n, a;
a=0;
printf("Enter a number to reverse\n");
scanf("%d",&n);
while (n != 0)
{
a = a * 10;
a = a + n%10;
n = n/10;
};
printf("Reverse of entered number is = %d\n", a);
return 0;
}
#include <stdio.h>
int main()
{
int n, a;
a=0;
printf("Enter a number to reverse\n");
scanf("%d",&n);
while (n != 0)
{
a = a * 10;
a = a + n%10;
n = n/10;
};
printf("Reverse of entered number is = %d\n", a);
return 0;
}
## Solution for problem 10:
#include <stdio.h>
int main()
{
int n, a, b;
printf("Enter a number to check if it is a palindrome or not\n");
scanf("%d",&n);
a=0;
b = n;
while( b != 0 )
{
a = a * 10;
a = a + b%10;
b = b/10;
}
if ( n == a )
printf("%d is a palindrome number.\n", n);
else
printf("%d is not a palindrome number.\n", n);
return 0;
}
#include <stdio.h>
int main()
{
int n, a, b;
printf("Enter a number to check if it is a palindrome or not\n");
scanf("%d",&n);
a=0;
b = n;
while( b != 0 )
{
a = a * 10;
a = a + b%10;
b = b/10;
}
if ( n == a )
printf("%d is a palindrome number.\n", n);
else
printf("%d is not a palindrome number.\n", n);
return 0;
}
## Solution for problem 12:
#include <stdio.h>
main()
{
int n,c;
printf("Enter a number\n");
scanf("%d", &n);
if ( n == 1 )
{
printf("Unique number.\n");
return 0;
}
else if ( n == 2 )
{
printf("Prime number.\n");
return 0;
}
else
{for ( c=2;c<=n-1;c++ )
{
if ( n % c == 0 )
break;
}}
if ( c != n )
printf("Not prime.\n");
else
printf("Prime number.\n");
return 0;
}
#include <stdio.h>
main()
{
int n,c;
printf("Enter a number\n");
scanf("%d", &n);
if ( n == 1 )
{
printf("Unique number.\n");
return 0;
}
else if ( n == 2 )
{
printf("Prime number.\n");
return 0;
}
else
{for ( c=2;c<=n-1;c++ )
{
if ( n % c == 0 )
break;
}}
if ( c != n )
printf("Not prime.\n");
else
printf("Prime number.\n");
return 0;
}
## Solution for problem 13:
#include <stdio.h>
int main()
{
int a,c,d,e,m,i,t,p;
printf("Enter the number\n");
scanf("%d",&a);
e=0;
m=a;
while(a!=0)
{
c=a%10;
d=c*c*c;
a=a/10;
e=e+d;
};
if (e==m)
{
printf("armstrong number\n");
}
else
{
printf("not armstrong number\n");
};
return 0;
}
#include <stdio.h>
int main()
{
int a,c,d,e,m,i,t,p;
printf("Enter the number\n");
scanf("%d",&a);
e=0;
m=a;
while(a!=0)
{
c=a%10;
d=c*c*c;
a=a/10;
e=e+d;
};
if (e==m)
{
printf("armstrong number\n");
}
else
{
printf("not armstrong number\n");
};
return 0;
}
## Solution for problem 14:
#include <stdio.h>
int main()
{
int a,c,d,e,m,i,t,p,l,u;
printf("Enter the lowest number\n");
scanf("%d",&l);
printf("Enter the highest number\n");
scanf("%d",&u);
for(i=l;i<=u;i++)
{
a=i;
e=0;
m=a;
while(a!=0)
{
c=a%10;
d=c*c*c;
a=a/10;
e=e+d;
};
if (e==m)
{
printf("%d-armstrong number\n",m);
}
else
{
printf("");
};
};
return 0;
}
#include <stdio.h>
int main()
{
int a,c,d,e,m,i,t,p,l,u;
printf("Enter the lowest number\n");
scanf("%d",&l);
printf("Enter the highest number\n");
scanf("%d",&u);
for(i=l;i<=u;i++)
{
a=i;
e=0;
m=a;
while(a!=0)
{
c=a%10;
d=c*c*c;
a=a/10;
e=e+d;
};
if (e==m)
{
printf("%d-armstrong number\n",m);
}
else
{
printf("");
};
};
return 0;
}
## Solution for problem 15:
#include <stdio.h>
int main()
{
int n,r,g,q,i,p,v;
int e=0;
printf("enter the number of rows.");
scanf("%d",&v);
for(n=0;n<=v;n++)
{
e=e+n;
g=e;
for(i=q+1;i<=g;i++)
{
printf("%d",i);
};
printf("\n",e);
q=e;
};
return 0;
}
#include <stdio.h>
int main()
{
int n,r,g,q,i,p,v;
int e=0;
printf("enter the number of rows.");
scanf("%d",&v);
for(n=0;n<=v;n++)
{
e=e+n;
g=e;
for(i=q+1;i<=g;i++)
{
printf("%d",i);
};
printf("\n",e);
q=e;
};
return 0;
}
For more C problems