Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
81 views10 pages

Assignment #4: Department of CS&IT (BSSE)

This document contains an assignment submission for a programming fundamentals course. It includes 8 sections with 2 programming code examples in each section covering various fundamental concepts like input/output, conditional statements, loops, functions, and arrays. The concepts covered include printing text, if-else, switch, for loop, while loop, do-while loop, functions, and arrays. Each code example is accompanied by its corresponding output.

Uploaded by

shorif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views10 pages

Assignment #4: Department of CS&IT (BSSE)

This document contains an assignment submission for a programming fundamentals course. It includes 8 sections with 2 programming code examples in each section covering various fundamental concepts like input/output, conditional statements, loops, functions, and arrays. The concepts covered include printing text, if-else, switch, for loop, while loop, do-while loop, functions, and arrays. Each code example is accompanied by its corresponding output.

Uploaded by

shorif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Assignment #4

Subject: Programming Fundamentals


Semester: 1st
Submitted To: Sir. Junaid
Submitted By: Zohaib Zeeshan
Roll No: BSSE-F17-57
Date: 22/01/2018

Department of CS&IT
(BSSE)

University Of Sargodha Mandi Bahauddin Campus

1
Programming Fundamentals
(1). Printing text on screen:
a). Write a simple C program.
Code:
#include<stdio.h>
int main(void)
{
printf(“Welcome to C!”);
return 0;
}
Output:

b). Write a program to find sum of two integers.


Code:
#include<stdio.h>
int main()
{
int a=5;
int b=7;
int sum;
sum = a+b;
printf("The sum is = %d", sum);
}
Output:

(2). Write two code examples of if-else.


a). Find maximum between two numbers.
Code:
#include<stdio.h>
int main()
{
int num1, num2;
printf("Enter two integers to find which is maximum\n");
scanf("%d%d", &num1, &num2);
if(num1 > num2){
printf("First numbers is maximum\n");
}
else
{
printf("Second is maximum\n");
}
return 0;
}

2
Output:

b). Write a program to check an integer is even or odd.


Code:
#include<stdio.h>
int main()
{
int num;
printf("Enter an integer to check even or odd\n");
scanf("%d", &num);
if(num % 2 == 0)
{
printf("Even\n");
}
else
{
printf("Odd\n");
}
}
Output:

(3). Write two code examples of switch statement.


a). Write a code to check an alphabet is vowel or consonant.
Code:
#include<stdio.h>
int main ()
{
char ch;
printf("Enter an alphabet\n");
scanf("%c", &ch);
switch (ch)
{
case'a':
printf("a is vowel\n");
break;
case 'e':
printf("e is vowel\n");
break;

3
case 'i':
printf("i is vowel\n");
break;
case 'o':
printf("o is vowel\n");
break;
case 'u':
printf("u is wowel\n");
break;
case 'A':
printf("A is vowel\n");
break;
case 'E':
printf("E is vowel\n");
break;
case 'I':
printf("I is vowel\n");
break;
case 'O':
printf("O is vowel\n");
break;
case 'U':

printf("U is vowel\n");

break;

default:

printf("is consonant");
}
return 0;
}
Output:

b). Write a code to check number is even or odd.


Code:
#include<stdio.h>
int main()
{
int num;
printf("Enter a number to check even or odd\n");
scanf("%d", &num);
switch(num % 2){

4
case 0:
printf("Number is Even\n");
break;
case 1:
printf("Number is Odd\n");
break;
}
}
Output:

(4). Write two code examples of For Loop.


a). C program to find power of a number using for loop.
Code:
#include<stdio.h>
int main(){
int base,exponent;
int power = 1;
int i;
printf("Enter base: \n");
scanf("%d", &base);
printf("Enter exponenet: \n");
scanf("%d", &exponent);
for(i=1; i<=exponent; i++){
power=power*base;
}
printf("%d ^ %d = %d", base, exponent, power);
return 0;
}
Output:

b). C program to print all even numbers from 1 to n.


Code:
#include<stdio.h>
int main(){
int i,n;
printf("Print all even numbers: \n");
scanf("%d", &n);
printf("Even numbers from 1 to %d are \n", n);

5
for(i=1; i<=n; i++){
if(i%2 == 0){
printf("%d ", i);
}
}
return 0;
}
Output:

(5). Write two code examples using while loop.


a). C program to print multiplication table of a number using while loop.
Code:
#include <stdio.h>
int main()
{
int i, num;
printf("Enter number to print table: ");
scanf("%d", &num);
while(i <=10)
{
printf("%d * %d = %d\n", num, i, (num*i));
i++;
}
return 0;
}
Output:

b). Write a program to genrate star pattern as shown below using while loop.
Code:
#include<stdio.h>

int main()
{
int i,j;
6
i=1;
while(i<=5){
printf("");
j=1;
while(j<=i)
{
printf("*");
j++;
}
printf("\n");
i++;
}
return 0;
}
Output:

(6).Write two code examples using do while loop.


a). Value of a using do while loop.
Code:
#include <stdio.h>
int main(){
int a = 0;
// do loop execution
do {
printf("value of a: %d\n", a);
a++;
}
while( a <= 5 );
return 0;
}
Output:

b). C program to print the table of 5 from 1 to 10.


Code:
#include<stdio.h>
int main()
{
int i=1;
do
{
printf("5 * %d = %d\n",i,5*i);
7
i++;
}
while(i<=10);
return 0;
}
Output:

(7). Write two code examples of Functions.


a). C program to find cube of a number using function.
Code:
#include <stdio.h>
/* Function declaration */
int cube(int num);
int main(){
int num;
int c;
printf("Enter any number: ");
scanf("%d", &num);
c = cube(num);
printf("Cube of %d is %d", num, c);
return 0;
}
int cube(int num)
{
return (num * num * num);
}
Output:

b). Find factorial of a number using function.


Code:
#include<stdio.h>
int factorial(int);
int main(){
int fact;
int numbr;

printf("Enter a number: ");


scanf("%d",&numbr);

8
fact= factorial(numbr);
printf("Factorial of %d is: %d",numbr,fact);
return 0;
}
int factorial(int n){
int i;
int factorial;
factorial =1;
for(i=1;i<=n;i++)
factorial=factorial*i;
return(factorial);
}
Output:

(8). Write two code examples of Array.


a). Write a program to find repeated elements using array.
Code:
#include<stdio.h>
int main(){
int i,arr[20],j,num;
printf("Enter size of array: ");
scanf("%d",&num);
printf("Enter any %d elements in array: ",num);
for(i=0;i<num;i++)
{
scanf("%d",&arr[i]);
}
printf("Repeated elements are: \n");
for(i=0; i<num; i++)
{
for(j=i+1;j<num;j++)
{
if(arr[i]==arr[j])
{
printf("%d\n",arr[i]);
}
}
}
return 0;
}
Output:

9
b). Find largest element using array.
Code:
#include <stdio.h>
int main()
{
int array[50], size, i, largest;
printf("Enter the size of the array: \n");
scanf("%d", &size);
printf("Enter %d elements of the array: \n", size);
for(i=0; i<size; i++){
scanf("%d", &array[i]);}
largest = array[0];
for (i = 1; i < size; i++)
{
if (largest < array[i])
largest = array[i];
}
printf("The largest element is : %d\n", largest);
return 0;
}
Output:

10

You might also like