Finding out a number is even or odd by using switch case,Finding out a natural numerical sum using for loop,Finding out a natural numerical multiple using for loop,Take an integer from the user determine that number is prime or not prime.

 Problem : Finding out a number is even or odd by using switch case.


Solution: 

Finding out a number is even or odd by using switch case.


Introduction: In this problem, we found out one a number is even or odd by using switch case

through C programming by using codeblocks. Each instruction in a C program is written as a 

separate statement. C has no specific rules for the position at which a statement is to be written in 

a given line. That’s why it is often called a free-form language. #include is a preprocessor directive. 

Then we used int main ( ) with a {}. We used it to start our programming We used printf ( ) to 

show data, information etc. on the screen. We used scanf for taking input from the user. We used 

int here to two one variables. Here, n for the number which is given by user.


Code: 

#include<stdio.h>

int main()

{

 int n,r;

 printf("Enter a number:");

 scanf("%d",&n);

 r=n%2;

 switch(r){

 case 0:

 printf("Even");

 break;

 case 1:

 printf("odd");

 break;

 }

}


Output:

Finding out a number is even or odd by using switch case.


Discussion: We successfully run the program by using codeblocks. Here, we took n= 5 and it 

was an odd number which was seen on the display automatically and the result was absolutely 

correct .So, we could see that by using C programming we can find any number weither it was 

even or odd without any possibility of any kind of error.


Problem : Finding out a natural numerical sum using for loop.


Solution: 

Finding out a natural numerical sum using for loop.



Introduction: In this problem, we found out one a number is even or odd by using switch case

through C programming by using codeblocks. Each instruction in a C program is written as a 

separate statement. C has no specific rules for the position at which a statement is to be written in 

a given line. That’s why it is often called a free-form language. #include is a preprocessor directive. 

Then we used int main ( ) with a {}. We used it to start our programming We used printf ( ) to 

show data, information etc. on the screen. We used scanf for taking input from the user. We used 

int here to two variables. Here, we have write a condition in for loop.


Code: 


#include<stdio.h>

int main(){

int i,n=0;

for(i=1;i<=100;i++)

 {

printf("%d+",i);

 n=n+i;

}

printf("total %d",n);

return 0;}


Output:

Finding out a natural numerical sum using for loop.



Discussion: We successfully run the program by using codeblocks. The result was seen on the 

display automatically and the result was absolutely correct. So, we could see that by using C 

programming we can find any numerical sum without any possibility of any kind of error.



Problem : Finding out a natural numerical multiple using for loop.




Introduction: In this problem, we found out one a number is even or odd by using switch case

through C programming by using codeblocks. Each instruction in a C program is written as a 

separate statement. C has no specific rules for the position at which a statement is to be written in 

a given line. That’s why it is often called a free-form language. #include is a preprocessor directive. 

Then we used int main ( ) with a {}. We used it to start our programming We used printf ( ) to 

show data, information etc. on the screen. We used scanf for taking input from the user. We used 

int here to two variables. Here, we have write a condition in for loop.


Code: 

include<stdio.h>

int main(){

int i,m=1;

for(i=1;i<=5;i++){

printf("%d*",i);

m=m*i; }

printf("total %d",m);

return 0;}


Output:



Discussion: We successfully run the program by using codeblocks. The result was seen on the 

display automatically and the result was absolutely correct. So, we could see that by using C 

programming we can find any numerical sum without any possibility of any kind of error.



Problem : Take an integer from the user determine that number is prime or  not prime.


Solution:




Introduction: A prime number is a positive integer that is divisible only by 1 and 

itself. For example: 2, 3, 5, 7, 11, 13, 17. If n is perfectly divisible by i, n is not a 

prime number.In the program, a for loop is iterated from i = 1 to i <= n.In each 

iteration, whether n is perfectly divisible by i is checked using.


Code: 

#include <stdio.h>

int main() {

 int n, i, c = 0;

 printf("Enter number n:");

 scanf("%d", &n);

for (i = 1; i <= n; i++) {

 if (n % i == 0) {

 c++;}}

if (c == 2) {

 printf("n is a Prime number");}

 else {

 printf("n is not a Prime number");}

 return 0;}


Output:





Discussion: In this program we have studied about “printf, float, + operator, return 

0 ”. Our primary goal was to print the result of area of a circle as an output. The 

program ran in 5.421 seconds successfully. Thus, it can be observe that the whole 

program was complete righteous. 








0 Comments

Post a Comment

Post a Comment (0)

Previous Post Next Post